From 6f602fbd3568ed7bebadfa3c7d3de40a271216f9 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Fri, 15 Nov 2019 16:21:27 -0800 Subject: [PATCH] bpo-38453: Ensure correct short path is obtained for test (GH-17184) (cherry picked from commit 7c6130c8c36c941255365e5414c956fc919b8629) Co-authored-by: Steve Dower --- Lib/test/test_ntpath.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index a84b94c9bad..4a02db28172 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -22,6 +22,23 @@ except AttributeError: 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 @@ class TestNtpath(NtpathTestCase): 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 @@ class TestNtpath(NtpathTestCase): 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")