bpo-1635741: Port resource extension module to module state (GH-23462)

Signed-off-by: Christian Heimes <christian@python.org>
This commit is contained in:
Christian Heimes 2020-12-23 08:55:11 +01:00 committed by GitHub
parent cf3565ca9a
commit 6d9ec8bbfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 13 deletions

View File

@ -0,0 +1 @@
Port :mod:`resource` extension module to module state

View File

@ -63,8 +63,20 @@ static PyStructSequence_Desc struct_rusage_desc = {
16 /* n_in_sequence */ 16 /* n_in_sequence */
}; };
static int initialized; typedef struct {
static PyTypeObject StructRUsageType; PyTypeObject *StructRUsageType;
} resourcemodulestate;
static inline resourcemodulestate*
get_resource_state(PyObject *module)
{
void *state = PyModule_GetState(module);
assert(state != NULL);
return (resourcemodulestate *)state;
}
static struct PyModuleDef resourcemodule;
/*[clinic input] /*[clinic input]
resource.getrusage resource.getrusage
@ -91,7 +103,8 @@ resource_getrusage_impl(PyObject *module, int who)
return NULL; return NULL;
} }
result = PyStructSequence_New(&StructRUsageType); result = PyStructSequence_New(
get_resource_state(module)->StructRUsageType);
if (!result) if (!result)
return NULL; return NULL;
@ -336,10 +349,10 @@ resource_methods[] = {
/* Module initialization */ /* Module initialization */
static int static int
resource_exec(PyObject *module) resource_exec(PyObject *module)
{ {
resourcemodulestate *state = get_resource_state(module);
#define ADD_INT(module, value) \ #define ADD_INT(module, value) \
do { \ do { \
if (PyModule_AddIntConstant(module, #value, value) < 0) { \ if (PyModule_AddIntConstant(module, #value, value) < 0) { \
@ -353,13 +366,12 @@ resource_exec(PyObject *module)
Py_DECREF(PyExc_OSError); Py_DECREF(PyExc_OSError);
return -1; return -1;
} }
if (!initialized) {
if (PyStructSequence_InitType2(&StructRUsageType,
&struct_rusage_desc) < 0)
return -1;
}
if(PyModule_AddType(module, &StructRUsageType) < 0) { state->StructRUsageType = PyStructSequence_NewType(&struct_rusage_desc);
if (state->StructRUsageType == NULL) {
return -1;
}
if (PyModule_AddType(module, state->StructRUsageType) < 0) {
return -1; return -1;
} }
@ -483,8 +495,6 @@ resource_exec(PyObject *module)
Py_DECREF(v); Py_DECREF(v);
return -1; return -1;
} }
initialized = 1;
return 0; return 0;
#undef ADD_INT #undef ADD_INT
@ -495,12 +505,32 @@ static struct PyModuleDef_Slot resource_slots[] = {
{0, NULL} {0, NULL}
}; };
static int
resourcemodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(get_resource_state(m)->StructRUsageType);
return 0;
}
static int
resourcemodule_clear(PyObject *m) {
Py_CLEAR(get_resource_state(m)->StructRUsageType);
return 0;
}
static void
resourcemodule_free(void *m) {
resourcemodule_clear((PyObject *)m);
}
static struct PyModuleDef resourcemodule = { static struct PyModuleDef resourcemodule = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
.m_name = "resource", .m_name = "resource",
.m_size = 0, .m_size = sizeof(resourcemodulestate),
.m_methods = resource_methods, .m_methods = resource_methods,
.m_slots = resource_slots, .m_slots = resource_slots,
.m_traverse = resourcemodule_traverse,
.m_clear = resourcemodule_clear,
.m_free = resourcemodule_free,
}; };
PyMODINIT_FUNC PyMODINIT_FUNC