]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-109599: Expose `CapsuleType` via the `_types` module (#131969)
authorAdam Turner <9087854+AA-Turner@users.noreply.github.com>
Fri, 4 Apr 2025 22:37:41 +0000 (23:37 +0100)
committerGitHub <noreply@github.com>
Fri, 4 Apr 2025 22:37:41 +0000 (23:37 +0100)
Lib/types.py
Modules/Setup
Modules/Setup.bootstrap.in
Modules/_typesmodule.c [new file with mode: 0644]
PC/config.c
PCbuild/pythoncore.vcxproj
PCbuild/pythoncore.vcxproj.filters
Python/stdlib_module_names.h
configure
configure.ac

index 1484c22ee9dffab216d187a8a20fd61351621855..9aa0a3b087c1c7568860dca9e3de406bba778bac 100644 (file)
@@ -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 @@ FunctionType = type(_f)
 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 @@ MethodWrapperType = type(object().__str__)
 MethodDescriptorType = type(str.join)
 ClassMethodDescriptorType = type(dict.__dict__['fromkeys'])
 
-ModuleType = type(sys)
+ModuleType = type(_types)
 
 try:
     raise TypeError
@@ -60,7 +60,9 @@ except TypeError as exc:
 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 @@ EllipsisType = type(Ellipsis)
 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
index f075571ab94577fe33526df6af6e2eebd653a76b..f75f44e98c78184187fd296710fb01b0123e2e46 100644 (file)
@@ -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
index 4dcc0f55176d0e828b9fb76c9184be8af75c3837..2b2e8cb3e3cacde8300740001ffd7231ad607577 100644 (file)
@@ -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 (file)
index 0000000..aabb35f
--- /dev/null
@@ -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);
+}
index 5c6f37ccf218964b7bab99d919b6eb7a017d031e..6ce2131c7b84d00163928c9dfba89bd11b3c932c 100644 (file)
@@ -19,6 +19,7 @@ extern PyObject* PyInit__operator(void);
 extern PyObject* PyInit__signal(void);
 extern PyObject* PyInit__statistics(void);
 extern PyObject* PyInit__sysconfig(void);
+extern PyObject* PyInit__types(void);
 extern PyObject* PyInit__typing(void);
 extern PyObject* PyInit_time(void);
 extern PyObject* PyInit__thread(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},
 
index 27c50fa53dab87f743cfbc578199f7ee9881291c..ac2e7e848994a4362efd9863f99ad290f8aa59a4 100644 (file)
     <ClCompile Include="..\Modules\_sysconfig.c" />
     <ClCompile Include="..\Modules\_threadmodule.c" />
     <ClCompile Include="..\Modules\_tracemalloc.c" />
+    <ClCompile Include="..\Modules\_typesmodule.c" />
     <ClCompile Include="..\Modules\_typingmodule.c" />
     <ClCompile Include="..\Modules\timemodule.c" />
     <ClCompile Include="..\Modules\xxsubtype.c" />
index 617165b576e3620ef685a36da1f97ca149c34ede..acb4ab35dcad973409de4e063dc441f7718fdd1f 100644 (file)
     <ClCompile Include="..\Modules\_statisticsmodule.c">
       <Filter>Modules</Filter>
     </ClCompile>
+    <ClCompile Include="..\Modules\_typesmodule.c">
+      <Filter>Modules</Filter>
+    </ClCompile>
     <ClCompile Include="..\Modules\_typingmodule.c">
       <Filter>Modules</Filter>
     </ClCompile>
index 29dd5c1df0f23a1c39827c8675f597871996610f..9ae15f8cfe71bf5199ca607f18b5b60293e7b192 100644 (file)
@@ -94,6 +94,7 @@ static const char* _Py_stdlib_module_names[] = {
 "_tkinter",
 "_tokenize",
 "_tracemalloc",
+"_types",
 "_typing",
 "_uuid",
 "_warnings",
index 49de2f5db55e71de0f1c5788d44af58681dc64d0..1b75ddfa26dcddcba3d1354bb5bda5264a735dea 100755 (executable)
--- a/configure
+++ b/configure
@@ -783,6 +783,8 @@ MODULE__INTERPRETERS_FALSE
 MODULE__INTERPRETERS_TRUE
 MODULE__TYPING_FALSE
 MODULE__TYPING_TRUE
+MODULE__TYPES_FALSE
+MODULE__TYPES_TRUE
 MODULE__STRUCT_FALSE
 MODULE__STRUCT_TRUE
 MODULE_SELECT_FALSE
@@ -31008,6 +31010,28 @@ then :
 
 
 
+fi
+
+
+        if test "$py_cv_module__types" != "n/a"
+then :
+  py_cv_module__types=yes
+fi
+   if test "$py_cv_module__types" = yes; then
+  MODULE__TYPES_TRUE=
+  MODULE__TYPES_FALSE='#'
+else
+  MODULE__TYPES_TRUE='#'
+  MODULE__TYPES_FALSE=
+fi
+
+  as_fn_append MODULE_BLOCK "MODULE__TYPES_STATE=$py_cv_module__types$as_nl"
+  if test "x$py_cv_module__types" = xyes
+then :
+
+
+
+
 fi
 
 
@@ -33723,6 +33747,10 @@ if test -z "${MODULE__STRUCT_TRUE}" && test -z "${MODULE__STRUCT_FALSE}"; then
   as_fn_error $? "conditional \"MODULE__STRUCT\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
 fi
+if test -z "${MODULE__TYPES_TRUE}" && test -z "${MODULE__TYPES_FALSE}"; then
+  as_fn_error $? "conditional \"MODULE__TYPES\" was never defined.
+Usually this means the macro was only invoked conditionally." "$LINENO" 5
+fi
 if test -z "${MODULE__TYPING_TRUE}" && test -z "${MODULE__TYPING_FALSE}"; then
   as_fn_error $? "conditional \"MODULE__TYPING\" was never defined.
 Usually this means the macro was only invoked conditionally." "$LINENO" 5
index ac86a1f847e675e5f4735c28d63c05e06c5c7f30..c449bb5ebb3cd4dcc0ee83c79daf16a4a87387d0 100644 (file)
@@ -7787,6 +7787,7 @@ PY_STDLIB_MOD_SIMPLE([_queue])
 PY_STDLIB_MOD_SIMPLE([_random])
 PY_STDLIB_MOD_SIMPLE([select])
 PY_STDLIB_MOD_SIMPLE([_struct])
+PY_STDLIB_MOD_SIMPLE([_types])
 PY_STDLIB_MOD_SIMPLE([_typing])
 PY_STDLIB_MOD_SIMPLE([_interpreters])
 PY_STDLIB_MOD_SIMPLE([_interpchannels])