summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorChristian Heimes <christian@python.org>2020-12-23 07:55:11 (GMT)
committerGitHub <noreply@github.com>2020-12-23 07:55:11 (GMT)
commit6d9ec8bbfa07161431dc6190dd0772a6fbaf7ebd (patch)
tree384fb440406f0cba1635063ecf0b2a3f06ae99b8 /Modules
parentcf3565ca9a7ed0f7decd000e41fa3de400986e4d (diff)
downloadcpython-6d9ec8bbfa07161431dc6190dd0772a6fbaf7ebd.zip
cpython-6d9ec8bbfa07161431dc6190dd0772a6fbaf7ebd.tar.gz
cpython-6d9ec8bbfa07161431dc6190dd0772a6fbaf7ebd.tar.bz2
bpo-1635741: Port resource extension module to module state (GH-23462)
Signed-off-by: Christian Heimes <christian@python.org>
Diffstat (limited to 'Modules')
-rw-r--r--Modules/resource.c56
1 files changed, 43 insertions, 13 deletions
diff --git a/Modules/resource.c b/Modules/resource.c
index 4f5dcf8..f10a80f 100644
--- a/Modules/resource.c
+++ b/Modules/resource.c
@@ -63,8 +63,20 @@ static PyStructSequence_Desc struct_rusage_desc = {
16 /* n_in_sequence */
};
-static int initialized;
-static PyTypeObject StructRUsageType;
+typedef struct {
+ 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]
resource.getrusage
@@ -91,7 +103,8 @@ resource_getrusage_impl(PyObject *module, int who)
return NULL;
}
- result = PyStructSequence_New(&StructRUsageType);
+ result = PyStructSequence_New(
+ get_resource_state(module)->StructRUsageType);
if (!result)
return NULL;
@@ -336,10 +349,10 @@ resource_methods[] = {
/* Module initialization */
-
static int
resource_exec(PyObject *module)
{
+ resourcemodulestate *state = get_resource_state(module);
#define ADD_INT(module, value) \
do { \
if (PyModule_AddIntConstant(module, #value, value) < 0) { \
@@ -353,13 +366,12 @@ resource_exec(PyObject *module)
Py_DECREF(PyExc_OSError);
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;
}
@@ -483,8 +495,6 @@ resource_exec(PyObject *module)
Py_DECREF(v);
return -1;
}
-
- initialized = 1;
return 0;
#undef ADD_INT
@@ -495,12 +505,32 @@ static struct PyModuleDef_Slot resource_slots[] = {
{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 = {
PyModuleDef_HEAD_INIT,
.m_name = "resource",
- .m_size = 0,
+ .m_size = sizeof(resourcemodulestate),
.m_methods = resource_methods,
.m_slots = resource_slots,
+ .m_traverse = resourcemodule_traverse,
+ .m_clear = resourcemodule_clear,
+ .m_free = resourcemodule_free,
};
PyMODINIT_FUNC