mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Upgrade Lib/types.py from Python 3.13.5 (#5928)
This commit is contained in:
1 parent
cbd9b30bd1
commit
54ff198409
1 file changed
+43
-9
Vendored
+43
-9
@@ -1,6 +1,7 @@
|
||||
"""
|
||||
Define names for built-in types that aren't directly accessible as a builtin.
|
||||
"""
|
||||
|
||||
import sys
|
||||
|
||||
# Iterators in Python aren't a matter of type but of protocol. A large
|
||||
@@ -52,17 +53,14 @@ ModuleType = type(sys)
|
||||
|
||||
try:
|
||||
raise TypeError
|
||||
except TypeError:
|
||||
tb = sys.exc_info()[2]
|
||||
TracebackType = type(tb)
|
||||
FrameType = type(tb.tb_frame)
|
||||
tb = None; del tb
|
||||
except TypeError as exc:
|
||||
TracebackType = type(exc.__traceback__)
|
||||
FrameType = type(exc.__traceback__.tb_frame)
|
||||
|
||||
# For Jython, the following two types are identical
|
||||
GetSetDescriptorType = type(FunctionType.__code__)
|
||||
MemberDescriptorType = type(FunctionType.__globals__)
|
||||
|
||||
del sys, _f, _g, _C, _c, _ag # Not for export
|
||||
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export
|
||||
|
||||
|
||||
# Provide a PEP 3115 compliant mechanism for class creation
|
||||
@@ -82,7 +80,7 @@ def resolve_bases(bases):
|
||||
updated = False
|
||||
shift = 0
|
||||
for i, base in enumerate(bases):
|
||||
if isinstance(base, type) and not isinstance(base, GenericAlias):
|
||||
if isinstance(base, type):
|
||||
continue
|
||||
if not hasattr(base, "__mro_entries__"):
|
||||
continue
|
||||
@@ -146,6 +144,35 @@ def _calculate_meta(meta, bases):
|
||||
"of the metaclasses of all its bases")
|
||||
return winner
|
||||
|
||||
|
||||
def get_original_bases(cls, /):
|
||||
"""Return the class's "original" bases prior to modification by `__mro_entries__`.
|
||||
|
||||
Examples::
|
||||
|
||||
from typing import TypeVar, Generic, NamedTuple, TypedDict
|
||||
|
||||
T = TypeVar("T")
|
||||
class Foo(Generic[T]): ...
|
||||
class Bar(Foo[int], float): ...
|
||||
class Baz(list[str]): ...
|
||||
Eggs = NamedTuple("Eggs", [("a", int), ("b", str)])
|
||||
Spam = TypedDict("Spam", {"a": int, "b": str})
|
||||
|
||||
assert get_original_bases(Bar) == (Foo[int], float)
|
||||
assert get_original_bases(Baz) == (list[str],)
|
||||
assert get_original_bases(Eggs) == (NamedTuple,)
|
||||
assert get_original_bases(Spam) == (TypedDict,)
|
||||
assert get_original_bases(int) == (object,)
|
||||
"""
|
||||
try:
|
||||
return cls.__dict__.get("__orig_bases__", cls.__bases__)
|
||||
except AttributeError:
|
||||
raise TypeError(
|
||||
f"Expected an instance of type, not {type(cls).__name__!r}"
|
||||
) from None
|
||||
|
||||
|
||||
class DynamicClassAttribute:
|
||||
"""Route attribute access on a class to __getattr__.
|
||||
|
||||
@@ -158,7 +185,7 @@ class DynamicClassAttribute:
|
||||
attributes on the class with the same name. (Enum used this between Python
|
||||
versions 3.4 - 3.9 .)
|
||||
|
||||
Subclass from this to use a different method of accessing virtual atributes
|
||||
Subclass from this to use a different method of accessing virtual attributes
|
||||
and still be treated properly by the inspect module. (Enum uses this since
|
||||
Python 3.10 .)
|
||||
|
||||
@@ -305,4 +332,11 @@ EllipsisType = type(Ellipsis)
|
||||
NoneType = type(None)
|
||||
NotImplementedType = type(NotImplemented)
|
||||
|
||||
def __getattr__(name):
|
||||
if name == 'CapsuleType':
|
||||
import _socket
|
||||
return type(_socket.CAPI)
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
|
||||
__all__ = [n for n in globals() if n[:1] != '_']
|
||||
__all__ += ['CapsuleType']
|
||||
Reference in new issue
Block a user