utils.py 5.3 KB

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