diff -r 09a1bd44d96f Lib/test/test_os.py --- a/Lib/test/test_os.py Tue Oct 18 13:02:11 2011 +0300 +++ b/Lib/test/test_os.py Thu Oct 20 18:53:06 2011 +0200 @@ -804,6 +804,51 @@ 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): + """Set the different tests.""" + super(Win32ListdirTests, self).setUp() + self.created_paths = [] + while len(self.created_paths) < 3: + dir_path = os.join(test_support.TESTFN, + 'SUB' + len(self.created_paths)) + file_path = os.join(test_support.TESTFN, + 'FILE' + len(self.created_paths)) + os.makedirs(dirpath) + with file(file_path, 'w') as f: + f.write("I'm " + path + " and proud of it. Blame test_os.\n") + self.created_paths.extend([dir_path, file_path]) + self.created_paths.sort() + + + def tearDown(self): + """Clean after tests.""" + # same tear down as the one found in the walk tests + for root, dirs, files in os.walk(test_support.TESTFN, topdown=False): + for name in files: + os.remove(os.path.join(root, name)) + for name in dirs: + dirname = os.path.join(root, name) + if not os.path.islink(dirname): + os.rmdir(dirname) + else: + os.remove(dirname) + os.rmdir(test_support.TESTFN) + + def test_listdir_no_literal_path(self): + """Test when the path is not a literal path.""" + self.assertEqual(self.created_paths, + sorted(os.listdir(test_support.TESTFN)) + + def test_listdir_literal_path(self): + """Test when the path starts with \\?\""" + path = '\\\\?\\' + os.path.abspath(test_support.TESTFN) + self.assertEqual(self.created_paths, sorted(os.listdir(path)) + + def test_main(): test_support.run_unittest( FileTests, @@ -817,7 +862,8 @@ Win32ErrorTests, TestInvalidFD, PosixUidGidTests, - Win32KillTests + Win32KillTests, + Win32ListdirTests, ) if __name__ == "__main__": diff -r 09a1bd44d96f Modules/posixmodule.c --- a/Modules/posixmodule.c Tue Oct 18 13:02:11 2011 +0300 +++ b/Modules/posixmodule.c Thu Oct 20 18:53:06 2011 +0200 @@ -57,6 +57,7 @@ #include #include #include +#include #include #endif #include "osdefs.h" @@ -2190,8 +2191,14 @@ return NULL; if (len > 0) { char ch = namebuf[len-1]; - if (ch != SEP && ch != ALTSEP && ch != ':') - namebuf[len++] = '/'; + if (ch != SEP && ch != ALTSEP && ch != ':'){ + /* We need to ensure that we are not dealing with + a literal path. If we do we have to use \ */ + if(strncmp(namebuf, "\\\\?\\", 4) == 0) + namebuf[len++] = '\\'; + else + namebuf[len++] = '/'; + } strcpy(namebuf + len, "*.*"); }