From a8d4f066ae15da7277c34377768a4ecfea48241d Mon Sep 17 00:00:00 2001 From: Zaid Date: Mon, 16 Nov 2026 01:37:20 +0530 Subject: [PATCH] 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. --- Lib/ntpath.py | 15 ++++++-- Lib/test/test_ntpath.py | 38 ++++++++++++++++++- ...-11-15-20-06-52.gh-issue-135122.uchDVu.rst | 4 ++ 3 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-11-15-20-06-52.gh-issue-135122.uchDVu.rst diff --git a/Lib/ntpath.py b/Lib/ntpath.py index 0aa6cb7c52f2c69..2ecbc7e42c71fbc 100644 --- a/Lib/ntpath.py +++ b/Lib/ntpath.py @@ -17,6 +17,7 @@ defpath = '.;C:\\bin' devnull = 'nul' +import errno import os import sys import genericpath @@ -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 @@ -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 @@ -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. diff --git a/Lib/test/test_ntpath.py b/Lib/test/test_ntpath.py index 96fce534ba75d3e..4abdea5bc42079d 100644 --- a/Lib/test/test_ntpath.py +++ b/Lib/test/test_ntpath.py @@ -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 @@ -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") @@ -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") diff --git a/Misc/NEWS.d/next/Library/2026-11-15-20-06-52.gh-issue-135122.uchDVu.rst b/Misc/NEWS.d/next/Library/2026-11-15-20-06-52.gh-issue-135122.uchDVu.rst new file mode 100644 index 000000000000000..f16e7e11c552eee --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-15-20-06-52.gh-issue-135122.uchDVu.rst @@ -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.