From 4115d04777837dbff7198df6ab75ffd19e6ada3c Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Tue, 21 Oct 2014 10:55:30 -0700 Subject: Fixes #1024 -- a utility function for checking an implementor against an ABC --- cryptography/utils.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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() -- cgit v1.2.3