diff -r 116fb94e5dae Modules/grpmodule.c --- a/Modules/grpmodule.c Thu Aug 16 23:50:09 2012 +0200 +++ b/Modules/grpmodule.c Thu Aug 16 23:55:45 2012 +0200 @@ -27,19 +27,50 @@ 4, }; +typedef struct { + int initialized; + PyObject *StructGrpType; +} grpstate; -static int initialized; -static PyTypeObject StructGrpType; + +#define grp_state(o) ((grpstate *)PyModule_GetState(o)) + +static int +grp_clear(PyObject *m) +{ + Py_CLEAR(grp_state(m)->StructGrpType); + return 0; +} + +static int +grp_traverse(PyObject *m, visitproc visit, void *arg) +{ + Py_VISIT(grp_state(m)->StructGrpType); + return 0; +} + +static void +grp_free(void *m) +{ + grp_clear((PyObject *)m); +} + +static PyModuleDef grpmodule; + +#define grpstate_global ((grpstate *)PyModule_GetState(PyState_FindModule(&grpmodule))) + static PyObject * mkgrent(struct group *p) { int setIndex = 0; - PyObject *v = PyStructSequence_New(&StructGrpType), *w; + PyObject *v = PyStructSequence_New( + (PyTypeObject *)grpstate_global->StructGrpType); + PyObject *w; char **member; - if (v == NULL) return NULL; + Py_INCREF(grpstate_global->StructGrpType); if ((w = PyList_New(0)) == NULL) { Py_DECREF(v); @@ -77,7 +108,6 @@ Py_DECREF(v); return NULL; } - return v; } @@ -187,25 +217,33 @@ PyModuleDef_HEAD_INIT, "grp", grp__doc__, - -1, + sizeof(grpstate), grp_methods, NULL, - NULL, - NULL, - NULL + grp_traverse, + grp_clear, + grp_free }; PyMODINIT_FUNC PyInit_grp(void) { PyObject *m, *d; - m = PyModule_Create(&grpmodule); - if (m == NULL) - return NULL; + m = PyState_FindModule(&grpmodule); + if (!m) { + m = PyModule_Create(&grpmodule); + if (m == NULL) + return NULL; + grp_state(m)->initialized = 0; + } else { + Py_INCREF(m); + } d = PyModule_GetDict(m); - if (!initialized) - PyStructSequence_InitType(&StructGrpType, &struct_group_type_desc); - PyDict_SetItemString(d, "struct_group", (PyObject *) &StructGrpType); - initialized = 1; + if(!grp_state(m)->initialized) { + grp_state(m)->StructGrpType = + (PyObject *)PyStructSequence_NewType(&struct_group_type_desc); + } + PyDict_SetItemString(d, "struct_group", grp_state(m)->StructGrpType); + grp_state(m)->initialized = 1; return m; } diff -r 116fb94e5dae Objects/structseq.c --- a/Objects/structseq.c Thu Aug 16 23:50:09 2012 +0200 +++ b/Objects/structseq.c Thu Aug 16 23:55:45 2012 +0200 @@ -36,6 +36,9 @@ return NULL; /* Hack the size of the variable object, so invisible fields don't appear to Python code. */ + if(type->tp_flags & Py_TPFLAGS_HEAPTYPE) + Py_INCREF(type); + Py_SIZE(obj) = VISIBLE_SIZE_TP(type); for (i = 0; i < size; i++) obj->ob_item[i] = NULL;