[Python-checkins] bpo-38453: Ensure correct short path is obtained for test (GH-17184)

Steve Dower webhook-mailer at python.org
Fri Nov 15 19:04:04 EST 2019


https://github.com/python/cpython/commit/7c6130c8c36c941255365e5414c956fc919b8629
commit: 7c6130c8c36c941255365e5414c956fc919b8629
branch: master
author: Steve Dower <steve.dower at python.org>
committer: GitHub <noreply at github.com>
date: 2019-11-15T16:04:00-08:00
summary:

bpo-38453: Ensure correct short path is obtained for test (GH-17184)

files:
M Lib/test/test_ntpath.py

diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py
index a84b94c9badbe..4a02db281724e 100644
--- a/Lib/test/test_ntpath.py
+++ b/Lib/test/test_ntpath.py
@@ -22,6 +22,23 @@
 else:
     HAVE_GETFINALPATHNAME = True
 
+try:
+    import ctypes
+except ImportError:
+    HAVE_GETSHORTPATHNAME = False
+else:
+    HAVE_GETSHORTPATHNAME = True
+    def _getshortpathname(path):
+        GSPN = ctypes.WinDLL("kernel32", use_last_error=True).GetShortPathNameW
+        GSPN.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32]
+        GSPN.restype = ctypes.c_uint32
+        result_len = GSPN(path, None, 0)
+        if not result_len:
+            raise OSError("failed to get short path name 0x{:08X}"
+                          .format(ctypes.get_last_error()))
+        result = ctypes.create_unicode_buffer(result_len)
+        result_len = GSPN(path, result, result_len)
+        return result[:result_len]
 
 def _norm(path):
     if isinstance(path, (bytes, str, os.PathLike)):
@@ -403,6 +420,7 @@ def test_realpath_nul(self):
         tester("ntpath.realpath('NUL')", r'\\.\NUL')
 
     @unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
+    @unittest.skipUnless(HAVE_GETSHORTPATHNAME, 'need _getshortpathname')
     def test_realpath_cwd(self):
         ABSTFN = ntpath.abspath(support.TESTFN)
 
@@ -412,12 +430,12 @@ def test_realpath_cwd(self):
         self.addCleanup(support.rmtree, ABSTFN)
 
         test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
-        test_dir_short = ntpath.join(ABSTFN, "MYVERY~1")
+        os.mkdir(test_dir_long)
+
+        test_dir_short = _getshortpathname(test_dir_long)
         test_file_long = ntpath.join(test_dir_long, "file.txt")
         test_file_short = ntpath.join(test_dir_short, "file.txt")
 
-        os.mkdir(test_dir_long)
-
         with open(test_file_long, "wb") as f:
             f.write(b"content")
 



More information about the Python-checkins mailing list