Skip to content

Commit

Permalink
bpo-1635741: Port resource extension module to module state (pythonGH…
Browse files Browse the repository at this point in the history
…-23462)

Signed-off-by: Christian Heimes <christian@python.org>
  • Loading branch information
tiran authored and adorilson committed Mar 11, 2021
1 parent 979a57a commit 44259ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Port :mod:`resource` extension module to module state
56 changes: 43 additions & 13 deletions Modules/resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand Down Expand Up @@ -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) { \
Expand All @@ -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;
}

Expand Down Expand Up @@ -483,8 +495,6 @@ resource_exec(PyObject *module)
Py_DECREF(v);
return -1;
}

initialized = 1;
return 0;

#undef ADD_INT
Expand All @@ -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
Expand Down

0 comments on commit 44259ce

Please sign in to comment.