Skip to content

Commit 058e0db

Browse files
committed
test_cext: Remove the 'create_moduledef.c' workaround
1 parent 391281d commit 058e0db

File tree

4 files changed

+10
-73
lines changed

4 files changed

+10
-73
lines changed

Lib/test/test_cext/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
SOURCES = [
1616
os.path.join(os.path.dirname(__file__), 'extension.c'),
17-
os.path.join(os.path.dirname(__file__), 'create_moduledef.c'),
1817
]
1918
SETUP = os.path.join(os.path.dirname(__file__), 'setup.py')
2019

Lib/test/test_cext/create_moduledef.c

Lines changed: 0 additions & 29 deletions
This file was deleted.

Lib/test/test_cext/extension.c

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ _testcext_exec(
7878
return 0;
7979
}
8080

81-
#define _FUNC_NAME(NAME) PyInit_ ## NAME
81+
#define _FUNC_NAME(NAME) PyModExport_ ## NAME
8282
#define FUNC_NAME(NAME) _FUNC_NAME(NAME)
8383

8484
// Converting from function pointer to void* has undefined behavior, but
@@ -88,58 +88,26 @@ _testcext_exec(
8888
_Py_COMP_DIAG_PUSH
8989
#if defined(__GNUC__)
9090
#pragma GCC diagnostic ignored "-Wpedantic"
91+
#pragma GCC diagnostic ignored "-Wcast-qual"
9192
#elif defined(__clang__)
9293
#pragma clang diagnostic ignored "-Wpedantic"
94+
#pragma clang diagnostic ignored "-Wcast-qual"
9395
#endif
9496

97+
PyDoc_STRVAR(_testcext_doc, "C test extension.");
98+
9599
static PyModuleDef_Slot _testcext_slots[] = {
100+
{Py_mod_name, STR(MODULE_NAME)},
101+
{Py_mod_doc, (void*)(char*)_testcext_doc},
96102
{Py_mod_exec, (void*)_testcext_exec},
103+
{Py_mod_methods, _testcext_methods},
97104
{0, NULL}
98105
};
99106

100107
_Py_COMP_DIAG_POP
101108

102-
PyDoc_STRVAR(_testcext_doc, "C test extension.");
103-
104-
#ifndef _Py_OPAQUE_PYOBJECT
105-
106-
static struct PyModuleDef _testcext_module = {
107-
PyModuleDef_HEAD_INIT, // m_base
108-
STR(MODULE_NAME), // m_name
109-
_testcext_doc, // m_doc
110-
0, // m_size
111-
_testcext_methods, // m_methods
112-
_testcext_slots, // m_slots
113-
NULL, // m_traverse
114-
NULL, // m_clear
115-
NULL, // m_free
116-
};
117-
118-
119-
PyMODINIT_FUNC
120-
FUNC_NAME(MODULE_NAME)(void)
121-
{
122-
return PyModuleDef_Init(&_testcext_module);
123-
}
124-
125-
#else // _Py_OPAQUE_PYOBJECT
126-
127-
// Opaque PyObject means that PyModuleDef is also opaque and cannot be
128-
// declared statically. See PEP 793.
129-
// So, this part of module creation is split into a separate source file
130-
// which uses non-limited API.
131-
132-
// (repeated definition to avoid creating a header)
133-
extern PyObject *testcext_create_moduledef(
134-
const char *name, const char *doc,
135-
PyMethodDef *methods, PyModuleDef_Slot *slots);
136-
137-
138-
PyMODINIT_FUNC
109+
PyMODEXPORT_FUNC
139110
FUNC_NAME(MODULE_NAME)(void)
140111
{
141-
return testcext_create_moduledef(
142-
STR(MODULE_NAME), _testcext_doc, _testcext_methods, _testcext_slots);
112+
return _testcext_slots;
143113
}
144-
145-
#endif // _Py_OPAQUE_PYOBJECT

Lib/test/test_cext/setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ def main():
9999
# Define _Py_OPAQUE_PYOBJECT macro
100100
if opaque_pyobject:
101101
cflags.append(f'-D_Py_OPAQUE_PYOBJECT')
102-
sources.append('create_moduledef.c')
103102

104103
if internal:
105104
cflags.append('-DTEST_INTERNAL_C_API=1')

0 commit comments

Comments
 (0)