Skip to content

Commit 7c6130c

Browse files
authored
bpo-38453: Ensure correct short path is obtained for test (GH-17184)
1 parent b5e170f commit 7c6130c

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

Lib/test/test_ntpath.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@
2222
else:
2323
HAVE_GETFINALPATHNAME = True
2424

25+
try:
26+
import ctypes
27+
except ImportError:
28+
HAVE_GETSHORTPATHNAME = False
29+
else:
30+
HAVE_GETSHORTPATHNAME = True
31+
def _getshortpathname(path):
32+
GSPN = ctypes.WinDLL("kernel32", use_last_error=True).GetShortPathNameW
33+
GSPN.argtypes = [ctypes.c_wchar_p, ctypes.c_wchar_p, ctypes.c_uint32]
34+
GSPN.restype = ctypes.c_uint32
35+
result_len = GSPN(path, None, 0)
36+
if not result_len:
37+
raise OSError("failed to get short path name 0x{:08X}"
38+
.format(ctypes.get_last_error()))
39+
result = ctypes.create_unicode_buffer(result_len)
40+
result_len = GSPN(path, result, result_len)
41+
return result[:result_len]
2542

2643
def _norm(path):
2744
if isinstance(path, (bytes, str, os.PathLike)):
@@ -403,6 +420,7 @@ def test_realpath_nul(self):
403420
tester("ntpath.realpath('NUL')", r'\\.\NUL')
404421

405422
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
423+
@unittest.skipUnless(HAVE_GETSHORTPATHNAME, 'need _getshortpathname')
406424
def test_realpath_cwd(self):
407425
ABSTFN = ntpath.abspath(support.TESTFN)
408426

@@ -412,12 +430,12 @@ def test_realpath_cwd(self):
412430
self.addCleanup(support.rmtree, ABSTFN)
413431

414432
test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
415-
test_dir_short = ntpath.join(ABSTFN, "MYVERY~1")
433+
os.mkdir(test_dir_long)
434+
435+
test_dir_short = _getshortpathname(test_dir_long)
416436
test_file_long = ntpath.join(test_dir_long, "file.txt")
417437
test_file_short = ntpath.join(test_dir_short, "file.txt")
418438

419-
os.mkdir(test_dir_long)
420-
421439
with open(test_file_long, "wb") as f:
422440
f.write(b"content")
423441

0 commit comments

Comments
 (0)