Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/PyNaCl-1.6.0.dev1-py3.8-linux-x86_64.egg/nacl/exceptions.py: 96%
26 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 06:06 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-06 06:06 +0000
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.
16# We create a clone of various builtin Exception types which additionally
17# inherit from CryptoError. Below, we refer to the parent types via the
18# `builtins` namespace, so mypy can distinguish between (e.g.)
19# `nacl.exceptions.RuntimeError` and `builtins.RuntimeError`.
20import builtins
21from typing import Type
24class CryptoError(Exception):
25 """
26 Base exception for all nacl related errors
27 """
30class BadSignatureError(CryptoError):
31 """
32 Raised when the signature was forged or otherwise corrupt.
33 """
36class RuntimeError(builtins.RuntimeError, CryptoError):
37 pass
40class AssertionError(builtins.AssertionError, CryptoError):
41 pass
44class TypeError(builtins.TypeError, CryptoError):
45 pass
48class ValueError(builtins.ValueError, CryptoError):
49 pass
52class InvalidkeyError(CryptoError):
53 pass
56class CryptPrefixError(InvalidkeyError):
57 pass
60class UnavailableError(RuntimeError):
61 """
62 is a subclass of :class:`~nacl.exceptions.RuntimeError`, raised when
63 trying to call functions not available in a minimal build of
64 libsodium.
65 """
67 pass
70def ensure(cond: bool, *args: object, **kwds: Type[Exception]) -> None:
71 """
72 Return if a condition is true, otherwise raise a caller-configurable
73 :py:class:`Exception`
74 :param bool cond: the condition to be checked
75 :param sequence args: the arguments to be passed to the exception's
76 constructor
77 The only accepted named parameter is `raising` used to configure the
78 exception to be raised if `cond` is not `True`
79 """
80 _CHK_UNEXP = "check_condition() got an unexpected keyword argument {0}"
82 raising = kwds.pop("raising", AssertionError)
83 if kwds:
84 raise TypeError(_CHK_UNEXP.format(repr(kwds.popitem()[0])))
86 if cond is True:
87 return
88 raise raising(*args)