summaryrefslogtreecommitdiff
path: root/src/cryptography/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/cryptography/utils.py')
-rw-r--r--src/cryptography/utils.py40
1 files changed, 19 insertions, 21 deletions
diff --git a/src/cryptography/utils.py b/src/cryptography/utils.py
index cbbae3a79..bdb3dbf47 100644
--- a/src/cryptography/utils.py
+++ b/src/cryptography/utils.py
@@ -20,22 +20,20 @@ class CryptographyDeprecationWarning(UserWarning):
# Several APIs were deprecated with no specific end-of-life date because of the
# ubiquity of their use. They should not be removed until we agree on when that
# cycle ends.
-PersistentlyDeprecated = CryptographyDeprecationWarning
-DeprecatedIn21 = CryptographyDeprecationWarning
-DeprecatedIn23 = CryptographyDeprecationWarning
-DeprecatedIn25 = CryptographyDeprecationWarning
+PersistentlyDeprecated2017 = CryptographyDeprecationWarning
+PersistentlyDeprecated2019 = CryptographyDeprecationWarning
def _check_bytes(name, value):
if not isinstance(value, bytes):
- raise TypeError("{0} must be bytes".format(name))
+ raise TypeError("{} must be bytes".format(name))
def _check_byteslike(name, value):
try:
memoryview(value)
except TypeError:
- raise TypeError("{0} must be bytes-like".format(name))
+ raise TypeError("{} must be bytes-like".format(name))
def read_only_property(name):
@@ -47,6 +45,7 @@ def register_interface(iface):
verify_interface(iface, klass)
iface.register(klass)
return klass
+
return register_decorator
@@ -56,27 +55,33 @@ def register_interface_if(predicate, iface):
verify_interface(iface, klass)
iface.register(klass)
return klass
+
return register_decorator
if hasattr(int, "from_bytes"):
int_from_bytes = int.from_bytes
else:
+
def int_from_bytes(data, byteorder, signed=False):
- assert byteorder == 'big'
+ assert byteorder == "big"
assert not signed
return int(binascii.hexlify(data), 16)
if hasattr(int, "to_bytes"):
+
def int_to_bytes(integer, length=None):
return integer.to_bytes(
- length or (integer.bit_length() + 7) // 8 or 1, 'big'
+ length or (integer.bit_length() + 7) // 8 or 1, "big"
)
+
+
else:
+
def int_to_bytes(integer, length=None):
- hex_string = '%x' % integer
+ hex_string = "%x" % integer
if length is None:
n = len(hex_string)
else:
@@ -98,7 +103,7 @@ 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)
+ "{} is missing a {!r} method".format(klass, method)
)
if isinstance(getattr(iface, method), abc.abstractproperty):
# Can't properly verify these yet.
@@ -107,19 +112,11 @@ def verify_interface(iface, klass):
actual = signature(getattr(klass, method))
if sig != actual:
raise InterfaceNotImplemented(
- "{0}.{1}'s signature differs from the expected. Expected: "
- "{2!r}. Received: {3!r}".format(
- klass, method, sig, actual
- )
+ "{}.{}'s signature differs from the expected. Expected: "
+ "{!r}. Received: {!r}".format(klass, method, sig, actual)
)
-# No longer needed as of 2.2, but retained because we have external consumers
-# who use it.
-def bit_length(x):
- return x.bit_length()
-
-
class _DeprecatedValue(object):
def __init__(self, value, message, warning_class):
self.value = value
@@ -160,7 +157,7 @@ def deprecated(value, module_name, message, warning_class):
def cached_property(func):
- cached_name = "_cached_{0}".format(func)
+ cached_name = "_cached_{}".format(func)
sentinel = object()
def inner(instance):
@@ -170,4 +167,5 @@ def cached_property(func):
result = func(instance)
setattr(instance, cached_name, result)
return result
+
return property(inner)