[Python-checkins] cpython (merge 3.6 -> default): Issue #27998: Fixed bytes path support in os.scandir() on Windows.

serhiy.storchaka python-checkins at python.org
Sat Oct 8 13:17:44 EDT 2016


https://hg.python.org/cpython/rev/837114dea493
changeset:   104374:837114dea493
parent:      104372:43b1b3c883ff
parent:      104373:4ed634870a9a
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sat Oct 08 20:17:26 2016 +0300
summary:
  Issue #27998: Fixed bytes path support in os.scandir() on Windows.
Patch by Eryk Sun.

files:
  Misc/NEWS             |    3 +
  Modules/posixmodule.c |  109 ++++++++++++++---------------
  2 files changed, 56 insertions(+), 56 deletions(-)


diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -70,6 +70,9 @@
 Library
 -------
 
+- Issue #27998: Fixed bytes path support in os.scandir() on Windows.
+  Patch by Eryk Sun.
+
 - Issue #28317: The disassembler now decodes FORMAT_VALUE argument.
 
 - Issue #26293: Fixed writing ZIP files that starts not from the start of the
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -1337,27 +1337,37 @@
 #endif /* MS_WINDOWS */
 
 static PyObject *
+path_object_error(PyObject *path)
+{
+#ifdef MS_WINDOWS
+    return PyErr_SetExcFromWindowsErrWithFilenameObject(
+                PyExc_OSError, 0, path);
+#else
+    return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
+#endif
+}
+
+static PyObject *
+path_object_error2(PyObject *path, PyObject *path2)
+{
+#ifdef MS_WINDOWS
+    return PyErr_SetExcFromWindowsErrWithFilenameObjects(
+                PyExc_OSError, 0, path, path2);
+#else
+    return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
+#endif
+}
+
+static PyObject *
 path_error(path_t *path)
 {
-#ifdef MS_WINDOWS
-    return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
-                                                        0, path->object);
-#else
-    return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
-#endif
-}
-
+    return path_object_error(path->object);
+}
 
 static PyObject *
 path_error2(path_t *path, path_t *path2)
 {
-#ifdef MS_WINDOWS
-    return PyErr_SetExcFromWindowsErrWithFilenameObjects(PyExc_OSError,
-            0, path->object, path2->object);
-#else
-    return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError,
-        path->object, path2->object);
-#endif
+    return path_object_error2(path->object, path2->object);
 }
 
 
@@ -11152,41 +11162,26 @@
 DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
 {
     int result;
-    struct _Py_stat_struct st;
-
-#ifdef MS_WINDOWS
-    const wchar_t *path;
-
-    path = PyUnicode_AsUnicode(self->path);
-    if (!path)
-        return NULL;
-
-    if (follow_symlinks)
-        result = win32_stat(path, &st);
-    else
-        result = win32_lstat(path, &st);
-
-    if (result != 0) {
-        return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
-                                                            0, self->path);
-    }
+    STRUCT_STAT st;
+    PyObject *ub;
+
+#ifdef MS_WINDOWS
+    if (PyUnicode_FSDecoder(self->path, &ub)) {
+        const wchar_t *path = PyUnicode_AsUnicode(ub);
 #else /* POSIX */
-    PyObject *bytes;
-    const char *path;
-
-    if (!PyUnicode_FSConverter(self->path, &bytes))
-        return NULL;
-    path = PyBytes_AS_STRING(bytes);
-
-    if (follow_symlinks)
-        result = STAT(path, &st);
-    else
-        result = LSTAT(path, &st);
-    Py_DECREF(bytes);
+    if (PyUnicode_FSConverter(self->path, &ub)) {
+        const char *path = PyBytes_AS_STRING(ub);
+#endif
+        if (follow_symlinks)
+            result = STAT(path, &st);
+        else
+            result = LSTAT(path, &st);
+        Py_DECREF(ub);
+    } else
+        return NULL;
 
     if (result != 0)
-        return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, self->path);
-#endif
+        return path_object_error(self->path);
 
     return _pystat_fromstructstat(&st);
 }
@@ -11356,17 +11351,19 @@
 {
 #ifdef MS_WINDOWS
     if (!self->got_file_index) {
+        PyObject *unicode;
         const wchar_t *path;
-        struct _Py_stat_struct stat;
-
-        path = PyUnicode_AsUnicode(self->path);
-        if (!path)
+        STRUCT_STAT stat;
+        int result;
+
+        if (!PyUnicode_FSDecoder(self->path, &unicode))
             return NULL;
-
-        if (win32_lstat(path, &stat) != 0) {
-            return PyErr_SetExcFromWindowsErrWithFilenameObject(PyExc_OSError,
-                                                                0, self->path);
-        }
+        path = PyUnicode_AsUnicode(unicode);
+        result = LSTAT(path, &stat);
+        Py_DECREF(unicode);
+
+        if (result != 0)
+            return path_object_error(self->path);
 
         self->win32_file_index = stat.st_ino;
         self->got_file_index = 1;

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list