@@ -34,19 +34,24 @@ static PyStructSequence_Desc struct_group_type_desc = {
34
34
};
35
35
36
36
37
- static int initialized ;
38
- static PyTypeObject StructGrpType ;
37
+ typedef struct {
38
+ PyTypeObject * StructGrpType ;
39
+ } grpmodulestate ;
40
+ #define modulestate (o ) ((grpmodulestate *)PyModule_GetState(o))
41
+ #define modulestate_global modulestate(PyState_FindModule(&grpmodule))
42
+
43
+ static struct PyModuleDef grpmodule ;
39
44
40
45
#define DEFAULT_BUFFER_SIZE 1024
41
46
42
47
static PyObject *
43
48
mkgrent (struct group * p )
44
49
{
45
50
int setIndex = 0 ;
46
- PyObject * v = PyStructSequence_New ( & StructGrpType ) , * w ;
51
+ PyObject * v , * w ;
47
52
char * * member ;
48
53
49
- if (v == NULL )
54
+ if (( v = PyStructSequence_New ( modulestate_global -> StructGrpType )) == NULL )
50
55
return NULL ;
51
56
52
57
if ((w = PyList_New (0 )) == NULL ) {
@@ -314,36 +319,52 @@ users are not explicitly listed as members of the groups they are in\n\
314
319
according to the password database. Check both databases to get\n\
315
320
complete membership information.)" );
316
321
322
+ static int grpmodule_traverse (PyObject * m , visitproc visit , void * arg ) {
323
+ Py_VISIT (modulestate (m )-> StructGrpType );
324
+ return 0 ;
325
+ }
317
326
327
+ static int grpmodule_clear (PyObject * m ) {
328
+ Py_CLEAR (modulestate (m )-> StructGrpType );
329
+ return 0 ;
330
+ }
331
+
332
+ static void grpmodule_free (void * m ) {
333
+ grpmodule_clear ((PyObject * )m );
334
+ }
318
335
319
336
static struct PyModuleDef grpmodule = {
320
337
PyModuleDef_HEAD_INIT ,
321
338
"grp" ,
322
339
grp__doc__ ,
323
- -1 ,
340
+ sizeof ( grpmodulestate ) ,
324
341
grp_methods ,
325
342
NULL ,
326
- NULL ,
327
- NULL ,
328
- NULL
343
+ grpmodule_traverse ,
344
+ grpmodule_clear ,
345
+ grpmodule_free ,
329
346
};
330
347
331
348
PyMODINIT_FUNC
332
349
PyInit_grp (void )
333
350
{
334
- PyObject * m , * d ;
335
- m = PyModule_Create (& grpmodule );
336
- if (m == NULL )
351
+ PyObject * m ;
352
+ if ((m = PyState_FindModule (& grpmodule )) != NULL ) {
353
+ Py_INCREF (m );
354
+ return m ;
355
+ }
356
+
357
+ if ((m = PyModule_Create (& grpmodule )) == NULL ) {
337
358
return NULL ;
338
- d = PyModule_GetDict (m );
339
- if (!initialized ) {
340
- if (PyStructSequence_InitType2 (& StructGrpType ,
341
- & struct_group_type_desc ) < 0 )
342
- return NULL ;
343
359
}
344
- if (PyDict_SetItemString (d , "struct_group" ,
345
- (PyObject * )& StructGrpType ) < 0 )
360
+
361
+ grpmodulestate * state = PyModule_GetState (m );
362
+ state -> StructGrpType = PyStructSequence_NewType (& struct_group_type_desc );
363
+ if (state -> StructGrpType == NULL ) {
346
364
return NULL ;
347
- initialized = 1 ;
365
+ }
366
+
367
+ Py_INCREF (state -> StructGrpType );
368
+ PyModule_AddObject (m , "struct_group" , (PyObject * ) state -> StructGrpType );
348
369
return m ;
349
370
}
0 commit comments