Skip to content

Commit 781bbeb

Browse files
committed
Issue13234 Allow listdir to handle extended paths on Windows (Patch by Santoso Wijaya)
1 parent 502834c commit 781bbeb

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

Lib/test/test_os.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,52 @@ def test_CTRL_BREAK_EVENT(self):
15581558
self._kill_with_event(signal.CTRL_BREAK_EVENT, "CTRL_BREAK_EVENT")
15591559

15601560

1561+
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
1562+
class Win32ListdirTests(unittest.TestCase):
1563+
"""Test listdir on Windows."""
1564+
1565+
def setUp(self):
1566+
self.created_paths = []
1567+
for i in range(2):
1568+
dir_name = 'SUB%d' % i
1569+
dir_path = os.path.join(support.TESTFN, dir_name)
1570+
file_name = 'FILE%d' % i
1571+
file_path = os.path.join(support.TESTFN, file_name)
1572+
os.makedirs(dir_path)
1573+
with open(file_path, 'w') as f:
1574+
f.write("I'm %s and proud of it. Blame test_os.\n" % file_path)
1575+
self.created_paths.extend([dir_name, file_name])
1576+
self.created_paths.sort()
1577+
1578+
def tearDown(self):
1579+
shutil.rmtree(support.TESTFN)
1580+
1581+
def test_listdir_no_extended_path(self):
1582+
"""Test when the path is not an "extended" path."""
1583+
# unicode
1584+
self.assertEqual(
1585+
sorted(os.listdir(support.TESTFN)),
1586+
self.created_paths)
1587+
# bytes
1588+
self.assertEqual(
1589+
sorted(os.listdir(os.fsencode(support.TESTFN))),
1590+
[os.fsencode(path) for path in self.created_paths])
1591+
1592+
def test_listdir_extended_path(self):
1593+
"""Test when the path starts with '\\\\?\\'."""
1594+
# See: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx#maxpath
1595+
# unicode
1596+
path = '\\\\?\\' + os.path.abspath(support.TESTFN)
1597+
self.assertEqual(
1598+
sorted(os.listdir(path)),
1599+
self.created_paths)
1600+
# bytes
1601+
path = b'\\\\?\\' + os.fsencode(os.path.abspath(support.TESTFN))
1602+
self.assertEqual(
1603+
sorted(os.listdir(path)),
1604+
[os.fsencode(path) for path in self.created_paths])
1605+
1606+
15611607
@unittest.skipUnless(sys.platform == "win32", "Win32 specific tests")
15621608
@support.skip_unless_symlink
15631609
class Win32SymlinkTests(unittest.TestCase):
@@ -2427,6 +2473,7 @@ def test_main():
24272473
PosixUidGidTests,
24282474
Pep383Tests,
24292475
Win32KillTests,
2476+
Win32ListdirTests,
24302477
Win32SymlinkTests,
24312478
NonLocalSymlinkTests,
24322479
FSEncodingTests,

Modules/posixmodule.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3608,8 +3608,8 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
36083608
wcscpy(wnamebuf, po_wchars);
36093609
if (len > 0) {
36103610
wchar_t wch = wnamebuf[len-1];
3611-
if (wch != L'/' && wch != L'\\' && wch != L':')
3612-
wnamebuf[len++] = L'\\';
3611+
if (wch != SEP && wch != ALTSEP && wch != L':')
3612+
wnamebuf[len++] = SEP;
36133613
wcscpy(wnamebuf + len, L"*.*");
36143614
}
36153615
if ((list = PyList_New(0)) == NULL) {
@@ -3663,8 +3663,8 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
36633663
len = path->length;
36643664
if (len > 0) {
36653665
char ch = namebuf[len-1];
3666-
if (ch != SEP && ch != ALTSEP && ch != ':')
3667-
namebuf[len++] = '/';
3666+
if (ch != '\\' && ch != '/' && ch != ':')
3667+
namebuf[len++] = '\\';
36683668
strcpy(namebuf + len, "*.*");
36693669
}
36703670

0 commit comments

Comments
 (0)