utils.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. import abc
  5. import inspect
  6. import sys
  7. import typing
  8. import warnings
  9. # We use a UserWarning subclass, instead of DeprecationWarning, because CPython
  10. # decided deprecation warnings should be invisble by default.
  11. class CryptographyDeprecationWarning(UserWarning):
  12. pass
  13. # Several APIs were deprecated with no specific end-of-life date because of the
  14. # ubiquity of their use. They should not be removed until we agree on when that
  15. # cycle ends.
  16. PersistentlyDeprecated2017 = CryptographyDeprecationWarning
  17. PersistentlyDeprecated2019 = CryptographyDeprecationWarning
  18. DeprecatedIn34 = CryptographyDeprecationWarning
  19. def _check_bytes(name: str, value: bytes):
  20. if not isinstance(value, bytes):
  21. raise TypeError("{} must be bytes".format(name))
  22. def _check_byteslike(name: str, value: bytes):
  23. try:
  24. memoryview(value)
  25. except TypeError:
  26. raise TypeError("{} must be bytes-like".format(name))
  27. def read_only_property(name: str):
  28. return property(lambda self: getattr(self, name))
  29. def register_interface(iface):
  30. def register_decorator(klass, *, check_annotations=False):
  31. verify_interface(iface, klass, check_annotations=check_annotations)
  32. iface.register(klass)
  33. return klass
  34. return register_decorator
  35. def register_interface_if(predicate, iface):
  36. def register_decorator(klass, *, check_annotations=False):
  37. if predicate:
  38. verify_interface(iface, klass, check_annotations=check_annotations)
  39. iface.register(klass)
  40. return klass
  41. return register_decorator
  42. def int_to_bytes(integer: int, length: typing.Optional[int] = None) -> bytes:
  43. return integer.to_bytes(
  44. length or (integer.bit_length() + 7) // 8 or 1, "big"
  45. )
  46. class InterfaceNotImplemented(Exception):
  47. pass
  48. def strip_annotation(signature):
  49. return inspect.Signature(
  50. [
  51. param.replace(annotation=inspect.Parameter.empty)
  52. for param in signature.parameters.values()
  53. ]
  54. )
  55. def verify_interface(iface, klass, *, check_annotations=False):
  56. for method in iface.__abstractmethods__:
  57. if not hasattr(klass, method):
  58. raise InterfaceNotImplemented(
  59. "{} is missing a {!r} method".format(klass, method)
  60. )
  61. if isinstance(getattr(iface, method), abc.abstractproperty):
  62. # Can't properly verify these yet.
  63. continue
  64. sig = inspect.signature(getattr(iface, method))
  65. actual = inspect.signature(getattr(klass, method))
  66. if check_annotations:
  67. ok = sig == actual
  68. else:
  69. ok = strip_annotation(sig) == strip_annotation(actual)
  70. if not ok:
  71. raise InterfaceNotImplemented(
  72. "{}.{}'s signature differs from the expected. Expected: "
  73. "{!r}. Received: {!r}".format(klass, method, sig, actual)
  74. )
  75. class _DeprecatedValue(object):
  76. def __init__(self, value, message, warning_class):
  77. self.value = value
  78. self.message = message
  79. self.warning_class = warning_class
  80. class _ModuleWithDeprecations(object):
  81. def __init__(self, module):
  82. self.__dict__["_module"] = module
  83. def __getattr__(self, attr):
  84. obj = getattr(self._module, attr)
  85. if isinstance(obj, _DeprecatedValue):
  86. warnings.warn(obj.message, obj.warning_class, stacklevel=2)
  87. obj = obj.value
  88. return obj
  89. def __setattr__(self, attr, value):
  90. setattr(self._module, attr, value)
  91. def __delattr__(self, attr):
  92. obj = getattr(self._module, attr)
  93. if isinstance(obj, _DeprecatedValue):
  94. warnings.warn(obj.message, obj.warning_class, stacklevel=2)
  95. delattr(self._module, attr)
  96. def __dir__(self):
  97. return ["_module"] + dir(self._module)
  98. def deprecated(value, module_name, message, warning_class):
  99. module = sys.modules[module_name]
  100. if not isinstance(module, _ModuleWithDeprecations):
  101. sys.modules[module_name] = _ModuleWithDeprecations(
  102. module
  103. ) # type: ignore[assignment]
  104. return _DeprecatedValue(value, message, warning_class)
  105. def cached_property(func):
  106. cached_name = "_cached_{}".format(func)
  107. sentinel = object()
  108. def inner(instance):
  109. cache = getattr(instance, cached_name, sentinel)
  110. if cache is not sentinel:
  111. return cache
  112. result = func(instance)
  113. setattr(instance, cached_name, result)
  114. return result
  115. return property(inner)
  116. int_from_bytes = deprecated(
  117. int.from_bytes,
  118. __name__,
  119. "int_from_bytes is deprecated, use int.from_bytes instead",
  120. DeprecatedIn34,
  121. )