This commit is contained in:
wuzilong committed 2026-10-05 12:57:51 +08:00
1 parent 59fafb92d5
commit 6abfb6e251
1512 files changed
+1565614 -275592

No files matched your search

@@ -9,7 +9,7 @@ import sys
import types
import typing
import warnings
from collections.abc import Callable, Sequence
from collections.abc import Sequence
# We use a UserWarning subclass, instead of DeprecationWarning, because CPython
@@ -27,6 +27,7 @@ DeprecatedIn41 = CryptographyDeprecationWarning
DeprecatedIn42 = CryptographyDeprecationWarning
DeprecatedIn43 = CryptographyDeprecationWarning
DeprecatedIn47 = CryptographyDeprecationWarning
DeprecatedIn50 = CryptographyDeprecationWarning
# If you're wondering why we don't use `Buffer`, it's because `Buffer` would
@@ -42,6 +43,8 @@ def _check_bytes(name: str, value: bytes) -> None:
def _check_byteslike(name: str, value: Buffer) -> None:
if isinstance(value, (bytes, bytearray, memoryview)):
return
try:
memoryview(value)
except TypeError:
@@ -77,9 +80,21 @@ class _ModuleWithDeprecations(types.ModuleType):
if isinstance(obj, _DeprecatedValue):
warnings.warn(obj.message, obj.warning_class, stacklevel=2)
obj = obj.value
else:
# Cache non-deprecated attributes in our own `__dict__` so that
# subsequent lookups are ordinary module attribute accesses and
# don't pay for this `__getattr__` (which would otherwise defeat
# CPython's LOAD_ATTR module caching for every attribute of the
# module). `__setattr__` and `__delattr__` keep the cache
# coherent.
self.__dict__[name] = obj
return obj
def __setattr__(self, attr: str, value: object) -> None:
if isinstance(value, _DeprecatedValue):
self.__dict__.pop(attr, None)
else:
self.__dict__[attr] = value
setattr(self._module, attr, value)
def __delattr__(self, attr: str) -> None:
@@ -87,6 +102,7 @@ class _ModuleWithDeprecations(types.ModuleType):
if isinstance(obj, _DeprecatedValue):
warnings.warn(obj.message, obj.warning_class, stacklevel=2)
self.__dict__.pop(attr, None)
delattr(self._module, attr)
def __dir__(self) -> Sequence[str]:
@@ -110,21 +126,6 @@ def deprecated(
return dv
def cached_property(func: Callable) -> property:
cached_name = f"_cached_{func}"
sentinel = object()
def inner(instance: object):
cache = getattr(instance, cached_name, sentinel)
if cache is not sentinel:
return cache
result = func(instance)
setattr(instance, cached_name, result)
return result
return property(inner)
# Python 3.10 changed representation of enums. We use well-defined object
# representation and string representation from Python 3.9.
class Enum(enum.Enum):