aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/cryptography/utils.py9
-rw-r--r--tests/test_interfaces.py28
2 files changed, 36 insertions, 1 deletions
diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py
index d3e845ab..48ed449a 100644
--- a/src/cryptography/utils.py
+++ b/src/cryptography/utils.py
@@ -31,6 +31,15 @@ def register_interface(iface):
return register_decorator
+def register_interface_if(predicate, iface):
+ def register_decorator(klass):
+ if predicate:
+ verify_interface(iface, klass)
+ iface.register(klass)
+ return klass
+ return register_decorator
+
+
if hasattr(int, "from_bytes"):
int_from_bytes = int.from_bytes
else:
diff --git a/tests/test_interfaces.py b/tests/test_interfaces.py
index bdb4a94d..97df45a3 100644
--- a/tests/test_interfaces.py
+++ b/tests/test_interfaces.py
@@ -8,7 +8,33 @@ import pytest
import six
-from cryptography.utils import InterfaceNotImplemented, verify_interface
+from cryptography.utils import (
+ InterfaceNotImplemented, register_interface_if, verify_interface
+)
+
+
+def test_register_interface_if_true():
+ @six.add_metaclass(abc.ABCMeta)
+ class SimpleInterface(object):
+ pass
+
+ @register_interface_if(1 == 1, SimpleInterface)
+ class SimpleClass(object):
+ pass
+
+ assert issubclass(SimpleClass, SimpleInterface) is True
+
+
+def test_register_interface_if_false():
+ @six.add_metaclass(abc.ABCMeta)
+ class SimpleInterface(object):
+ pass
+
+ @register_interface_if(1 == 2, SimpleInterface)
+ class SimpleClass(object):
+ pass
+
+ assert issubclass(SimpleClass, SimpleInterface) is False
class TestVerifyInterface(object):