Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-38073: Make pwd module PEP-384 compatible #15790

Merged
merged 4 commits into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
`:mod:`pwd` is :pep:`384` compatible
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sphinx doesn't like the reference here, please change the text to Make pwd extension module PEP-384 compatible

51 changes: 35 additions & 16 deletions Modules/pwdmodule.c
Expand Up @@ -47,8 +47,13 @@ The uid and gid items are integers, all others are strings. An\n\
exception is raised if the entry asked for cannot be found.");


static int initialized;
static PyTypeObject StructPwdType;
typedef struct {
PyTypeObject *StructPwdType;
} pwdmodulestate;
#define modulestate(o) ((pwdmodulestate *)PyModule_GetState(o))
#define modulestate_global modulestate(PyState_FindModule(&pwdmodule))

static struct PyModuleDef pwdmodule;

#define DEFAULT_BUFFER_SIZE 1024

Expand All @@ -69,7 +74,7 @@ static PyObject *
mkpwent(struct passwd *p)
{
int setIndex = 0;
PyObject *v = PyStructSequence_New(&StructPwdType);
PyObject *v = PyStructSequence_New(modulestate_global->StructPwdType);
if (v == NULL)
return NULL;

Expand Down Expand Up @@ -310,34 +315,48 @@ static PyMethodDef pwd_methods[] = {
{NULL, NULL} /* sentinel */
};

static int pwdmodule_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(modulestate(m)->StructPwdType);
return 0;
}
static int pwdmodule_clear(PyObject *m) {
Py_CLEAR(modulestate(m)->StructPwdType);
return 0;
}
static void pwdmodule_free(void *m) {
pwdmodule_clear((PyObject *)m);
}

static struct PyModuleDef pwdmodule = {
PyModuleDef_HEAD_INIT,
"pwd",
pwd__doc__,
-1,
sizeof(pwdmodulestate),
pwd_methods,
NULL,
NULL,
NULL,
NULL
pwdmodule_traverse,
pwdmodule_clear,
pwdmodule_free,
};


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

if (!initialized) {
if (PyStructSequence_InitType2(&StructPwdType,
&struct_pwd_type_desc) < 0)
return NULL;
initialized = 1;
pwdmodulestate *state = PyModule_GetState(m);
state->StructPwdType = PyStructSequence_NewType(&struct_pwd_type_desc);
if (state->StructPwdType == NULL) {
return NULL;
}
Py_INCREF((PyObject *) &StructPwdType);
PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType);
Py_INCREF(state->StructPwdType);
PyModule_AddObject(m, "struct_passwd", (PyObject *) state->StructPwdType);
return m;
}