Skip to content

Commit 112560e

Browse files
encukouvstinner
andauthored
gh-155561: Use multi-phase init for Modules/_testlimitedcapi.c (GH-156052)
Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent 5f7d709 commit 112560e

1 file changed

Lines changed: 50 additions & 59 deletions

File tree

Modules/_testlimitedcapi.c

Lines changed: 50 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,122 +2,113 @@
22
* Test the limited C API.
33
*
44
* The 'test_*' functions exported by this module are run as part of the
5-
* standard Python regression test, via Lib/test/test_capi.py.
5+
* standard Python regression test, via Lib/test/test_capi/test_misc.py.
66
*/
77

8-
#include "pyconfig.h" // Py_GIL_DISABLED
9-
10-
#ifdef Py_GIL_DISABLED
11-
// Cannot test the limited C API
12-
#else
13-
// Use the oldest limited C API version
14-
# define Py_LIMITED_API 0x03020000
15-
#endif
16-
178
#include "_testlimitedcapi/parts.h"
189

19-
static PyMethodDef TestMethods[] = {
20-
{NULL, NULL} /* sentinel */
21-
};
22-
23-
static struct PyModuleDef _testlimitedcapimodule = {
24-
PyModuleDef_HEAD_INIT,
25-
.m_name = "_testlimitedcapi",
26-
.m_size = 0,
27-
.m_methods = TestMethods,
28-
};
29-
30-
PyMODINIT_FUNC
31-
PyInit__testlimitedcapi(void)
10+
static int
11+
module_exec(PyObject *mod)
3212
{
33-
PyObject *mod = PyModule_Create(&_testlimitedcapimodule);
34-
if (mod == NULL) {
35-
return NULL;
36-
}
37-
#ifdef Py_GIL_DISABLED
38-
PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
39-
#endif
40-
4113
if (_PyTestLimitedCAPI_Init_Abstract(mod) < 0) {
42-
return NULL;
14+
return -1;
4315
}
4416
if (_PyTestLimitedCAPI_Init_ByteArray(mod) < 0) {
45-
return NULL;
17+
return -1;
4618
}
4719
if (_PyTestLimitedCAPI_Init_Bytes(mod) < 0) {
48-
return NULL;
20+
return -1;
4921
}
5022
if (_PyTestLimitedCAPI_Init_Capsule(mod) < 0) {
51-
return NULL;
23+
return -1;
5224
}
5325
if (_PyTestLimitedCAPI_Init_Codec(mod) < 0) {
54-
return NULL;
26+
return -1;
5527
}
5628
if (_PyTestLimitedCAPI_Init_Complex(mod) < 0) {
57-
return NULL;
29+
return -1;
5830
}
5931
if (_PyTestLimitedCAPI_Init_Dict(mod) < 0) {
60-
return NULL;
32+
return -1;
6133
}
6234
if (_PyTestLimitedCAPI_Init_Eval(mod) < 0) {
63-
return NULL;
35+
return -1;
6436
}
6537
if (_PyTestLimitedCAPI_Init_Float(mod) < 0) {
66-
return NULL;
38+
return -1;
6739
}
6840
if (_PyTestLimitedCAPI_Init_HeaptypeRelative(mod) < 0) {
69-
return NULL;
41+
return -1;
7042
}
7143
if (_PyTestLimitedCAPI_Init_Import(mod) < 0) {
72-
return NULL;
44+
return -1;
7345
}
7446
if (_PyTestLimitedCAPI_Init_List(mod) < 0) {
75-
return NULL;
47+
return -1;
7648
}
7749
if (_PyTestLimitedCAPI_Init_Long(mod) < 0) {
78-
return NULL;
50+
return -1;
7951
}
8052
if (_PyTestLimitedCAPI_Init_Object(mod) < 0) {
81-
return NULL;
53+
return -1;
8254
}
8355
if (_PyTestLimitedCAPI_Init_PyOS(mod) < 0) {
84-
return NULL;
56+
return -1;
8557
}
8658
if (_PyTestLimitedCAPI_Init_Set(mod) < 0) {
87-
return NULL;
59+
return -1;
8860
}
8961
if (_PyTestLimitedCAPI_Init_Slots(mod) < 0) {
90-
return NULL;
62+
return -1;
9163
}
9264
if (_PyTestLimitedCAPI_Init_Sys(mod) < 0) {
93-
return NULL;
65+
return -1;
9466
}
9567
if (_PyTestLimitedCAPI_Init_ThreadState(mod) < 0) {
96-
return NULL;
68+
return -1;
9769
}
9870
if (_PyTestLimitedCAPI_Init_Tuple(mod) < 0) {
99-
return NULL;
71+
return -1;
10072
}
10173
if (_PyTestLimitedCAPI_Init_Unicode(mod) < 0) {
102-
return NULL;
74+
return -1;
10375
}
10476
if (_PyTestLimitedCAPI_Init_VectorcallLimited(mod) < 0) {
105-
return NULL;
77+
return -1;
10678
}
10779
if (_PyTestLimitedCAPI_Init_Version(mod) < 0) {
108-
return NULL;
80+
return -1;
10981
}
11082
if (_PyTestLimitedCAPI_Init_File(mod) < 0) {
111-
return NULL;
83+
return -1;
11284
}
11385
if (_PyTestLimitedCAPI_Init_Weakref(mod) < 0) {
114-
return NULL;
86+
return -1;
11587
}
11688
if (_PyTestLimitedCAPI_Init_Run(mod) < 0) {
117-
return NULL;
89+
return -1;
11890
}
11991
if (_PyTestLimitedCAPI_Init_Type(mod) < 0) {
120-
return NULL;
92+
return -1;
12193
}
122-
return mod;
94+
return 0;
95+
}
96+
97+
static struct PyModuleDef _testlimitedcapimodule_def = {
98+
PyModuleDef_HEAD_INIT,
99+
.m_name = "_testlimitedcapi",
100+
.m_size = 0,
101+
.m_slots = (PyModuleDef_Slot[]){
102+
{Py_mod_exec, module_exec},
103+
#ifdef Py_GIL_DISABLED
104+
{Py_mod_gil, Py_MOD_GIL_NOT_USED},
105+
#endif
106+
{0}
107+
}
108+
};
109+
110+
PyMODINIT_FUNC
111+
PyInit__testlimitedcapi(void)
112+
{
113+
return PyModuleDef_Init(&_testlimitedcapimodule_def);
123114
}

0 commit comments

Comments
 (0)