Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 231a50f

Browse filesBrowse files
authored
gh-109599: Expose CapsuleType via the _types module (#131969)
1 parent 7473c60 commit 231a50f
Copy full SHA for 231a50f

10 files changed

+82
-12
lines changed

‎Lib/types.py

Copy file name to clipboardExpand all lines: Lib/types.py
+7-12Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Define names for built-in types that aren't directly accessible as a builtin.
33
"""
44

5-
import sys
5+
import _types
66

77
# Iterators in Python aren't a matter of type but of protocol. A large
88
# and changing number of builtin types implement *some* flavor of
@@ -14,7 +14,7 @@ def _f(): pass
1414
LambdaType = type(lambda: None) # Same as FunctionType
1515
CodeType = type(_f.__code__)
1616
MappingProxyType = type(type.__dict__)
17-
SimpleNamespace = type(sys.implementation)
17+
SimpleNamespace = _types.SimpleNamespace
1818

1919
def _cell_factory():
2020
a = 1
@@ -49,7 +49,7 @@ def _m(self): pass
4949
MethodDescriptorType = type(str.join)
5050
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
5151

52-
ModuleType = type(sys)
52+
ModuleType = type(_types)
5353

5454
try:
5555
raise TypeError
@@ -60,7 +60,9 @@ def _m(self): pass
6060
GetSetDescriptorType = type(FunctionType.__code__)
6161
MemberDescriptorType = type(FunctionType.__globals__)
6262

63-
del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export
63+
CapsuleType = _types.CapsuleType
64+
65+
del _types, _f, _g, _C, _c, _ag, _cell_factory # Not for export
6466

6567

6668
# Provide a PEP 3115 compliant mechanism for class creation
@@ -331,11 +333,4 @@ def wrapped(*args, **kwargs):
331333
NoneType = type(None)
332334
NotImplementedType = type(NotImplemented)
333335

334-
def __getattr__(name):
335-
if name == 'CapsuleType':
336-
import _socket
337-
return type(_socket.CAPI)
338-
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
339-
340-
__all__ = [n for n in globals() if n[:1] != '_']
341-
__all__ += ['CapsuleType']
336+
__all__ = [n for n in globals() if not n.startswith('_')] # for pydoc

‎Modules/Setup

Copy file name to clipboardExpand all lines: Modules/Setup
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ PYTHONPATH=$(COREPYTHONPATH)
150150
#_socket socketmodule.c
151151
#_statistics _statisticsmodule.c
152152
#_struct _struct.c
153+
#_types _typesmodule.c
153154
#_typing _typingmodule.c
154155
#_zoneinfo _zoneinfo.c
155156
#array arraymodule.c

‎Modules/Setup.bootstrap.in

Copy file name to clipboardExpand all lines: Modules/Setup.bootstrap.in
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ _sre _sre/sre.c
2323
_sysconfig _sysconfig.c
2424
_thread _threadmodule.c
2525
time timemodule.c
26+
_types _typesmodule.c
2627
_typing _typingmodule.c
2728
_weakref _weakref.c
2829

‎Modules/_typesmodule.c

Copy file name to clipboard
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* _types module */
2+
3+
#include "Python.h"
4+
#include "pycore_namespace.h" // _PyNamespace_Type
5+
6+
static int
7+
_types_exec(PyObject *m)
8+
{
9+
if (PyModule_AddObjectRef(m, "CapsuleType", (PyObject *)&PyCapsule_Type) < 0) {
10+
return -1;
11+
}
12+
if (PyModule_AddObjectRef(m, "SimpleNamespace", (PyObject *)&_PyNamespace_Type) < 0) {
13+
return -1;
14+
}
15+
return 0;
16+
}
17+
18+
static struct PyModuleDef_Slot _typesmodule_slots[] = {
19+
{Py_mod_exec, _types_exec},
20+
{Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
21+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
22+
{0, NULL}
23+
};
24+
25+
static struct PyModuleDef typesmodule = {
26+
.m_base = PyModuleDef_HEAD_INIT,
27+
.m_name = "_types",
28+
.m_doc = "Define names for built-in types.",
29+
.m_size = 0,
30+
.m_slots = _typesmodule_slots,
31+
};
32+
33+
PyMODINIT_FUNC
34+
PyInit__types(void)
35+
{
36+
return PyModuleDef_Init(&typesmodule);
37+
}

‎PC/config.c

Copy file name to clipboardExpand all lines: PC/config.c
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ extern PyObject* PyInit__operator(void);
1919
extern PyObject* PyInit__signal(void);
2020
extern PyObject* PyInit__statistics(void);
2121
extern PyObject* PyInit__sysconfig(void);
22+
extern PyObject* PyInit__types(void);
2223
extern PyObject* PyInit__typing(void);
2324
extern PyObject* PyInit_time(void);
2425
extern PyObject* PyInit__thread(void);
@@ -107,6 +108,7 @@ struct _inittab _PyImport_Inittab[] = {
107108
{"time", PyInit_time},
108109
{"_thread", PyInit__thread},
109110
{"_tokenize", PyInit__tokenize},
111+
{"_types", PyInit__types},
110112
{"_typing", PyInit__typing},
111113
{"_statistics", PyInit__statistics},
112114

‎PCbuild/pythoncore.vcxproj

Copy file name to clipboardExpand all lines: PCbuild/pythoncore.vcxproj
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@
485485
<ClCompile Include="..\Modules\_sysconfig.c" />
486486
<ClCompile Include="..\Modules\_threadmodule.c" />
487487
<ClCompile Include="..\Modules\_tracemalloc.c" />
488+
<ClCompile Include="..\Modules\_typesmodule.c" />
488489
<ClCompile Include="..\Modules\_typingmodule.c" />
489490
<ClCompile Include="..\Modules\timemodule.c" />
490491
<ClCompile Include="..\Modules\xxsubtype.c" />

‎PCbuild/pythoncore.vcxproj.filters

Copy file name to clipboardExpand all lines: PCbuild/pythoncore.vcxproj.filters
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,9 @@
998998
<ClCompile Include="..\Modules\_statisticsmodule.c">
999999
<Filter>Modules</Filter>
10001000
</ClCompile>
1001+
<ClCompile Include="..\Modules\_typesmodule.c">
1002+
<Filter>Modules</Filter>
1003+
</ClCompile>
10011004
<ClCompile Include="..\Modules\_typingmodule.c">
10021005
<Filter>Modules</Filter>
10031006
</ClCompile>

‎Python/stdlib_module_names.h

Copy file name to clipboardExpand all lines: Python/stdlib_module_names.h
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure

Copy file name to clipboardExpand all lines: configure
+28Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎configure.ac

Copy file name to clipboardExpand all lines: configure.ac
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7787,6 +7787,7 @@ PY_STDLIB_MOD_SIMPLE([_queue])
77877787
PY_STDLIB_MOD_SIMPLE([_random])
77887788
PY_STDLIB_MOD_SIMPLE([select])
77897789
PY_STDLIB_MOD_SIMPLE([_struct])
7790+
PY_STDLIB_MOD_SIMPLE([_types])
77907791
PY_STDLIB_MOD_SIMPLE([_typing])
77917792
PY_STDLIB_MOD_SIMPLE([_interpreters])
77927793
PY_STDLIB_MOD_SIMPLE([_interpchannels])

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.