8000 bpo-9949: Call normpath() in realpath() and avoid unnecessary prefixe… · python/cpython@06be2c7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06be2c7

Browse files
authored
bpo-9949: Call normpath() in realpath() and avoid unnecessary prefixes (GH-15369)
1 parent 7ebdda0 commit 06be2c7

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Lib/ntpath.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,8 @@ def normpath(path):
458458
# in the case of paths with these prefixes:
459459
# \\.\ -> device names
460460
# \\?\ -> literal paths
461-
# do not do any normalization, but return the path unchanged
461+
# do not do any normalization, but return the path
462+
# unchanged apart from the call to os.fspath()
462463
return path
463464
path = path.replace(altsep, sep)
464465
prefix, path = splitdrive(path)
@@ -575,7 +576,7 @@ def _getfinalpathname_nonstrict(path):
575576
return abspath(tail)
576577

577578
def realpath(path):
578-
path = os.fspath(path)
579+
path = normpath(path)
579580
if isinstance(path, bytes):
580581
prefix = b'\\\\?\\'
581582
unc_prefix = b'\\\\?\\UNC\\'
@@ -586,6 +587,7 @@ def realpath(path):
586587
unc_prefix = '\\\\?\\UNC\\'
587588
new_unc_prefix = '\\\\'
588589
cwd = os.getcwd()
590+
did_not_exist = not exists(path)
589591
had_prefix = path.startswith(prefix)
590592
path = _getfinalpathname_nonstrict(path)
591593
# The path returned by _getfinalpathname will always start with \\?\ -
@@ -603,7 +605,10 @@ def realpath(path):
603605
if _getfinalpathname(spath) == path:
604606
path = spath
605607
except OSError as ex:
606-
pass
608+
# If the path does not exist and originally did not exist, then
609+
# strip the prefix anyway.
610+
if ex.winerror in {2, 3} and did_not_exist:
611< 8000 /td>+
path = spath
607612
return path
608613

609614

Lib/test/test_ntpath.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,11 +333,11 @@ def test_realpath_symlink_loops(self):
333333
self.assertEqual(ntpath.realpath(ABSTFN + "1\\.."),
334334
ntpath.dirname(ABSTFN))
335335
self.assertEqual(ntpath.realpath(ABSTFN + "1\\..\\x"),
336-
ntpath.dirname(P + ABSTFN) + "\\x")
336+
ntpath.dirname(ABSTFN) + "\\x")
337337
os.symlink(ABSTFN + "x", ABSTFN + "y")
338338
self.assertEqual(ntpath.realpath(ABSTFN + "1\\..\\"
339339
+ ntpath.basename(ABSTFN) + "y"),
340-
P + ABSTFN + "x")
340+
ABSTFN + "x")
341341
self.assertIn(ntpath.realpath(ABSTFN + "1\\..\\"
342342
+ ntpath.basename(ABSTFN) + "1"),
343343
expected)

0 commit comments

Comments
 (0)
0