Skip to content

Fix os.path.sameopenfile for fake file descriptors - #1349

Open
anshurajbisoyi98-ctrl wants to merge 3 commits into
pytest-dev:mainfrom
anshurajbisoyi98-ctrl:codex/fix-sameopenfile
Open

anshurajbisoyi98-ctrl wants to merge 3 commits into
pytest-dev:mainfrom
anshurajbisoyi98-ctrl:codex/fix-sameopenfile

Conversation

@anshurajbisoyi98-ctrl

@anshurajbisoyi98-ctrl anshurajbisoyi98-ctrl commented Oct 2, 2026 •

Copy link
Copy Markdown

Describe the changes

Fixes #1346.

os.path.sameopenfile() currently calls the real filesystem's fstat() with fake file descriptors. Comparing two handles to the same fake file therefore raises EBADF, or compares unrelated real descriptors when those numbers happen to exist.

Register sameopenfile for patching and compare fake fstat() results through the existing samestat() helper. Invalid descriptors continue to raise OSError. The regression checks matching and different files, a descriptor compared with itself, and closed descriptors under Linux, macOS, and Windows simulation.

Testing

Codex ran these checks locally on macOS arm64 with Python 3.13.12:

  • python -m pyfakefs.tests.all_tests: 2,413 tests run; OK with 973 skips and 2 expected failures, with the extra dependency group installed.
  • pytest pyfakefs/pytest_tests -q: 34 passed, 1 skipped, 2 xpassed.
  • Namespace-package pytest test: 1 passed.
  • pre-commit run --all-files: all hooks passed, including Ruff and mypy; hooks used the configured Python 3.10 runtime.
  • The new regression failed for all three simulated operating systems before the fix and passed afterward.
  • git diff --check: clean.

With TEST_REAL_FS=1, the suite has one failure in RealOsModuleTest.test_lexists_with_trailing_separator_macos. The identical failure occurs in a clean checkout of upstream commit 796c48e; it concerns real macOS symlink behavior and is outside this change. Native Windows/Linux, root-user runs, the full Python-version CI matrix, and CI's Linux pytype job were not run locally.

Tasks

  • Unit tests added that reproduce the issue or prove feature is working
  • Fix or feature added
  • Entry to release notes added
  • Pre-commit CI shows no errors (pending CI; all local hooks pass)
  • Unit tests passing (fake-filesystem suite; real-filesystem exception disclosed above)
  • Documentation preview: N/A; no documentation-page changes.

AI usage

I used Codex for investigation, implementation, tests. Codex performed the verification listed above.

I kept the fix in the existing path adapter, using fake filesystem metadata to compare open descriptors. The regression tests cover matching files, different files, and closed descriptors across all three simulated operating systems.

AI assistance

I used OpenAI Codex to help investigate, implement, and check parts of this change. I take responsibility for the submitted changes.

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.sameopenfile doesn't work with files in the fake filesystem

1 participant