bpo-38072: PEP-384 grpmodule (GH-15788)

Make the grp module PEP-384 compliant.
This commit is contained in:
Dino Viehland 2019-09-10 11:30:36 +01:00 committed by T. Wouters
parent 9e61066355
commit 40a5313edf
2 changed files with 42 additions and 20 deletions

View file

@ -0,0 +1 @@
grp module made PEP-384 compatible

View file

@ -34,8 +34,13 @@ static PyStructSequence_Desc struct_group_type_desc = {
}; };
static int initialized; typedef struct {
static PyTypeObject StructGrpType; PyTypeObject *StructGrpType;
} grpmodulestate;
#define modulestate(o) ((grpmodulestate *)PyModule_GetState(o))
#define modulestate_global modulestate(PyState_FindModule(&grpmodule))
static struct PyModuleDef grpmodule;
#define DEFAULT_BUFFER_SIZE 1024 #define DEFAULT_BUFFER_SIZE 1024
@ -43,10 +48,10 @@ static PyObject *
mkgrent(struct group *p) mkgrent(struct group *p)
{ {
int setIndex = 0; int setIndex = 0;
PyObject *v = PyStructSequence_New(&StructGrpType), *w; PyObject *v, *w;
char **member; char **member;
if (v == NULL) if ((v = PyStructSequence_New(modulestate_global->StructGrpType)) == NULL)
return NULL; return NULL;
if ((w = PyList_New(0)) == NULL) { if ((w = PyList_New(0)) == NULL) {
@ -314,36 +319,52 @@ users are not explicitly listed as members of the groups they are in\n\
according to the password database. Check both databases to get\n\ according to the password database. Check both databases to get\n\
complete membership information.)"); complete membership information.)");
static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(modulestate(m)->StructGrpType);
return 0;
}
static int grpmodule_clear(PyObject *m) {
Py_CLEAR(modulestate(m)->StructGrpType);
return 0;
}
static void grpmodule_free(void *m) {
grpmodule_clear((PyObject *)m);
}
static struct PyModuleDef grpmodule = { static struct PyModuleDef grpmodule = {
PyModuleDef_HEAD_INIT, PyModuleDef_HEAD_INIT,
"grp", "grp",
grp__doc__, grp__doc__,
-1, sizeof(grpmodulestate),
grp_methods, grp_methods,
NULL, NULL,
NULL, grpmodule_traverse,
NULL, grpmodule_clear,
NULL grpmodule_free,
}; };
PyMODINIT_FUNC PyMODINIT_FUNC
PyInit_grp(void) PyInit_grp(void)
{ {
PyObject *m, *d; PyObject *m;
m = PyModule_Create(&grpmodule); if ((m = PyState_FindModule(&grpmodule)) != NULL) {
if (m == NULL) Py_INCREF(m);
return NULL; return m;
d = PyModule_GetDict(m); }
if (!initialized) {
if (PyStructSequence_InitType2(&StructGrpType, if ((m = PyModule_Create(&grpmodule)) == NULL) {
&struct_group_type_desc) < 0) return NULL;
return NULL; }
}
if (PyDict_SetItemString(d, "struct_group", grpmodulestate *state = PyModule_GetState(m);
(PyObject *)&StructGrpType) < 0) state->StructGrpType = PyStructSequence_NewType(&struct_group_type_desc);
return NULL; if (state->StructGrpType == NULL) {
initialized = 1; return NULL;
}
Py_INCREF(state->StructGrpType);
PyModule_AddObject(m, "struct_group", (PyObject *) state->StructGrpType);
return m; return m;
} }