Commits

Martin von Löwis committed 28d6419

Issue #4859: Implement PEP 383 for pwd, spwd, and grp.

Comments (0)

Files changed (5)

 Barry Warsaw
 Steve Waterbury
 Bob Watson
+David Watson
 Aaron Watters
 Henrik Weber
 Corran Webster
 Library
 -------
 
+- Issue #4859: Implement PEP 383 for pwd, spwd, and grp.
+
 - smtplib 'login' and 'cram-md5' login are also fixed (see Issue #5259).
 
 - Issue #6121: pydoc now ignores leading and trailing spaces in the

Modules/grpmodule.c

         Py_DECREF(v);
         return NULL;
     }
+#define FSDECODE(val) PyUnicode_Decode(val, strlen(val),\
+                                       Py_FileSystemDefaultEncoding,\
+                                       "surrogateescape")
     for (member = p->gr_mem; *member != NULL; member++) {
-        PyObject *x = PyUnicode_FromString(*member);
+        PyObject *x = FSDECODE(*member);
         if (x == NULL || PyList_Append(w, x) != 0) {
             Py_XDECREF(x);
             Py_DECREF(w);
     }
 
 #define SET(i,val) PyStructSequence_SET_ITEM(v, i, val)
-    SET(setIndex++, PyUnicode_FromString(p->gr_name));
+    SET(setIndex++, FSDECODE(p->gr_name));
 #ifdef __VMS
     SET(setIndex++, Py_None);
     Py_INCREF(Py_None);
 #else
     if (p->gr_passwd)
-	    SET(setIndex++, PyUnicode_FromString(p->gr_passwd));
+	    SET(setIndex++, FSDECODE(p->gr_passwd));
     else {
 	    SET(setIndex++, Py_None);
 	    Py_INCREF(Py_None);
 }
 
 static PyObject *
-grp_getgrnam(PyObject *self, PyObject *pyo_name)
+grp_getgrnam(PyObject *self, PyObject *args)
 {
-    PyObject *py_str_name;
     char *name;
     struct group *p;
+    PyObject *arg, *bytes, *retval = NULL;
 
-    py_str_name = PyObject_Str(pyo_name);
-    if (!py_str_name)
-	    return NULL;
-    name = _PyUnicode_AsString(py_str_name);
+    if (!PyArg_ParseTuple(args, "U:getgrnam", &arg))
+        return NULL;
+    if ((bytes = PyUnicode_AsEncodedString(arg, Py_FileSystemDefaultEncoding,
+                                           "surrogateescape")) == NULL)
+        return NULL;
+    if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
+        goto out;
     
     if ((p = getgrnam(name)) == NULL) {
 	PyErr_Format(PyExc_KeyError, "getgrnam(): name not found: %s", name);
-	Py_DECREF(py_str_name);
-        return NULL;
+        goto out;
     }
-
-    Py_DECREF(py_str_name);
-    return mkgrent(p);
+    retval = mkgrent(p);
+out:
+    Py_DECREF(bytes);
+    return retval;
 }
 
 static PyObject *
      "getgrgid(id) -> tuple\n\
 Return the group database entry for the given numeric group ID.  If\n\
 id is not valid, raise KeyError."},
-    {"getgrnam",	grp_getgrnam,	METH_O,
+    {"getgrnam",	grp_getgrnam,	METH_VARARGS,
      "getgrnam(name) -> tuple\n\
 Return the group database entry for the given group name.  If\n\
 name is not valid, raise KeyError."},

Modules/pwdmodule.c

 sets(PyObject *v, int i, const char* val)
 {
   if (val) {
-	  PyObject *o =
-		PyUnicode_DecodeUnicodeEscape(val, strlen(val), "strict");
+	  PyObject *o = PyUnicode_Decode(val, strlen(val),
+					 Py_FileSystemDefaultEncoding,
+					 "surrogateescape");
 	  PyStructSequence_SET_ITEM(v, i, o);
   }
   else {
 {
 	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_AsEncodedString(arg,
+					       Py_FileSystemDefaultEncoding,
+					       "surrogateescape")) == 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

Modules/spwdmodule.c

 static void
 sets(PyObject *v, int i, const char* val)
 {
-  if (val)
-	  PyStructSequence_SET_ITEM(v, i, PyUnicode_FromString(val));
-  else {
+  if (val) {
+	  PyObject *o = PyUnicode_Decode(val, strlen(val),
+					 Py_FileSystemDefaultEncoding,
+					 "surrogateescape");
+	  PyStructSequence_SET_ITEM(v, i, o);
+  } else {
 	  PyStructSequence_SET_ITEM(v, i, Py_None);
 	  Py_INCREF(Py_None);
   }
 {
 	char *name;
 	struct spwd *p;
-	if (!PyArg_ParseTuple(args, "s:getspnam", &name))
+	PyObject *arg, *bytes, *retval = NULL;
+
+	if (!PyArg_ParseTuple(args, "U:getspnam", &arg))
 		return NULL;
+	if ((bytes = PyUnicode_AsEncodedString(arg,
+					       Py_FileSystemDefaultEncoding,
+					       "surrogateescape")) == NULL)
+		return NULL;
+	if (PyBytes_AsStringAndSize(bytes, &name, NULL) == -1)
+		goto out;
 	if ((p = getspnam(name)) == NULL) {
 		PyErr_SetString(PyExc_KeyError, "getspnam(): name not found");
-		return NULL;
+		goto out;
 	}
-	return mkspent(p);
+	retval = mkspent(p);
+out:
+	Py_DECREF(bytes);
+	return retval;
 }
 
 #endif /* HAVE_GETSPNAM */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.