Skip to content

Extract OpenFileTable from FakeFilesystem - #1380

Merged
mrbean-bremen merged 6 commits into
pytest-dev:mainfrom
kabilanpichaipillai:refactor-extract-open-file-table
Nov 24, 2026
Merged

mrbean-bremen merged 6 commits into
pytest-dev:mainfrom
kabilanpichaipillai:refactor-extract-open-file-table

Conversation

@kabilanpichaipillai

Copy link
Copy Markdown
Contributor

This PR extracts the open file descriptor bookkeeping from FakeFilesystem into a separate OpenFileTable class. Since FakeFilesystem is already quite large, this helps separate the file descriptor management logic into a smaller class and makes it easier to maintain.

  • Moved the open file tracking, free-fd heap and related logic into OpenFileTable.
  • Kept the existing FakeFilesystem methods as forwarders to maintain compatibility.
  • Added a read/write open_files property to preserve existing access to the file list.
  • Moved FakeFileWrapper._is_open() into OpenFileTable.is_open() and extracted the shared removal logic into OpenFileTable.remove().

Added 11 unit tests for OpenFileTable. The existing tests pass locally on Windows without any modifications, and pre-commit checks also pass.
This PR is part of the same university refactoring assignment as #1378 and #1379. I'm happy to move the class into a separate module, rename it, or make other adjustments if you prefer a different approach.

kabilanpichaipillai and others added 3 commits November 22, 2026 10:56
Move the open file descriptor list, the free descriptor heap and the
logic to add, close and look up open files into a separate class.
FakeFilesystem keeps its existing methods and the open_files attribute
as thin wrappers, so its interface does not change. Add unit tests for
the new class.
FakeFileWrapper._is_open() only inspected the open file table, so move
it there as OpenFileTable.is_open(). Also move the code removing a
wrapper from a shared descriptor, duplicated in FakeFileWrapper and
FakePipeWrapper, into OpenFileTable.remove().
Comment thread pyfakefs/fake_filesystem.py Resolved
Comment thread pyfakefs/fake_filesystem.py Resolved
Comment on lines +194 to +196
self.open_files: list[list[AnyFileWrapper] | None] = []
# A heap containing all free positions in self.open_files list
self._free_fd_heap: list[int] = []

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that the heap variable is no longer in the FakeFilesystem class - that was ugly from the beginning. Generally a helpful refactoring.

kabilanpichaipillai and others added 3 commits November 23, 2026 18:25
Make open_files read-only, as suggested in review. The pickle test now
resets the table with open_file_table.clear() instead.
Move the descriptor checks into OpenFileTable.get() and drop the
undocumented get_open_files(), as suggested in review.

@mrbean-bremen mrbean-bremen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mrbean-bremen
mrbean-bremen merged commit 30fc7d6 into pytest-dev:main Nov 24, 2026
103 of 104 checks passed
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.

2 participants