Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Lib/ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
defpath = '.;C:\\bin'
devnull = 'nul'

import errno
import os
import sys
import genericpath
Expand Down Expand Up @@ -613,7 +614,7 @@ def _readlink_deep(path, ignored_error=OSError):
break
return path

def _getfinalpathname_nonstrict(path, ignored_error=OSError):
def _getfinalpathname_nonstrict(path, ignored_error=OSError, strict=False):
# These error codes indicate that we should stop resolving the path
# and return the value we currently have.
# 1: ERROR_INVALID_FUNCTION
Expand Down Expand Up @@ -650,8 +651,13 @@ def join(path, tail):
tail = path[:0]
while path:
try:
path = _getfinalpathname(path)
return join(path, tail)
final_path = _getfinalpathname(path)
# gh-135122: Windows reports ERROR_PATH_NOT_FOUND when a
# non-final component is a file, as if it were missing.
if strict and tail and not isdir(final_path):
raise NotADirectoryError(errno.ENOTDIR,
os.strerror(errno.ENOTDIR), path)
return join(final_path, tail)
except ignored_error as ex:
if ex.winerror not in allowed_winerror:
raise
Expand Down Expand Up @@ -735,7 +741,8 @@ def realpath(path, /, *, strict=False):
raise
initial_winerror = ex.winerror
path = _getfinalpathname_nonstrict(path,
ignored_error=ignored_error)
ignored_error=ignored_error,
strict=strict)
# The path returned by _getfinalpathname will always start with \\?\ -
# strip off that prefix unless it was already provided on the original
# path.
Expand Down
38 changes: 36 additions & 2 deletions Lib/test/test_ntpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,38 @@ def test_realpath_strict(self):
self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN, strict=True)
self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN + "2", strict=True)

@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_allow_missing_not_a_directory(self):
# gh-135122: A file used as an intermediate directory is an error in
# ALLOW_MISSING mode, as on POSIX, not a missing path.
realpath = ntpath.realpath
ABSTFN = ntpath.abspath(os_helper.TESTFN)
ABSTFNb = os.fsencode(ABSTFN)
self.addCleanup(os_helper.rmtree, ABSTFN)
os.mkdir(ABSTFN)
os.mkdir(ABSTFN + "\\dir")
open(ABSTFN + "\\file", "wb").close()

for path in (ABSTFN + "\\file\\file2",
ABSTFN + "\\file\\file2\\file3",
ABSTFNb + b"\\file\\file2",
ABSTFNb + b"\\file\\file2\\file3"):
with self.subTest(path=path):
self.assertRaises(NotADirectoryError, realpath, path,
strict=ALLOW_MISSING)
self.assertRaises(FileNotFoundError, realpath, path,
strict=ALL_BUT_LAST)
self.assertRaises(FileNotFoundError, realpath, path,
strict=True)
self.assertPathEqual(realpath(path, strict=False), path)

# Missing paths under an existing directory are still allowed.
for path in (ABSTFN + "\\dir\\file2\\file3",
ABSTFNb + b"\\dir\\file2\\file3",
ABSTFN + "\\nonexistent\\file2"):
with self.subTest(path=path):
self.assertPathEqual(realpath(path, strict=ALLOW_MISSING), path)

@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
def test_realpath_invalid_paths(self):
realpath = ntpath.realpath
Expand Down Expand Up @@ -1083,7 +1115,8 @@ def check(path, modes, expected, errno=None):
all_modes = [False, ALLOW_MISSING, ALL_BUT_LAST, True]
check("file", all_modes, "/file")
check("file/", all_modes, "/file")
check("file/file2", [False, ALLOW_MISSING], "/file/file2")
check("file/file2", [False], "/file/file2")
check("file/file2", [ALLOW_MISSING], NotADirectoryError)
check("file/file2", [ALL_BUT_LAST, True], FileNotFoundError)
check("file/.", all_modes, "/file")
check("file/../link2", all_modes, "/dir")
Expand All @@ -1094,7 +1127,8 @@ def check(path, modes, expected, errno=None):

check("link", all_modes, "/file")
check("link/", all_modes, "/file")
check("link/file2", [False, ALLOW_MISSING], "/file/file2")
check("link/file2", [False], "/file/file2")
check("link/file2", [ALLOW_MISSING], NotADirectoryError)
check("link/file2", [ALL_BUT_LAST, True], FileNotFoundError)
check("link/.", all_modes, "/file")
check("link/../link", all_modes, "/file")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
:func:`os.path.realpath` on Windows now raises :exc:`NotADirectoryError`
in :data:`os.path.ALLOW_MISSING` mode if a non-final component of the path
is an existing file, as it does on POSIX, instead of returning the path
as if that component were missing.
Loading