Skip to content

Commit 737216d

Browse files
committed
Expose more core types in _types
1 parent 231a50f commit 737216d

File tree

2 files changed

+141
-57
lines changed

2 files changed

+141
-57
lines changed

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: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,97 @@
11
/* _types module */
22

33
#include "Python.h"
4+
#include "pycore_descrobject.h" // _PyMethodWrapper_Type
45
#include "pycore_namespace.h" // _PyNamespace_Type
6+
#include "pycore_object.h" // _PyNone_Type, _PyNotImplemented_Type
7+
#include "pycore_unionobject.h" // _PyUnion_Type
58

69
static int
710
_types_exec(PyObject *m)
811
{
12+
if (PyModule_AddObjectRef(m, "AsyncGeneratorType", (PyObject *)&PyAsyncGen_Type) < 0) {
13+
return -1;
14+
}
15+
if (PyModule_AddObjectRef(m, "BuiltinFunctionType", (PyObject *)&PyCFunction_Type) < 0) {
16+
return -1;
17+
}
18+
// Same as BuiltinMethodType
19+
if (PyModule_AddObjectRef(m, "BuiltinMethodType", (PyObject *)&PyCFunction_Type) < 0) {
20+
return -1;
21+
}
922
if (PyModule_AddObjectRef(m, "CapsuleType", (PyObject *)&PyCapsule_Type) < 0) {
1023
return -1;
1124
}
25+
if (PyModule_AddObjectRef(m, "CellType", (PyObject *)&PyCell_Type) < 0) {
26+
return -1;
27+
}
28+
if (PyModule_AddObjectRef(m, "ClassMethodDescriptorType", (PyObject *)&PyClassMethodDescr_Type) < 0) {
29+
return -1;
30+
}
31+
if (PyModule_AddObjectRef(m, "CodeType", (PyObject *)&PyCode_Type) < 0) {
32+
return -1;
33+
}
34+
if (PyModule_AddObjectRef(m, "CoroutineType", (PyObject *)&PyCoro_Type) < 0) {
35+
return -1;
36+
}
37+
if (PyModule_AddObjectRef(m, "EllipsisType", (PyObject *)&PyEllipsis_Type) < 0) {
38+
return -1;
39+
}
40+
if (PyModule_AddObjectRef(m, "FrameType", (PyObject *)&PyFrame_Type) < 0) {
41+
return -1;
42+
}
43+
if (PyModule_AddObjectRef(m, "FunctionType", (PyObject *)&PyFunction_Type) < 0) {
44+
return -1;
45+
}
46+
if (PyModule_AddObjectRef(m, "GeneratorType", (PyObject *)&PyGen_Type) < 0) {
47+
return -1;
48+
}
49+
if (PyModule_AddObjectRef(m, "GenericAlias", (PyObject *)&Py_GenericAliasType) < 0) {
50+
return -1;
51+
}
52+
if (PyModule_AddObjectRef(m, "GetSetDescriptorType", (PyObject *)&PyGetSetDescr_Type) < 0) {
53+
return -1;
54+
}
55+
// Same as FunctionType
56+
if (PyModule_AddObjectRef(m, "LambdaType", (PyObject *)&PyFunction_Type) < 0) {
57+
return -1;
58+
}
59+
if (PyModule_AddObjectRef(m, "MappingProxyType", (PyObject *)&PyDictProxy_Type) < 0) {
60+
return -1;
61+
}
62+
if (PyModule_AddObjectRef(m, "MemberDescriptorType", (PyObject *)&PyMemberDescr_Type) < 0) {
63+
return -1;
64+
}
65+
if (PyModule_AddObjectRef(m, "MethodDescriptorType", (PyObject *)&PyMethodDescr_Type) < 0) {
66+
return -1;
67+
}
68+
if (PyModule_AddObjectRef(m, "MethodType", (PyObject *)&PyMethod_Type) < 0) {
69+
return -1;
70+
}
71+
if (PyModule_AddObjectRef(m, "MethodWrapperType", (PyObject *)&_PyMethodWrapper_Type) < 0) {
72+
return -1;
73+
}
74+
if (PyModule_AddObjectRef(m, "ModuleType", (PyObject *)&PyModule_Type) < 0) {
75+
return -1;
76+
}
77+
if (PyModule_AddObjectRef(m, "NoneType", (PyObject *)&_PyNone_Type) < 0) {
78+
return -1;
79+
}
80+
if (PyModule_AddObjectRef(m, "NotImplementedType", (PyObject *)&_PyNotImplemented_Type) < 0) {
81+
return -1;
82+
}
1283
if (PyModule_AddObjectRef(m, "SimpleNamespace", (PyObject *)&_PyNamespace_Type) < 0) {
1384
return -1;
1485
}
86+
if (PyModule_AddObjectRef(m, "TracebackType", (PyObject *)&PyTraceBack_Type) < 0) {
87+
return -1;
88+
}
89+
if (PyModule_AddObjectRef(m, "UnionType", (PyObject *)&_PyUnion_Type) < 0) {
90+
return -1;
91+
}
92+
if (PyModule_AddObjectRef(m, "WrapperDescriptorType", (PyObject *)&PyWrapperDescr_Type) < 0) {
93+
return -1;
94+
}
1595
return 0;
1696
}
1797

0 commit comments

Comments
 (0)