22
22
else :
23
23
HAVE_GETFINALPATHNAME = True
24
24
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 ]
25
42
26
43
def _norm (path ):
27
44
if isinstance (path , (bytes , str , os .PathLike )):
@@ -403,6 +420,7 @@ def test_realpath_nul(self):
403
420
tester ("ntpath.realpath('NUL')" , r'\\.\NUL' )
404
421
405
422
@unittest .skipUnless (HAVE_GETFINALPATHNAME , 'need _getfinalpathname' )
423
+ @unittest .skipUnless (HAVE_GETSHORTPATHNAME , 'need _getshortpathname' )
406
424
def test_realpath_cwd (self ):
407
425
ABSTFN = ntpath .abspath (support .TESTFN )
408
426
@@ -412,12 +430,12 @@ def test_realpath_cwd(self):
412
430
self .addCleanup (support .rmtree , ABSTFN )
413
431
414
432
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 )
416
436
test_file_long = ntpath .join (test_dir_long , "file.txt" )
417
437
test_file_short = ntpath .join (test_dir_short , "file.txt" )
418
438
419
- os .mkdir (test_dir_long )
420
-
421
439
with open (test_file_long , "wb" ) as f :
422
440
f .write (b"content" )
423
441
0 commit comments