diff options
author | Steve Dower <steve.dower@python.org> | 2019-08-22 00:23:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-22 00:23:38 (GMT) |
commit | a50d2f7e199f3be60c70c1586ee60ec60bf36642 (patch) | |
tree | 58fcc59f88f2f4222d5d50caaab994e0cd57a5f2 | |
parent | f93c15aedc2ea2cb8b56fc9dbb0d412918992e86 (diff) | |
download | cpython-a50d2f7e199f3be60c70c1586ee60ec60bf36642.zip cpython-a50d2f7e199f3be60c70c1586ee60ec60bf36642.tar.gz cpython-a50d2f7e199f3be60c70c1586ee60ec60bf36642.tar.bz2 |
bpo-9949: Call normpath() in realpath() and avoid unnecessary prefixes (GH-15376)
-rw-r--r-- | Lib/ntpath.py | 11 | ||||
-rw-r--r-- | Lib/test/test_ntpath.py | 4 |
2 files changed, 10 insertions, 5 deletions
diff --git a/Lib/ntpath.py b/Lib/ntpath.py index ef4999e..1d22d5f 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -458,7 +458,8 @@ def normpath(path): # in the case of paths with these prefixes: # \\.\ -> device names # \\?\ -> literal paths - # do not do any normalization, but return the path unchanged + # do not do any normalization, but return the path + # unchanged apart from the call to os.fspath() return path path = path.replace(altsep, sep) prefix, path = splitdrive(path) @@ -575,7 +576,7 @@ else: return abspath(tail) def realpath(path): - path = os.fspath(path) + path = normpath(path) if isinstance(path, bytes): prefix = b'\\\\?\\' unc_prefix = b'\\\\?\\UNC\\' @@ -586,6 +587,7 @@ else: unc_prefix = '\\\\?\\UNC\\' new_unc_prefix = '\\\\' cwd = os.getcwd() + did_not_exist = not exists(path) had_prefix = path.startswith(prefix) path = _getfinalpathname_nonstrict(path) # The path returned by _getfinalpathname will always start with \\?\ - @@ -603,7 +605,10 @@ else: if _getfinalpathname(spath) == path: path = spath except OSError as ex: - pass + # If the path does not exist and originally did not exist, then + # strip the prefix anyway. + if ex.winerror in {2, 3} and did_not_exist: + path = spath return path diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 74dc8c3..285fb69 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -333,11 +333,11 @@ class TestNtpath(unittest.TestCase): self.assertEqual(ntpath.realpath(ABSTFN + "1\\.."), ntpath.dirname(ABSTFN)) self.assertEqual(ntpath.realpath(ABSTFN + "1\\..\\x"), - ntpath.dirname(P + ABSTFN) + "\\x") + ntpath.dirname(ABSTFN) + "\\x") os.symlink(ABSTFN + "x", ABSTFN + "y") self.assertEqual(ntpath.realpath(ABSTFN + "1\\..\\" + ntpath.basename(ABSTFN) + "y"), - P + ABSTFN + "x") + ABSTFN + "x") self.assertIn(ntpath.realpath(ABSTFN + "1\\..\\" + ntpath.basename(ABSTFN) + "1"), expected) |