]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue13234 Allow listdir to handle extended paths on Windows (Patch by Santoso Wijaya)
authorTim Golden <mail@timgolden.me.uk>
Fri, 25 Oct 2013 19:24:06 +0000 (20:24 +0100)
committerTim Golden <mail@timgolden.me.uk>
Fri, 25 Oct 2013 19:24:06 +0000 (20:24 +0100)
Lib/test/test_os.py
Modules/posixmodule.c

index f1d652c9a5849d3ed406cd2e5c4ff15d7bffc058..1c5fe81eece0b7bbb39ba509e802a8774ef01cd6 100644 (file)
@@ -1558,6 +1558,52 @@ class Win32KillTests(unittest.TestCase):
         self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
 
 
+@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
+class Win32ListdirTests(unittest.TestCase):
+    """Test listdir on Windows."""
+
+    def setUp(self):
+        self.created_paths = []
+        for i in range(2):
+            dir_name = 'SUB%d' % i
+            dir_path = os.path.join(support.TESTFN, dir_name)
+            file_name = 'FILE%d' % i
+            file_path = os.path.join(support.TESTFN, file_name)
+            os.makedirs(dir_path)
+            with open(file_path, 'w') as f:
+                f.write("I'm %s and proud of it. Blame test_os.\n" % file_path)
+            self.created_paths.extend([dir_name, file_name])
+        self.created_paths.sort()
+
+    def tearDown(self):
+        shutil.rmtree(support.TESTFN)
+
+    def test_listdir_no_extended_path(self):
+        """Test when the path is not an "extended" path."""
+        # unicode
+        self.assertEqual(
+                sorted(os.listdir(support.TESTFN)),
+                self.created_paths)
+        # bytes
+        self.assertEqual(
+                sorted(os.listdir(os.fsencode(support.TESTFN))),
+                [os.fsencode(path) for path in self.created_paths])
+
+    def test_listdir_extended_path(self):
+        """Test when the path starts with '\\\\?\\'."""
+        # See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath 
+        # unicode
+        path = '\\\\?\\' + os.path.abspath(support.TESTFN)
+        self.assertEqual(
+                sorted(os.listdir(path)),
+                self.created_paths)
+        # bytes
+        path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
+        self.assertEqual(
+                sorted(os.listdir(path)),
+                [os.fsencode(path) for path in self.created_paths])
+
+
 @unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
 @support.skip_unless_symlink
 class Win32SymlinkTests(unittest.TestCase):
@@ -2427,6 +2473,7 @@ def test_main():
         PosixUidGidTests,
         Pep383Tests,
         Win32KillTests,
+        Win32ListdirTests,
         Win32SymlinkTests,
         NonLocalSymlinkTests,
         FSEncodingTests,
index b99b6a27670ca336fe38a802c4a11eaa61d49ed1..d5b5235a4d6e28bb98f5bcd91ad2b92942243adb 100644 (file)
@@ -3608,8 +3608,8 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
         wcscpy(wnamebuf, po_wchars);
         if (len > 0) {
             wchar_t wch = wnamebuf[len-1];
-            if (wch != L'/' && wch != L'\\' && wch != L':')
-                wnamebuf[len++] = L'\\';
+            if (wch != SEP && wch != ALTSEP && wch != L':')
+                wnamebuf[len++] = SEP;
             wcscpy(wnamebuf + len, L"*.*");
         }
         if ((list = PyList_New(0)) == NULL) {
@@ -3663,8 +3663,8 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
     len = path->length;
     if (len > 0) {
         char ch = namebuf[len-1];
-        if (ch != SEP && ch != ALTSEP && ch != ':')
-            namebuf[len++] = '/';
+        if (ch != '\\' && ch != '/' && ch != ':')
+            namebuf[len++] = '\\';
         strcpy(namebuf + len, "*.*");
     }