bpo-38453: Ensure correct short path is obtained for test (GH-17184)
(cherry picked from commit 7c6130c8c3
)
Co-authored-by: Steve Dower <steve.dower@python.org>
This commit is contained in:
parent
66c0f01f98
commit
6f602fbd35
|
@ -22,6 +22,23 @@ except AttributeError:
|
||||||
else:
|
else:
|
||||||
HAVE_GETFINALPATHNAME = True
|
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):
|
def _norm(path):
|
||||||
if isinstance(path, (bytes, str, os.PathLike)):
|
if isinstance(path, (bytes, str, os.PathLike)):
|
||||||
|
@ -403,6 +420,7 @@ class TestNtpath(NtpathTestCase):
|
||||||
tester("ntpath.realpath('NUL')", r'\\.\NUL')
|
tester("ntpath.realpath('NUL')", r'\\.\NUL')
|
||||||
|
|
||||||
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
|
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
|
||||||
|
@unittest.skipUnless(HAVE_GETSHORTPATHNAME, 'need _getshortpathname')
|
||||||
def test_realpath_cwd(self):
|
def test_realpath_cwd(self):
|
||||||
ABSTFN = ntpath.abspath(support.TESTFN)
|
ABSTFN = ntpath.abspath(support.TESTFN)
|
||||||
|
|
||||||
|
@ -412,12 +430,12 @@ class TestNtpath(NtpathTestCase):
|
||||||
self.addCleanup(support.rmtree, ABSTFN)
|
self.addCleanup(support.rmtree, ABSTFN)
|
||||||
|
|
||||||
test_dir_long = ntpath.join(ABSTFN, "MyVeryLongDirectoryName")
|
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_long = ntpath.join(test_dir_long, "file.txt")
|
||||||
test_file_short = ntpath.join(test_dir_short, "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:
|
with open(test_file_long, "wb") as f:
|
||||||
f.write(b"content")
|
f.write(b"content")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue