GH-118761: Expose more core interpreter types in ``_types`` (#132103) · python/cpython@1755157 · GitHub
Skip to content

Commit 1755157

Browse files
AA-TurnerpicnixzZeroIntensity
authored
GH-118761: Expose more core interpreter types in _types (#132103)
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent 92fb949 commit 1755157

3 files changed

Lines changed: 128 additions & 63 deletions

File tree

Lib/test/test_types.py

Lines changed: 26 additions & 0 deletions

Lib/types.py

Lines changed: 61 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,78 @@
22
Define names for built-in types that aren't directly accessible as a builtin.
33
"""
44

5-
import _types
6-
75
# Iterators in Python aren't a matter of type but of protocol. A large
86
# and changing number of builtin types implement *some* flavor of
97
# iterator. Don't check the type! Use hasattr to check for both
108
# "__iter__" and "__next__" attributes instead.
119

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)
4656

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)
5162

52-
ModuleType = type(_types)
63+
GetSetDescriptorType = type(FunctionType.__code__)
64+
MemberDescriptorType = type(FunctionType.__globals__)
5365

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)
5968

60-
GetSetDescriptorType = type(FunctionType.__code__)
61-
MemberDescriptorType = type(FunctionType.__globals__)
69+
EllipsisType = type(Ellipsis)
70+
NoneType = type(None)
71+
NotImplementedType = type(NotImplemented)
6272

63-
CapsuleType = _types.CapsuleType
73+
# CapsuleType cannot be accessed from pure Python,
74+
# so there is no fallback definition.
6475

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
6677

6778

6879
# Provide a PEP 3115 compliant mechanism for class creation
@@ -326,11 +337,4 @@ def wrapped(*args, **kwargs):
326337

327338
return wrapped
328339

329-
GenericAlias = type(list[int])
330-
UnionType = type(int | str)
331-
332-
EllipsisType = type(Ellipsis)
333-
NoneType = type(None)
334-
NotImplementedType = type(NotImplemented)
335-
336340
__all__ = [n for n in globals() if not n.startswith('_')] # for pydoc

Modules/_typesmodule.c

Lines changed: 41 additions & 6 deletions

0 commit comments

Comments
 (0)