You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.8 KiB

7 years ago
  1. # Copyright 2013 Donald Stufft and individual contributors
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import absolute_import, division, print_function
  15. class CryptoError(Exception):
  16. """
  17. Base exception for all nacl related errors
  18. """
  19. class BadSignatureError(CryptoError):
  20. """
  21. Raised when the signature was forged or otherwise corrupt.
  22. """
  23. class RuntimeError(RuntimeError, CryptoError):
  24. pass
  25. class AssertionError(AssertionError, CryptoError):
  26. pass
  27. class TypeError(TypeError, CryptoError):
  28. pass
  29. class ValueError(ValueError, CryptoError):
  30. pass
  31. class InvalidkeyError(CryptoError):
  32. pass
  33. def ensure(cond, *args, **kwds):
  34. """
  35. Return if a condition is true, otherwise raise a caller-configurable
  36. :py:class:`Exception`
  37. :param bool cond: the condition to be checked
  38. :param sequence args: the arguments to be passed to the exception's
  39. constructor
  40. The only accepted named parameter is `raising` used to configure the
  41. exception to be raised if `cond` is not `True`
  42. """
  43. _CHK_UNEXP = 'check_condition() got an unexpected keyword argument {0}'
  44. raising = kwds.pop('raising', AssertionError)
  45. if kwds:
  46. raise TypeError(_CHK_UNEXP.format(repr(kwds.popitem()[0])))
  47. if cond is True:
  48. return
  49. raise raising(*args)

Powered by TurnKey Linux.