Skip to content

Fix path splitting functions for the simulated file system - #1350

Open
wanjiang263 wants to merge 2 commits into
pytest-dev:mainfrom
wanjiang263:fix-windows-fakepath-basename-splitext-commonpath
Open

wanjiang263 wants to merge 2 commits into
pytest-dev:mainfrom
wanjiang263:fix-windows-fakepath-basename-splitext-commonpath

Conversation

@wanjiang263

Copy link
Copy Markdown

Summary

Fixes #1348.

FakePathModule overrides the os.path functions that depend on the simulated
file system, but basename, splitext and commonpath were not among them.
They fell through __getattr__ to _OS_PATH_COPY, a copy of the host
os.path, so under Windows simulation on a POSIX host they applied POSIX
separator rules.

This adds the three functions, dispatching to ntpath or posixpath based on
filesystem.is_windows_fs — the pattern fake_pathlib.py already uses. Results
are exactly what the matching stdlib module produces on every Python version,
rather than a reimplementation of e.g. ntpath._splitext, which has changed
across versions.

On a POSIX host simulating POSIX the behaviour is unchanged: the helper returns
posixpath, the same module __getattr__ reached before.

Test plan

  • python -bb -m pyfakefs.tests.all_tests — 2417 tests, same 5 pre-existing
    pyreadline3 doctest errors as on main
  • 6 new tests in FakePathModuleTest. Verified on a Windows host: the three
    *_posix tests fail before this change and pass after. The three
    *_windows tests cover the reported direction, which only reproduces on a
    POSIX host (CI).

wanjiang263 and others added 2 commits October 3, 2026 01:36
`FakePathModule` did not override `basename`, `splitext` and
`commonpath`, so these were forwarded to the host `os.path` module.
Under Windows simulation on a POSIX host this kept POSIX semantics and
therefore did not treat a backslash as a path separator, while other
functions like `split`, `dirname` and `normpath` were already
file-system-aware.

Add overrides that dispatch to `ntpath` or `posixpath` depending on the
simulated file system, using the same module dispatch already used by
the pathlib flavours, and register the functions in `dir()` so that
separately imported functions are patched as well.

Fixes pytest-dev#1348

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

os.path.basename, os.path.splitext and os.path.commonpath don't work under Windows simulation

1 participant