Skip to content

Commit

Permalink
bpo-1635741: Port grp and pwd to multiphase initialization (pythonGH-…
Browse files Browse the repository at this point in the history
…23360)

Signed-off-by: Christian Heimes <[email protected]>
  • Loading branch information
tiran authored Nov 19, 2020
1 parent cc0cd43 commit fa2eee9
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Port :mod:`grp` and :mod:`pwd` extension modules to multiphase
initialization (:pep:`489`)
71 changes: 36 additions & 35 deletions Modules/grpmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,19 @@ get_grp_state(PyObject *module)
return (grpmodulestate *)state;
}

#define modulestate_global get_grp_state(PyState_FindModule(&grpmodule))

static struct PyModuleDef grpmodule;

#define DEFAULT_BUFFER_SIZE 1024

static PyObject *
mkgrent(struct group *p)
mkgrent(PyObject *module, struct group *p)
{
int setIndex = 0;
PyObject *v, *w;
char **member;

if ((v = PyStructSequence_New(modulestate_global->StructGrpType)) == NULL)
v = PyStructSequence_New(get_grp_state(module)->StructGrpType);
if (v == NULL)
return NULL;

if ((w = PyList_New(0)) == NULL) {
Expand Down Expand Up @@ -170,7 +169,7 @@ grp_getgrgid_impl(PyObject *module, PyObject *id)
Py_DECREF(gid_obj);
return NULL;
}
retval = mkgrent(p);
retval = mkgrent(module, p);
#ifdef HAVE_GETGRGID_R
PyMem_RawFree(buf);
#endif
Expand Down Expand Up @@ -248,7 +247,7 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
}
goto out;
}
retval = mkgrent(p);
retval = mkgrent(module, p);
out:
PyMem_RawFree(buf);
Py_DECREF(bytes);
Expand All @@ -275,7 +274,7 @@ grp_getgrall_impl(PyObject *module)
return NULL;
setgrent();
while ((p = getgrent()) != NULL) {
PyObject *v = mkgrent(p);
PyObject *v = mkgrent(module, p);
if (v == NULL || PyList_Append(d, v) != 0) {
Py_XDECREF(v);
Py_DECREF(d);
Expand Down Expand Up @@ -311,6 +310,26 @@ 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\
complete membership information.)");

static int
grpmodule_exec(PyObject *module)
{
grpmodulestate *state = get_grp_state(module);

state->StructGrpType = PyStructSequence_NewType(&struct_group_type_desc);
if (state->StructGrpType == NULL) {
return -1;
}
if (PyModule_AddType(module, state->StructGrpType) < 0) {
return -1;
}
return 0;
}

static PyModuleDef_Slot grpmodule_slots[] = {
{Py_mod_exec, grpmodule_exec},
{0, NULL}
};

static int grpmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(get_grp_state(m)->StructGrpType);
return 0;
Expand All @@ -326,37 +345,19 @@ static void grpmodule_free(void *m) {
}

static struct PyModuleDef grpmodule = {
PyModuleDef_HEAD_INIT,
"grp",
grp__doc__,
sizeof(grpmodulestate),
grp_methods,
NULL,
grpmodule_traverse,
grpmodule_clear,
grpmodule_free,
PyModuleDef_HEAD_INIT,
.m_name = "grp",
.m_doc = grp__doc__,
.m_size = sizeof(grpmodulestate),
.m_methods = grp_methods,
.m_slots = grpmodule_slots,
.m_traverse = grpmodule_traverse,
.m_clear = grpmodule_clear,
.m_free = grpmodule_free,
};

PyMODINIT_FUNC
PyInit_grp(void)
{
PyObject *m;
if ((m = PyState_FindModule(&grpmodule)) != NULL) {
Py_INCREF(m);
return m;
}

if ((m = PyModule_Create(&grpmodule)) == NULL) {
return NULL;
}

grpmodulestate *state = PyModule_GetState(m);
state->StructGrpType = PyStructSequence_NewType(&struct_group_type_desc);
if (state->StructGrpType == NULL) {
return NULL;
}

Py_INCREF(state->StructGrpType);
PyModule_AddObject(m, "struct_group", (PyObject *) state->StructGrpType);
return m;
return PyModuleDef_Init(&grpmodule);
}
65 changes: 34 additions & 31 deletions Modules/pwdmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ get_pwd_state(PyObject *module)
return (pwdmodulestate *)state;
}

#define modulestate_global get_pwd_state(PyState_FindModule(&pwdmodule))

static struct PyModuleDef pwdmodule;

#define DEFAULT_BUFFER_SIZE 1024
Expand All @@ -79,10 +77,10 @@ sets(PyObject *v, int i, const char* val)
}

static PyObject *
mkpwent(struct passwd *p)
mkpwent(PyObject *module, struct passwd *p)
{
int setIndex = 0;
PyObject *v = PyStructSequence_New(modulestate_global->StructPwdType);
PyObject *v = PyStructSequence_New(get_pwd_state(module)->StructPwdType);
if (v == NULL)
return NULL;

Expand Down Expand Up @@ -194,7 +192,7 @@ pwd_getpwuid(PyObject *module, PyObject *uidobj)
Py_DECREF(uid_obj);
return NULL;
}
retval = mkpwent(p);
retval = mkpwent(module, p);
#ifdef HAVE_GETPWUID_R
PyMem_RawFree(buf);
#endif
Expand Down Expand Up @@ -274,7 +272,7 @@ pwd_getpwnam_impl(PyObject *module, PyObject *name)
}
goto out;
}
retval = mkpwent(p);
retval = mkpwent(module, p);
out:
PyMem_RawFree(buf);
Py_DECREF(bytes);
Expand All @@ -300,7 +298,7 @@ pwd_getpwall_impl(PyObject *module)
return NULL;
setpwent();
while ((p = getpwent()) != NULL) {
PyObject *v = mkpwent(p);
PyObject *v = mkpwent(module, p);
if (v == NULL || PyList_Append(d, v) != 0) {
Py_XDECREF(v);
Py_DECREF(d);
Expand All @@ -323,6 +321,26 @@ static PyMethodDef pwd_methods[] = {
{NULL, NULL} /* sentinel */
};

static int
pwdmodule_exec(PyObject *module)
{
pwdmodulestate *state = get_pwd_state(module);

state->StructPwdType = PyStructSequence_NewType(&struct_pwd_type_desc);
if (state->StructPwdType == NULL) {
return -1;
}
if (PyModule_AddType(module, state->StructPwdType) < 0) {
return -1;
}
return 0;
}

static PyModuleDef_Slot pwdmodule_slots[] = {
{Py_mod_exec, pwdmodule_exec},
{0, NULL}
};

static int pwdmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(get_pwd_state(m)->StructPwdType);
return 0;
Expand All @@ -337,34 +355,19 @@ static void pwdmodule_free(void *m) {

static struct PyModuleDef pwdmodule = {
PyModuleDef_HEAD_INIT,
"pwd",
pwd__doc__,
sizeof(pwdmodulestate),
pwd_methods,
NULL,
pwdmodule_traverse,
pwdmodule_clear,
pwdmodule_free,
.m_name = "pwd",
.m_doc = pwd__doc__,
.m_size = sizeof(pwdmodulestate),
.m_methods = pwd_methods,
.m_slots = pwdmodule_slots,
.m_traverse = pwdmodule_traverse,
.m_clear = pwdmodule_clear,
.m_free = pwdmodule_free,
};


PyMODINIT_FUNC
PyInit_pwd(void)
{
PyObject *m;
if ((m = PyState_FindModule(&pwdmodule)) != NULL) {
Py_INCREF(m);
return m;
}
if ((m = PyModule_Create(&pwdmodule)) == NULL)
return NULL;

pwdmodulestate *state = PyModule_GetState(m);
state->StructPwdType = PyStructSequence_NewType(&struct_pwd_type_desc);
if (state->StructPwdType == NULL) {
return NULL;
}
Py_INCREF(state->StructPwdType);
PyModule_AddObject(m, "struct_passwd", (PyObject *) state->StructPwdType);
return m;
return PyModuleDef_Init(&pwdmodule);
}

0 comments on commit fa2eee9

Please sign in to comment.