|
2 | 2 | Define names for built-in types that aren't directly accessible as a builtin. |
3 | 3 | """ |
4 | 4 |
|
5 | | -import _types |
6 | | - |
7 | 5 | # Iterators in Python aren't a matter of type but of protocol. A large |
8 | 6 | # and changing number of builtin types implement *some* flavor of |
9 | 7 | # iterator. Don't check the type! Use hasattr to check for both |
10 | 8 | # "__iter__" and "__next__" attributes instead. |
11 | 9 |
|
12 | | -def _f(): pass |
13 | | -FunctionType = type(_f) |
14 | | -LambdaType = type(lambda: None) # Same as FunctionType |
15 | | -CodeType = type(_f.__code__) |
16 | | -MappingProxyType = type(type.__dict__) |
17 | | -SimpleNamespace = _types.SimpleNamespace |
18 | | - |
19 | | -def _cell_factory(): |
20 | | - a = 1 |
21 | | - def f(): |
22 | | - nonlocal a |
23 | | - return f.__closure__[0] |
24 | | -CellType = type(_cell_factory()) |
25 | | - |
26 | | -def _g(): |
27 | | - yield 1 |
28 | | -GeneratorType = type(_g()) |
29 | | - |
30 | | -async def _c(): pass |
31 | | -_c = _c() |
32 | | -CoroutineType = type(_c) |
33 | | -_c.close() # Prevent ResourceWarning |
34 | | - |
35 | | -async def _ag(): |
36 | | - yield |
37 | | -_ag = _ag() |
38 | | -AsyncGeneratorType = type(_ag) |
39 | | - |
40 | | -class _C: |
41 | | - def _m(self): pass |
42 | | -MethodType = type(_C()._m) |
43 | | - |
44 | | -BuiltinFunctionType = type(len) |
45 | | -BuiltinMethodType = type([].append) # Same as BuiltinFunctionType |
| 10 | +try: |
| 11 | + from _types import * |
| 12 | +except ImportError: |
| 13 | + import sys |
| 14 | + |
| 15 | + def _f(): pass |
| 16 | + FunctionType = type(_f) |
| 17 | + LambdaType = type(lambda: None) # Same as FunctionType |
| 18 | + CodeType = type(_f.__code__) |
| 19 | + MappingProxyType = type(type.__dict__) |
| 20 | + SimpleNamespace = type(sys.implementation) |
| 21 | + |
| 22 | + def _cell_factory(): |
| 23 | + a = 1 |
| 24 | + def f(): |
| 25 | + nonlocal a |
| 26 | + return f.__closure__[0] |
| 27 | + CellType = type(_cell_factory()) |
| 28 | + |
| 29 | + def _g(): |
| 30 | + yield 1 |
| 31 | + GeneratorType = type(_g()) |
| 32 | + |
| 33 | + async def _c(): pass |
| 34 | + _c = _c() |
| 35 | + CoroutineType = type(_c) |
| 36 | + _c.close() # Prevent ResourceWarning |
| 37 | + |
| 38 | + async def _ag(): |
| 39 | + yield |
| 40 | + _ag = _ag() |
| 41 | + AsyncGeneratorType = type(_ag) |
| 42 | + |
| 43 | + class _C: |
| 44 | + def _m(self): pass |
| 45 | + MethodType = type(_C()._m) |
| 46 | + |
| 47 | + BuiltinFunctionType = type(len) |
| 48 | + BuiltinMethodType = type([].append) # Same as BuiltinFunctionType |
| 49 | + |
| 50 | + WrapperDescriptorType = type(object.__init__) |
| 51 | + MethodWrapperType = type(object().__str__) |
| 52 | + MethodDescriptorType = type(str.join) |
| 53 | + ClassMethodDescriptorType = type(dict.__dict__['fromkeys']) |
| 54 | + |
| 55 | + ModuleType = type(sys) |
46 | 56 |
|
47 | | -WrapperDescriptorType = type(object.__init__) |
48 | | -MethodWrapperType = type(object().__str__) |
49 | | -MethodDescriptorType = type(str.join) |
50 | | -ClassMethodDescriptorType = type(dict.__dict__['fromkeys']) |
| 57 | + try: |
| 58 | + raise TypeError |
| 59 | + except TypeError as exc: |
| 60 | + TracebackType = type(exc.__traceback__) |
| 61 | + FrameType = type(exc.__traceback__.tb_frame) |
51 | 62 |
|
52 | | -ModuleType = type(_types) |
| 63 | + GetSetDescriptorType = type(FunctionType.__code__) |
| 64 | + MemberDescriptorType = type(FunctionType.__globals__) |
53 | 65 |
|
54 | | -try: |
55 | | - raise TypeError |
56 | | -except TypeError as exc: |
57 | | - TracebackType = type(exc.__traceback__) |
58 | | - FrameType = type(exc.__traceback__.tb_frame) |
| 66 | + GenericAlias = type(list[int]) |
| 67 | + UnionType = type(int | str) |
59 | 68 |
|
60 | | -GetSetDescriptorType = type(FunctionType.__code__) |
61 | | -MemberDescriptorType = type(FunctionType.__globals__) |
| 69 | + EllipsisType = type(Ellipsis) |
| 70 | + NoneType = type(None) |
| 71 | + NotImplementedType = type(NotImplemented) |
62 | 72 |
|
63 | | -CapsuleType = _types.CapsuleType |
| 73 | + # CapsuleType cannot be accessed from pure Python, |
| 74 | + # so there is no fallback definition. |
64 | 75 |
|
65 | | -del _types, _f, _g, _C, _c, _ag, _cell_factory # Not for export |
| 76 | + del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export |
66 | 77 |
|
67 | 78 |
|
68 | 79 | # Provide a PEP 3115 compliant mechanism for class creation |
@@ -326,11 +337,4 @@ def wrapped(*args, **kwargs): |
326 | 337 |
|
327 | 338 | return wrapped |
328 | 339 |
|
329 | | -GenericAlias = type(list[int]) |
330 | | -UnionType = type(int | str) |
331 | | - |
332 | | -EllipsisType = type(Ellipsis) |
333 | | -NoneType = type(None) |
334 | | -NotImplementedType = type(NotImplemented) |
335 | | - |
336 | 340 | __all__ = [n for n in globals() if not n.startswith('_')] # for pydoc |
0 commit comments