[Python-checkins] bpo-34604: Fix possible mojibake in pwd.getpwnam() and grp.getgrnam() (GH-9098)

Victor Stinner webhook-mailer at python.org
Fri Sep 7 13:10:43 EDT 2018


https://github.com/python/cpython/commit/28658485a54ad5f9df52ecc12d9046269f1654ec
commit: 28658485a54ad5f9df52ecc12d9046269f1654ec
branch: master
author: William Grzybowski <wg at FreeBSD.org>
committer: Victor Stinner <vstinner at redhat.com>
date: 2018-09-07T19:10:39+02:00
summary:

bpo-34604: Fix possible mojibake in pwd.getpwnam() and grp.getgrnam() (GH-9098)

Pass the user/group name as Unicode to the formatting function,
instead of always decoding a bytes string from UTF-8.

files:
A Misc/NEWS.d/next/Library/2018-09-07-10-16-34.bpo-34604.xL7-kG.rst
M Modules/clinic/pwdmodule.c.h
M Modules/grpmodule.c
M Modules/pwdmodule.c

diff --git a/Misc/NEWS.d/next/Library/2018-09-07-10-16-34.bpo-34604.xL7-kG.rst b/Misc/NEWS.d/next/Library/2018-09-07-10-16-34.bpo-34604.xL7-kG.rst
new file mode 100644
index 000000000000..562a69124b3d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-09-07-10-16-34.bpo-34604.xL7-kG.rst
@@ -0,0 +1,2 @@
+Fix possible mojibake in the error message of `pwd.getpwnam` and
+`grp.getgrnam`. Patch by William Grzybowski.
diff --git a/Modules/clinic/pwdmodule.c.h b/Modules/clinic/pwdmodule.c.h
index f9e0644f264d..979dfdb07ee2 100644
--- a/Modules/clinic/pwdmodule.c.h
+++ b/Modules/clinic/pwdmodule.c.h
@@ -14,7 +14,7 @@ PyDoc_STRVAR(pwd_getpwuid__doc__,
     {"getpwuid", (PyCFunction)pwd_getpwuid, METH_O, pwd_getpwuid__doc__},
 
 PyDoc_STRVAR(pwd_getpwnam__doc__,
-"getpwnam($module, arg, /)\n"
+"getpwnam($module, name, /)\n"
 "--\n"
 "\n"
 "Return the password database entry for the given user name.\n"
@@ -25,18 +25,18 @@ PyDoc_STRVAR(pwd_getpwnam__doc__,
     {"getpwnam", (PyCFunction)pwd_getpwnam, METH_O, pwd_getpwnam__doc__},
 
 static PyObject *
-pwd_getpwnam_impl(PyObject *module, PyObject *arg);
+pwd_getpwnam_impl(PyObject *module, PyObject *name);
 
 static PyObject *
-pwd_getpwnam(PyObject *module, PyObject *arg_)
+pwd_getpwnam(PyObject *module, PyObject *arg)
 {
     PyObject *return_value = NULL;
-    PyObject *arg;
+    PyObject *name;
 
-    if (!PyArg_Parse(arg_, "U:getpwnam", &arg)) {
+    if (!PyArg_Parse(arg, "U:getpwnam", &name)) {
         goto exit;
     }
-    return_value = pwd_getpwnam_impl(module, arg);
+    return_value = pwd_getpwnam_impl(module, name);
 
 exit:
     return return_value;
@@ -69,4 +69,4 @@ pwd_getpwall(PyObject *module, PyObject *Py_UNUSED(ignored))
 #ifndef PWD_GETPWALL_METHODDEF
     #define PWD_GETPWALL_METHODDEF
 #endif /* !defined(PWD_GETPWALL_METHODDEF) */
-/*[clinic end generated code: output=fc41d8d88ec206d8 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=3c93120d6dd86905 input=a9049054013a1b77]*/
diff --git a/Modules/grpmodule.c b/Modules/grpmodule.c
index ffdfa1b6f9fb..74286ab3974d 100644
--- a/Modules/grpmodule.c
+++ b/Modules/grpmodule.c
@@ -245,7 +245,7 @@ grp_getgrnam_impl(PyObject *module, PyObject *name)
             PyErr_NoMemory();
         }
         else {
-            PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name_chars);
+            PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %S", name);
         }
         goto out;
     }
diff --git a/Modules/pwdmodule.c b/Modules/pwdmodule.c
index 36192a570433..d15286dc10fc 100644
--- a/Modules/pwdmodule.c
+++ b/Modules/pwdmodule.c
@@ -189,7 +189,7 @@ pwd_getpwuid(PyObject *module, PyObject *uidobj)
 /*[clinic input]
 pwd.getpwnam
 
-    arg: unicode
+    name: unicode
     /
 
 Return the password database entry for the given user name.
@@ -198,18 +198,18 @@ See `help(pwd)` for more on password database entries.
 [clinic start generated code]*/
 
 static PyObject *
-pwd_getpwnam_impl(PyObject *module, PyObject *arg)
-/*[clinic end generated code: output=6abeee92430e43d2 input=d5f7e700919b02d3]*/
+pwd_getpwnam_impl(PyObject *module, PyObject *name)
+/*[clinic end generated code: output=359ce1ddeb7a824f input=a6aeb5e3447fb9e0]*/
 {
-    char *buf = NULL, *buf2 = NULL, *name;
+    char *buf = NULL, *buf2 = NULL, *name_chars;
     int nomem = 0;
     struct passwd *p;
     PyObject *bytes, *retval = NULL;
 
-    if ((bytes = PyUnicode_EncodeFSDefault(arg)) == NULL)
+    if ((bytes = PyUnicode_EncodeFSDefault(name)) == NULL)
         return NULL;
     /* check for embedded null bytes */
-    if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
+    if (PyBytes_AsStringAndSize(bytes, &name_chars, NULL) == -1)
         goto out;
 #ifdef HAVE_GETPWNAM_R
     Py_BEGIN_ALLOW_THREADS
@@ -229,7 +229,7 @@ pwd_getpwnam_impl(PyObject *module, PyObject *arg)
             break;
         }
         buf = buf2;
-        status = getpwnam_r(name, &pwd, buf, bufsize, &p);
+        status = getpwnam_r(name_chars, &pwd, buf, bufsize, &p);
         if (status != 0) {
             p = NULL;
         }
@@ -245,7 +245,7 @@ pwd_getpwnam_impl(PyObject *module, PyObject *arg)
 
     Py_END_ALLOW_THREADS
 #else
-    p = getpwnam(name);
+    p = getpwnam(name_chars);
 #endif
     if (p == NULL) {
         if (nomem == 1) {
@@ -253,7 +253,7 @@ pwd_getpwnam_impl(PyObject *module, PyObject *arg)
         }
         else {
             PyErr_Format(PyExc_KeyError,
-                         "getpwnam(): name not found: %s", name);
+                         "getpwnam(): name not found: %S", name);
         }
         goto out;
     }



More information about the Python-checkins mailing list