https://github.com/python/cpython/commit/112560ee34a70c02baebdc11519d1f77e90ecab8
commit: 112560ee34a70c02baebdc11519d1f77e90ecab8
branch: main
author: Petr Viktorin <[email protected]>
committer: encukou <[email protected]>
date: 2026-09-04T11:32:15+02:00
summary:

gh-155561: Use multi-phase init for Modules/_testlimitedcapi.c (GH-156052)



Co-authored-by: Victor Stinner <[email protected]>

files:
M Modules/_testlimitedcapi.c

diff --git a/Modules/_testlimitedcapi.c b/Modules/_testlimitedcapi.c
index de8bed77d7d327..1f7392dd430d4c 100644
--- a/Modules/_testlimitedcapi.c
+++ b/Modules/_testlimitedcapi.c
@@ -2,122 +2,113 @@
  * Test the limited C API.
  *
  * The 'test_*' functions exported by this module are run as part of the
- * standard Python regression test, via Lib/test/test_capi.py.
+ * standard Python regression test, via Lib/test/test_capi/test_misc.py.
  */
 
-#include "pyconfig.h"   // Py_GIL_DISABLED
-
-#ifdef Py_GIL_DISABLED
-   // Cannot test the limited C API
-#else
-   // Use the oldest limited C API version
-#  define Py_LIMITED_API 0x03020000
-#endif
-
 #include "_testlimitedcapi/parts.h"
 
-static PyMethodDef TestMethods[] = {
-    {NULL, NULL} /* sentinel */
-};
-
-static struct PyModuleDef _testlimitedcapimodule = {
-    PyModuleDef_HEAD_INIT,
-    .m_name = "_testlimitedcapi",
-    .m_size = 0,
-    .m_methods = TestMethods,
-};
-
-PyMODINIT_FUNC
-PyInit__testlimitedcapi(void)
+static int
+module_exec(PyObject *mod)
 {
-    PyObject *mod = PyModule_Create(&_testlimitedcapimodule);
-    if (mod == NULL) {
-        return NULL;
-    }
-#ifdef Py_GIL_DISABLED
-    PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
-#endif
-
     if (_PyTestLimitedCAPI_Init_Abstract(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_ByteArray(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Bytes(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Capsule(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Codec(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Complex(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Dict(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Eval(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Float(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_HeaptypeRelative(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Import(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_List(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Long(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Object(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_PyOS(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Set(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Slots(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Sys(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_ThreadState(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Tuple(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Unicode(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_VectorcallLimited(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Version(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_File(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Weakref(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Run(mod) < 0) {
-        return NULL;
+        return -1;
     }
     if (_PyTestLimitedCAPI_Init_Type(mod) < 0) {
-        return NULL;
+        return -1;
     }
-    return mod;
+    return 0;
+}
+
+static struct PyModuleDef _testlimitedcapimodule_def = {
+    PyModuleDef_HEAD_INIT,
+    .m_name = "_testlimitedcapi",
+    .m_size = 0,
+    .m_slots = (PyModuleDef_Slot[]){
+        {Py_mod_exec, module_exec},
+#ifdef Py_GIL_DISABLED
+        {Py_mod_gil, Py_MOD_GIL_NOT_USED},
+#endif
+        {0}
+    }
+};
+
+PyMODINIT_FUNC
+PyInit__testlimitedcapi(void)
+{
+    return PyModuleDef_Init(&_testlimitedcapimodule_def);
 }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to