diff options
| author | Cory Benfield <lukasaoz@gmail.com> | 2016-03-29 15:32:48 +0100 |
|---|---|---|
| committer | Cory Benfield <lukasaoz@gmail.com> | 2016-03-29 15:32:48 +0100 |
| commit | ef404df8e980888083827f6797cf9d72a3159992 (patch) | |
| tree | 3da05201f4f6dfedc98caefbf87d1b26c6b48e1f /src/OpenSSL/SSL.py | |
| parent | e6f3588e88eb8073384fd59009c37b4ad7f51d9b (diff) | |
| download | pyopenssl-ef404df8e980888083827f6797cf9d72a3159992.tar.gz | |
Factor out common code of requires decorators
Diffstat (limited to 'src/OpenSSL/SSL.py')
| -rw-r--r-- | src/OpenSSL/SSL.py | 61 |
1 files changed, 26 insertions, 35 deletions
diff --git a/src/OpenSSL/SSL.py b/src/OpenSSL/SSL.py index 9eac166..794199c 100644 --- a/src/OpenSSL/SSL.py +++ b/src/OpenSSL/SSL.py @@ -406,50 +406,41 @@ def SSLeay_version(type): return _ffi.string(_lib.SSLeay_version(type)) -def _requires_npn(func): +def _make_requires(flag, error): """ - Wraps any function that requires NPN support in OpenSSL, ensuring that - NotImplementedError is raised if NPN is not present. - """ - @wraps(func) - def wrapper(*args, **kwargs): - if not _lib.Cryptography_HAS_NEXTPROTONEG: - raise NotImplementedError("NPN not available.") - - return func(*args, **kwargs) - - return wrapper + Builds a decorator that ensures that functions that rely on OpenSSL + functions that are not present in this build raise NotImplementedError, + rather than AttributeError coming out of cryptography. - -def _requires_alpn(func): - """ - Wraps any function that requires ALPN support in OpenSSL, ensuring that - NotImplementedError is raised if ALPN support is not present. + :param flag: A cryptography flag that guards the functions, e.g. + ``Cryptography_HAS_NEXTPROTONEG``. + :param error: The string to be used in the exception if the flag is false. """ - @wraps(func) - def wrapper(*args, **kwargs): - if not _lib.Cryptography_HAS_ALPN: - raise NotImplementedError("ALPN not available.") + def _requires_decorator(func): + if not flag: + @wraps(func) + def explode(*args, **kwargs): + raise NotImplementedError(error) + return explode + else: + return func - return func(*args, **kwargs) + return _requires_decorator - return wrapper +_requires_npn = _make_requires( + _lib.Cryptography_HAS_NEXTPROTONEG, "NPN not available" +) -def _requires_sni(func): - """ - Wraps any function that requires SNI support in OpenSSL, ensuring that - NotImplementedError is raised if SNI support is not present. This applies - to OpenSSL versions older than 1.0.0. - """ - @wraps(func) - def wrapper(*args, **kwargs): - if not _lib.Cryptography_HAS_TLSEXT_HOSTNAME: - raise NotImplementedError("SNI not available: OpenSSL too old.") - return func(*args, **kwargs) +_requires_alpn = _make_requires( + _lib.Cryptography_HAS_ALPN, "ALPN not available" +) + - return wrapper +_requires_sni = _make_requires( + _lib.Cryptography_HAS_TLSEXT_HOSTNAME, "SNI not available" +) class Session(object): |
