Skip to content

Commit a8d4f06

Browse files
committed
gh-135122: Raise NotADirectoryError in ntpath.realpath(strict=ALLOW_MISSING)
On Windows, a path whose non-final component is an existing file fails with ERROR_PATH_NOT_FOUND, which is a FileNotFoundError and was ignored in ALLOW_MISSING mode, so realpath() returned a path that can never be created. Check that the deepest existing prefix is a directory and raise NotADirectoryError otherwise, as posixpath.realpath() does.
1 parent 369d8bc commit a8d4f06

3 files changed

Lines changed: 51 additions & 6 deletions

File tree

‎Lib/ntpath.py‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
defpath = '.;C:\\bin'
1818
devnull = 'nul'
1919

20+
import errno
2021
import os
2122
import sys
2223
import genericpath
@@ -613,7 +614,7 @@ def _readlink_deep(path, ignored_error=OSError):
613614
break
614615
return path
615616

616-
def _getfinalpathname_nonstrict(path, ignored_error=OSError):
617+
def _getfinalpathname_nonstrict(path, ignored_error=OSError, strict=False):
617618
# These error codes indicate that we should stop resolving the path
618619
# and return the value we currently have.
619620
# 1: ERROR_INVALID_FUNCTION
@@ -650,8 +651,13 @@ def join(path, tail):
650651
tail = path[:0]
651652
while path:
652653
try:
653-
path = _getfinalpathname(path)
654-
return join(path, tail)
654+
final_path = _getfinalpathname(path)
655+
# gh-135122: Windows reports ERROR_PATH_NOT_FOUND when a
656+
# non-final component is a file, as if it were missing.
657+
if strict and tail and not isdir(final_path):
658+
raise NotADirectoryError(errno.ENOTDIR,
659+
os.strerror(errno.ENOTDIR), path)
660+
return join(final_path, tail)
655661
except ignored_error as ex:
656662
if ex.winerror not in allowed_winerror:
657663
raise
@@ -735,7 +741,8 @@ def realpath(path, /, *, strict=False):
735741
raise
736742
initial_winerror = ex.winerror
737743
path = _getfinalpathname_nonstrict(path,
738-
ignored_error=ignored_error)
744+
ignored_error=ignored_error,
745+
strict=strict)
739746
# The path returned by _getfinalpathname will always start with \\?\ -
740747
# strip off that prefix unless it was already provided on the original
741748
# path.

‎Lib/test/test_ntpath.py‎

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,38 @@ def test_realpath_strict(self):
594594
self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN, strict=True)
595595
self.assertRaises(FileNotFoundError, ntpath.realpath, ABSTFN + "2", strict=True)
596596

597+
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
598+
def test_realpath_allow_missing_not_a_directory(self):
599+
# gh-135122: A file used as an intermediate directory is an error in
600+
# ALLOW_MISSING mode, as on POSIX, not a missing path.
601+
realpath = ntpath.realpath
602+
ABSTFN = ntpath.abspath(os_helper.TESTFN)
603+
ABSTFNb = os.fsencode(ABSTFN)
604+
self.addCleanup(os_helper.rmtree, ABSTFN)
605+
os.mkdir(ABSTFN)
606+
os.mkdir(ABSTFN + "\\dir")
607+
open(ABSTFN + "\\file", "wb").close()
608+
609+
for path in (ABSTFN + "\\file\\file2",
610+
ABSTFN + "\\file\\file2\\file3",
611+
ABSTFNb + b"\\file\\file2",
612+
ABSTFNb + b"\\file\\file2\\file3"):
613+
with self.subTest(path=path):
614+
self.assertRaises(NotADirectoryError, realpath, path,
615+
strict=ALLOW_MISSING)
616+
self.assertRaises(FileNotFoundError, realpath, path,
617+
strict=ALL_BUT_LAST)
618+
self.assertRaises(FileNotFoundError, realpath, path,
619+
strict=True)
620+
self.assertPathEqual(realpath(path, strict=False), path)
621+
622+
# Missing paths under an existing directory are still allowed.
623+
for path in (ABSTFN + "\\dir\\file2\\file3",
624+
ABSTFNb + b"\\dir\\file2\\file3",
625+
ABSTFN + "\\nonexistent\\file2"):
626+
with self.subTest(path=path):
627+
self.assertPathEqual(realpath(path, strict=ALLOW_MISSING), path)
628+
597629
@unittest.skipUnless(HAVE_GETFINALPATHNAME, 'need _getfinalpathname')
598630
def test_realpath_invalid_paths(self):
599631
realpath = ntpath.realpath
@@ -1083,7 +1115,8 @@ def check(path, modes, expected, errno=None):
10831115
all_modes = [False, ALLOW_MISSING, ALL_BUT_LAST, True]
10841116
check("file", all_modes, "/file")
10851117
check("file/", all_modes, "/file")
1086-
check("file/file2", [False, ALLOW_MISSING], "/file/file2")
1118+
check("file/file2", [False], "/file/file2")
1119+
check("file/file2", [ALLOW_MISSING], NotADirectoryError)
10871120
check("file/file2", [ALL_BUT_LAST, True], FileNotFoundError)
10881121
check("file/.", all_modes, "/file")
10891122
check("file/../link2", all_modes, "/dir")
@@ -1094,7 +1127,8 @@ def check(path, modes, expected, errno=None):
10941127

10951128
check("link", all_modes, "/file")
10961129
check("link/", all_modes, "/file")
1097-
check("link/file2", [False, ALLOW_MISSING], "/file/file2")
1130+
check("link/file2", [False], "/file/file2")
1131+
check("link/file2", [ALLOW_MISSING], NotADirectoryError)
10981132
check("link/file2", [ALL_BUT_LAST, True], FileNotFoundError)
10991133
check("link/.", all_modes, "/file")
11001134
check("link/../link", all_modes, "/file")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:func:`os.path.realpath` on Windows now raises :exc:`NotADirectoryError`
2+
in :data:`os.path.ALLOW_MISSING` mode if a non-final component of the path
3+
is an existing file, as it does on POSIX, instead of returning the path
4+
as if that component were missing.

0 commit comments

Comments
 (0)