Fixes #1024 -- a utility function for checking an implementor against an ABC
diff --git a/cryptography/utils.py b/cryptography/utils.py
index 55187c3..636776f 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 @@
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()