Skip to content

Commit 40a5313

Browse files
DinoVYhg1s
authored andcommitted
bpo-38072: PEP-384 grpmodule (GH-15788)
Make the grp module PEP-384 compliant.
1 parent 9e61066 commit 40a5313

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
grp module made PEP-384 compatible

Modules/grpmodule.c

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,24 @@ static PyStructSequence_Desc struct_group_type_desc = {
3434
};
3535

3636

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;
3944

4045
#define DEFAULT_BUFFER_SIZE 1024
4146

4247
static PyObject *
4348
mkgrent(struct group *p)
4449
{
4550
int setIndex = 0;
46-
PyObject *v = PyStructSequence_New(&StructGrpType), *w;
51+
PyObject *v, *w;
4752
char **member;
4853

49-
if (v == NULL)
54+
if ((v = PyStructSequence_New(modulestate_global->StructGrpType)) == NULL)
5055
return NULL;
5156

5257
if ((w = PyList_New(0)) == NULL) {
@@ -314,36 +319,52 @@ users are not explicitly listed as members of the groups they are in\n\
314319
according to the password database. Check both databases to get\n\
315320
complete membership information.)");
316321

322+
static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) {
323+
Py_VISIT(modulestate(m)->StructGrpType);
324+
return 0;
325+
}
317326

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+
}
318335

319336
static struct PyModuleDef grpmodule = {
320337
PyModuleDef_HEAD_INIT,
321338
"grp",
322339
grp__doc__,
323-
-1,
340+
sizeof(grpmodulestate),
324341
grp_methods,
325342
NULL,
326-
NULL,
327-
NULL,
328-
NULL
343+
grpmodule_traverse,
344+
grpmodule_clear,
345+
grpmodule_free,
329346
};
330347

331348
PyMODINIT_FUNC
332349
PyInit_grp(void)
333350
{
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) {
337358
return NULL;
338-
d = PyModule_GetDict(m);
339-
if (!initialized) {
340-
if (PyStructSequence_InitType2(&StructGrpType,
341-
&struct_group_type_desc) < 0)
342-
return NULL;
343359
}
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) {
346364
return NULL;
347-
initialized = 1;
365+
}
366+
367+
Py_INCREF(state->StructGrpType);
368+
PyModule_AddObject(m, "struct_group", (PyObject *) state->StructGrpType);
348369
return m;
349370
}

0 commit comments

Comments
 (0)