[Python-checkins] bpo-32539: Fix OSError for os.listdir() for extended-length paths on Windows (#5169)

Serhiy Storchaka webhook-mailer at python.org
Mon Jan 15 16:39:07 EST 2018


https://github.com/python/cpython/commit/27f32e938ff51fd5d90a29abbbabc6b81d156f33
commit: 27f32e938ff51fd5d90a29abbbabc6b81d156f33
branch: 2.7
author: Anthony Sottile <asottile at umich.edu>
committer: Serhiy Storchaka <storchaka at gmail.com>
date: 2018年01月15日T23:39:04+02:00
summary:
bpo-32539: Fix OSError for os.listdir() for extended-length paths on Windows (#5169)
See https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx?f=255&MSPPError=-2147217396#maxpath
Paths that begin with `\\?\` are "extended-length paths".
files:
A Misc/NEWS.d/next/Library/2018-01-12-09-20-22.bpo-32539.D7AbdE.rst
M Lib/test/test_os.py
M Modules/posixmodule.c
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
index aca03fdabb1..84e20e0c013 100644
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -900,6 +900,56 @@ def test_CTRL_BREAK_EVENT(self):
 self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
 
 
+ at 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
+ fs_encoding = sys.getfilesystemencoding()
+ self.assertEqual(
+ sorted(os.listdir(support.TESTFN.decode(fs_encoding))),
+ [path.decode(fs_encoding) for path in self.created_paths])
+
+ # bytes
+ self.assertEqual(
+ sorted(os.listdir(os.fsencode(support.TESTFN))),
+ 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
+ fs_encoding = sys.getfilesystemencoding()
+ path = u'\\\\?\\' + os.path.abspath(support.TESTFN.decode(fs_encoding))
+ self.assertEqual(
+ sorted(os.listdir(path)),
+ [path.decode(fs_encoding) for path in self.created_paths])
+
+ # bytes
+ path = b'\\\\?\\' + os.path.abspath(support.TESTFN)
+ self.assertEqual(
+ sorted(os.listdir(path)),
+ self.created_paths)
+
+
 class SpawnTests(unittest.TestCase):
 def _test_invalid_env(self, spawn):
 args = [sys.executable, '-c', 'pass']
diff --git a/Misc/NEWS.d/next/Library/2018-01-12-09-20-22.bpo-32539.D7AbdE.rst b/Misc/NEWS.d/next/Library/2018-01-12-09-20-22.bpo-32539.D7AbdE.rst
new file mode 100644
index 00000000000..cc24a6b953d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-01-12-09-20-22.bpo-32539.D7AbdE.rst
@@ -0,0 +1,2 @@
+Fix ``OSError`` for ``os.listdir`` with deep paths (starting with ``\\?\``) on
+windows. Patch by Anthony Sottile.
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index c9886d514b7..cecbb45fb4b 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -2385,7 +2385,7 @@ posix_listdir(PyObject *self, PyObject *args)
 if (len > 0) {
 char ch = namebuf[len-1];
 if (ch != SEP && ch != ALTSEP && ch != ':')
- namebuf[len++] = '/';
+ namebuf[len++] = SEP;
 strcpy(namebuf + len, "*.*");
 }
 


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /