diff options
author | Alex Gaynor <alex.gaynor@gmail.com> | 2014-10-21 10:55:30 -0700 |
---|---|---|
committer | Alex Gaynor <alex.gaynor@gmail.com> | 2014-10-21 10:55:30 -0700 |
commit | 4115d04777837dbff7198df6ab75ffd19e6ada3c (patch) | |
tree | df5395073f97bc3a6881879b5599020bf76a3fb4 | |
parent | 126afca70edc3fac2e493c6b7cd05219c8d8e373 (diff) | |
download | cryptography-4115d04777837dbff7198df6ab75ffd19e6ada3c.tar.gz cryptography-4115d04777837dbff7198df6ab75ffd19e6ada3c.tar.bz2 cryptography-4115d04777837dbff7198df6ab75ffd19e6ada3c.zip |
Fixes #1024 -- a utility function for checking an implementor against an ABC
-rw-r--r-- | cryptography/utils.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/cryptography/utils.py b/cryptography/utils.py index 55187c3b..636776f4 100644 --- a/cryptography/utils.py +++ b/cryptography/utils.py @@ -13,6 +13,7 @@ from __future__ import absolute_import, division, print_function +import inspect import sys @@ -26,6 +27,26 @@ def register_interface(iface): return register_decorator +class InterfaceNotImplemented(Exception): + pass + + +def verify_interface(iface, klass): + for method in iface.__abstractmethods__: + if not hasattr(klass, method): + raise InterfaceNotImplemented( + "{0} is missing a {1!r} method".format(klass, method) + ) + spec = getattr(iface, method).__func__ + actual = getattr(klass, method) + if inspect.getargspec(spec) != inspect.getargspec(actual): + raise InterfaceNotImplemented( + "{0}.{1}'s signature differs from the expected".format( + klass, method + ) + ) + + def bit_length(x): if sys.version_info >= (2, 7): return x.bit_length() |