Skip to content

gh-135122: Raise NotADirectoryError in ntpath.realpath(strict=ALLOW_MISSING) - #162102

Open
zaidwhy wants to merge 1 commit into
python:mainfrom
zaidwhy:gh-135122-ntpath-realpath-allow-missing
Open

zaidwhy wants to merge 1 commit into
python:mainfrom
zaidwhy:gh-135122-ntpath-realpath-allow-missing

Conversation

@zaidwhy

@zaidwhy zaidwhy commented Nov 16, 2026 •

Copy link
Copy Markdown

On Windows, ntpath.realpath() was treating paths that go through a file as if the parent folder was missing. This happens because Windows returns ERROR_PATH_NOT_FOUND for both a missing parent and a parent that is actually a file. Since ALLOW_MISSING ignores this error, it wasn't catching this case.

I added a check to make sure the deepest existing parent is actually a directory when there's still part of the path left to resolve. This check only runs in strict modes and on paths that have already failed.

With this change, file\x, file\x\y, and link-to-file\x now raise NotADirectoryError for both str and bytes paths. This matches POSIX behaviour and what the documentation already describes.

Non-strict mode, strict=True, and ALL_BUT_LAST are unchanged.

I also updated some existing assertions in test_realpath_mode that were expecting the old behaviour. The two ALLOW_MISSING cases for file/file2 and link/file2 previously asserted the old result. test_posixpath.py already expects NotADirectoryError for these same cases. Changing the Windows tests is part of fixing the inconsistency.

This also means tarfile rejects archive paths that go through a file at the safety filter stage instead of failing later during extraction, matching POSIX behaviour.

Testing

Tested on a Windows build of main with Developer Mode enabled, so the symlink cases ran.

  • test_ntpath: 112 run, 3 skipped, 0 failures.
  • The new test and updated ALLOW_MISSING cases fail without the fix and pass with it.
  • Ran 12 related test suites covering 5,333 tests. Both versions had the same 4 unrelated failures.

AI assistance: I used AI assistance while working on this change and reviewing the implementation and tests. I reviewed and tested the final changes myself.

…LLOW_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.
@python-cla-bot

python-cla-bot Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@zaidwhy

zaidwhy commented Nov 16, 2026

Copy link
Copy Markdown
Author

The macOS Intel failure looks unrelated: the hosted runner lost communication with the server during the build step, so no tests ran. Could someone re-run that job when convenient? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant