utils.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. PersistentlyDeprecated2019 = CryptographyDeprecationWarning
  19. DeprecatedIn35 = CryptographyDeprecationWarning
  20. DeprecatedIn36 = CryptographyDeprecationWarning
  21. DeprecatedIn37 = 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. if typing.TYPE_CHECKING:
  31. from typing_extensions import Protocol
  32. _T_class = typing.TypeVar("_T_class", bound=type)
  33. class _RegisterDecoratorType(Protocol):
  34. def __call__(
  35. self, klass: _T_class, *, check_annotations: bool = False
  36. ) -> _T_class:
  37. ...
  38. def register_interface(iface: abc.ABCMeta) -> "_RegisterDecoratorType":
  39. def register_decorator(
  40. klass: "_T_class", *, check_annotations: bool = False
  41. ) -> "_T_class":
  42. verify_interface(iface, klass, check_annotations=check_annotations)
  43. iface.register(klass)
  44. return klass
  45. return register_decorator
  46. def int_to_bytes(integer: int, length: typing.Optional[int] = None) -> bytes:
  47. return integer.to_bytes(
  48. length or (integer.bit_length() + 7) // 8 or 1, "big"
  49. )
  50. class InterfaceNotImplemented(Exception):
  51. pass
  52. def strip_annotation(signature: inspect.Signature) -> inspect.Signature:
  53. return inspect.Signature(
  54. [
  55. param.replace(annotation=inspect.Parameter.empty)
  56. for param in signature.parameters.values()
  57. ]
  58. )
  59. def verify_interface(
  60. iface: abc.ABCMeta, klass: object, *, check_annotations: bool = False
  61. ):
  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:
  82. def __init__(self, value: object, message: str, 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: types.ModuleType):
  88. super().__init__(module.__name__)
  89. self.__dict__["_module"] = module
  90. def __getattr__(self, attr: str) -> object:
  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: str, value: object) -> None:
  97. setattr(self._module, attr, value)
  98. def __delattr__(self, attr: str) -> None:
  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) -> typing.Sequence[str]:
  104. return ["_module"] + dir(self._module)
  105. def deprecated(
  106. value: object,
  107. module_name: str,
  108. message: str,
  109. warning_class: typing.Type[Warning],
  110. name: typing.Optional[str] = None,
  111. ) -> _DeprecatedValue:
  112. module = sys.modules[module_name]
  113. if not isinstance(module, _ModuleWithDeprecations):
  114. sys.modules[module_name] = module = _ModuleWithDeprecations(module)
  115. dv = _DeprecatedValue(value, message, warning_class)
  116. # Maintain backwards compatibility with `name is None` for pyOpenSSL.
  117. if name is not None:
  118. setattr(module, name, dv)
  119. return dv
  120. def cached_property(func: typing.Callable) -> property:
  121. cached_name = "_cached_{}".format(func)
  122. sentinel = object()
  123. def inner(instance: object):
  124. cache = getattr(instance, cached_name, sentinel)
  125. if cache is not sentinel:
  126. return cache
  127. result = func(instance)
  128. setattr(instance, cached_name, result)
  129. return result
  130. return property(inner)
  131. # Python 3.10 changed representation of enums. We use well-defined object
  132. # representation and string representation from Python 3.9.
  133. class Enum(enum.Enum):
  134. def __repr__(self) -> str:
  135. return f"<{self.__class__.__name__}.{self._name_}: {self._value_!r}>"
  136. def __str__(self) -> str:
  137. return f"{self.__class__.__name__}.{self._name_}"