diff --git a/Lib/types.py b/Lib/types.py
index 1484c22ee9dffa..9aa0a3b087c1c7 100644
--- a/Lib/types.py
+++ b/Lib/types.py
@@ -2,7 +2,7 @@
Define names for built-in types that aren't directly accessible as a builtin.
"""
-import sys
+import _types
# Iterators in Python aren't a matter of type but of protocol. A large
# and changing number of builtin types implement *some* flavor of
@@ -14,7 +14,7 @@ def _f(): pass
LambdaType = type(lambda: None) # Same as FunctionType
CodeType = type(_f.__code__)
MappingProxyType = type(type.__dict__)
-SimpleNamespace = type(sys.implementation)
+SimpleNamespace = _types.SimpleNamespace
def _cell_factory():
a = 1
@@ -49,7 +49,7 @@ def _m(self): pass
MethodDescriptorType = type(str.join)
ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
-ModuleType = type(sys)
+ModuleType = type(_types)
try:
raise TypeError
@@ -60,7 +60,9 @@ def _m(self): pass
GetSetDescriptorType = type(FunctionType.__code__)
MemberDescriptorType = type(FunctionType.__globals__)
-del sys, _f, _g, _C, _c, _ag, _cell_factory # Not for export
+CapsuleType = _types.CapsuleType
+
+del _types, _f, _g, _C, _c, _ag, _cell_factory # Not for export
# Provide a PEP 3115 compliant mechanism for class creation
@@ -331,11 +333,4 @@ def wrapped(*args, **kwargs):
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']
+__all__ = [n for n in globals() if not n.startswith('_')] # for pydoc
diff --git a/Modules/Setup b/Modules/Setup
index ddf39e0b966610..d050d7a5fc2dc3 100644
--- a/Modules/Setup
+++ b/Modules/Setup
@@ -150,6 +150,7 @@ PYTHONPATH=$(COREPYTHONPATH)
#_socket socketmodule.c
#_statistics _statisticsmodule.c
#_struct _struct.c
+#_types _typesmodule.c
#_typing _typingmodule.c
#_zoneinfo _zoneinfo.c
#array arraymodule.c
diff --git a/Modules/Setup.bootstrap.in b/Modules/Setup.bootstrap.in
index 4dcc0f55176d0e..2b2e8cb3e3cacd 100644
--- a/Modules/Setup.bootstrap.in
+++ b/Modules/Setup.bootstrap.in
@@ -23,6 +23,7 @@ _sre _sre/sre.c
_sysconfig _sysconfig.c
_thread _threadmodule.c
time timemodule.c
+_types _typesmodule.c
_typing _typingmodule.c
_weakref _weakref.c
diff --git a/Modules/_typesmodule.c b/Modules/_typesmodule.c
new file mode 100644
index 00000000000000..aabb35f47eefc3
--- /dev/null
+++ b/Modules/_typesmodule.c
@@ -0,0 +1,37 @@
+/* _types module */
+
+#include "Python.h"
+#include "pycore_namespace.h" // _PyNamespace_Type
+
+static int
+_types_exec(PyObject *m)
+{
+ if (PyModule_AddObjectRef(m, "CapsuleType", (PyObject *)&PyCapsule_Type) < 0) {
+ return -1;
+ }
+ if (PyModule_AddObjectRef(m, "SimpleNamespace", (PyObject *)&_PyNamespace_Type) < 0) {
+ return -1;
+ }
+ return 0;
+}
+
+static struct PyModuleDef_Slot _typesmodule_slots[] = {
+ {Py_mod_exec, _types_exec},
+ {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
+ {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+ {0, NULL}
+};
+
+static struct PyModuleDef typesmodule = {
+ .m_base = PyModuleDef_HEAD_INIT,
+ .m_name = "_types",
+ .m_doc = "Define names for built-in types.",
+ .m_size = 0,
+ .m_slots = _typesmodule_slots,
+};
+
+PyMODINIT_FUNC
+PyInit__types(void)
+{
+ return PyModuleDef_Init(&typesmodule);
+}
diff --git a/PC/config.c b/PC/config.c
index 873f93063057dc..c1a314a7454f81 100644
--- a/PC/config.c
+++ b/PC/config.c
@@ -23,6 +23,7 @@ extern PyObject* PyInit__sha2(void);
extern PyObject* PyInit__sha3(void);
extern PyObject* PyInit__statistics(void);
extern PyObject* PyInit__sysconfig(void);
+extern PyObject* PyInit__types(void);
extern PyObject* PyInit__typing(void);
extern PyObject* PyInit__blake2(void);
extern PyObject* PyInit_time(void);
@@ -107,6 +108,7 @@ struct _inittab _PyImport_Inittab[] = {
{"time", PyInit_time},
{"_thread", PyInit__thread},
{"_tokenize", PyInit__tokenize},
+ {"_types", PyInit__types},
{"_typing", PyInit__typing},
{"_statistics", PyInit__statistics},
#ifdef WIN32
diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj
index 2e639ddfc320f5..3e9dbcbedbc19c 100644
--- a/PCbuild/pythoncore.vcxproj
+++ b/PCbuild/pythoncore.vcxproj
@@ -481,6 +481,7 @@
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: