diff options
Diffstat (limited to 'Modules/pwdmodule.c')
-rw-r--r-- | Modules/pwdmodule.c | 54 |
1 files changed, 38 insertions, 16 deletions
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c index 6729c84fedf..1e0903a3887 100644 --- a/Modules/pwdmodule.c +++ b/Modules/pwdmodule.c @@ -2,7 +2,6 @@ /* UNIX password file access module */ #include "Python.h" -#include "structseq.h" #include <sys/types.h> #include <pwd.h> @@ -46,10 +45,12 @@ static int initialized; static PyTypeObject StructPwdType; static void -sets(PyObject *v, int i, char* val) +sets(PyObject *v, int i, const char* val) { - if (val) - PyStructSequence_SET_ITEM(v, i, PyString_FromString(val)); + if (val) { + PyObject *o = PyUnicode_DecodeFSDefault(val); + PyStructSequence_SET_ITEM(v, i, o); + } else { PyStructSequence_SET_ITEM(v, i, Py_None); Py_INCREF(Py_None); @@ -64,7 +65,7 @@ mkpwent(struct passwd *p) if (v == NULL) return NULL; -#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyInt_FromLong((long) val)) +#define SETI(i,val) PyStructSequence_SET_ITEM(v, i, PyLong_FromLong((long) val)) #define SETS(i,val) sets(v, i, val) SETS(setIndex++, p->pw_name); @@ -126,14 +127,23 @@ pwd_getpwnam(PyObject *self, PyObject *args) { char *name; struct passwd *p; - if (!PyArg_ParseTuple(args, "s:getpwnam", &name)) + PyObject *arg, *bytes, *retval = NULL; + + if (!PyArg_ParseTuple(args, "U:getpwnam", &arg)) + return NULL; + if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL) return NULL; + if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1) + goto out; if ((p = getpwnam(name)) == NULL) { PyErr_Format(PyExc_KeyError, "getpwnam(): name not found: %s", name); - return NULL; + goto out; } - return mkpwent(p); + retval = mkpwent(p); +out: + Py_DECREF(bytes); + return retval; } #ifdef HAVE_GETPWENT @@ -180,21 +190,33 @@ static PyMethodDef pwd_methods[] = { {NULL, NULL} /* sentinel */ }; +static struct PyModuleDef pwdmodule = { + PyModuleDef_HEAD_INIT, + "pwd", + pwd__doc__, + -1, + pwd_methods, + NULL, + NULL, + NULL, + NULL +}; + + PyMODINIT_FUNC -initpwd(void) +PyInit_pwd(void) { PyObject *m; - m = Py_InitModule3("pwd", pwd_methods, pwd__doc__); + m = PyModule_Create(&pwdmodule); if (m == NULL) - return; + return NULL; - if (!initialized) + if (!initialized) { PyStructSequence_InitType(&StructPwdType, &struct_pwd_type_desc); + initialized = 1; + } Py_INCREF((PyObject *) &StructPwdType); PyModule_AddObject(m, "struct_passwd", (PyObject *) &StructPwdType); - /* And for b/w compatibility (this was defined by mistake): */ - Py_INCREF((PyObject *) &StructPwdType); - PyModule_AddObject(m, "struct_pwent", (PyObject *) &StructPwdType); - initialized = 1; + return m; } |