diff -r 0e506a370ebb Modules/resource.c --- a/Modules/resource.c Thu Aug 16 22:00:32 2012 +0200 +++ b/Modules/resource.c Thu Aug 16 22:05:15 2012 +0200 @@ -52,8 +52,38 @@ 16 /* n_in_sequence */ }; -static int initialized; -static PyTypeObject StructRUsageType; +typedef struct { + int initialized; + PyObject *StructRUsageType; +} resourcestate; + + +#define resource_state(o) ((resourcestate *)PyModule_GetState(o)) + +static int +resource_clear(PyObject *m) +{ + Py_CLEAR(resource_state(m)->StructRUsageType); + return 0; +} + +static int +resource_traverse(PyObject *m, visitproc visit, void *arg) +{ + Py_VISIT(resource_state(m)->StructRUsageType); + return 0; +} + +static void +resource_free(void *m) +{ + resource_clear((PyObject *)m); +} + +static PyModuleDef resourcemodule; + +#define resourcestate_global ((resourcestate *)PyModule_GetState(PyState_FindModule(&resourcemodule))) + static PyObject * resource_getrusage(PyObject *self, PyObject *args) @@ -75,7 +105,7 @@ return NULL; } - result = PyStructSequence_New(&StructRUsageType); + result = PyStructSequence_New((PyTypeObject *)resourcestate_global->StructRUsageType); if (!result) return NULL; @@ -225,12 +255,12 @@ PyModuleDef_HEAD_INIT, "resource", NULL, - -1, + sizeof(resourcestate), resource_methods, NULL, - NULL, - NULL, - NULL + resource_traverse, + resource_clear, + resource_free }; PyMODINIT_FUNC @@ -238,20 +268,24 @@ { PyObject *m, *v; - /* Create the module and add the functions */ - m = PyModule_Create(&resourcemodule); - if (m == NULL) - return NULL; + m = PyState_FindModule(&resourcemodule); + if(m == NULL) { + /* Create the module and add the functions */ + m = PyModule_Create(&resourcemodule); + if (m == NULL) + return NULL; + resource_state(m)->initialized = 0; + } /* Add some symbolic constants to the module */ Py_INCREF(PyExc_OSError); PyModule_AddObject(m, "error", PyExc_OSError); - if (!initialized) - PyStructSequence_InitType(&StructRUsageType, - &struct_rusage_desc); - Py_INCREF(&StructRUsageType); + if (!resource_state(m)->initialized) + resource_state(m)->StructRUsageType = + (PyObject *)PyStructSequence_NewType(&struct_rusage_desc); + Py_INCREF(resource_state(m)->StructRUsageType); PyModule_AddObject(m, "struct_rusage", - (PyObject*) &StructRUsageType); + resource_state(m)->StructRUsageType); /* insert constants */ #ifdef RLIMIT_CPU @@ -333,6 +367,6 @@ if (v) { PyModule_AddObject(m, "RLIM_INFINITY", v); } - initialized = 1; + resource_state(m)->initialized = 1; return m; }