From 74f10d8b43a498eff6de2e1cd3a275b8ca0dac7f Mon Sep 17 00:00:00 2001 From: kabilanpichaipillai Date: Sun, 22 Nov 2026 02:28:41 -0400 Subject: [PATCH 1/4] Extract pipe re-opening from FakeFileOpen.call into a method Move the branch that wraps an already open fake pipe descriptor unchanged into _open_pipe_wrapper(), shortening call(). --- pyfakefs/fake_open.py | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/pyfakefs/fake_open.py b/pyfakefs/fake_open.py index b3f8e534..021aa655 100644 --- a/pyfakefs/fake_open.py +++ b/pyfakefs/fake_open.py @@ -212,26 +212,8 @@ def call( file_ ) if file_object is None and file_path is None: - # file must be a fake pipe wrapper, find it... - if ( - filedes is None - or len(self.filesystem.open_files) <= filedes - or not self.filesystem.open_files[filedes] - ): - raise OSError(errno.EBADF, "invalid pipe file descriptor") - wrappers = self.filesystem.open_files[filedes] - assert wrappers is not None - existing_wrapper = wrappers[0] - assert isinstance(existing_wrapper, FakePipeWrapper) - wrapper = FakePipeWrapper( - self.filesystem, - existing_wrapper.fd, - existing_wrapper.can_write, - mode, - ) - file_des = self.filesystem.add_open_file(wrapper) - wrapper.filedes = file_des - return wrapper + # file must be a fake pipe wrapper + return self._open_pipe_wrapper(filedes, mode) assert file_path is not None if not filedes: @@ -299,6 +281,27 @@ def call( fakefile.filedes = self.filesystem.add_open_file(fakefile) return fakefile + def _open_pipe_wrapper(self, filedes: int | None, mode: str) -> FakePipeWrapper: + """Return a new wrapper for the fake pipe open under `filedes`.""" + if ( + filedes is None + or len(self.filesystem.open_files) <= filedes + or not self.filesystem.open_files[filedes] + ): + raise OSError(errno.EBADF, "invalid pipe file descriptor") + wrappers = self.filesystem.open_files[filedes] + assert wrappers is not None + existing_wrapper = wrappers[0] + assert isinstance(existing_wrapper, FakePipeWrapper) + wrapper = FakePipeWrapper( + self.filesystem, + existing_wrapper.fd, + existing_wrapper.can_write, + mode, + ) + wrapper.filedes = self.filesystem.add_open_file(wrapper) + return wrapper + @staticmethod def _open_flags_from_open_modes(open_modes: _OpenModes) -> int: flags = 0 From 7847611bb6107a79d5280c5fef6de8f22dcb7ab2 Mon Sep 17 00:00:00 2001 From: kabilanpichaipillai Date: Sun, 22 Nov 2026 02:30:49 -0400 Subject: [PATCH 2/4] Decompose permission check in FakeFileOpen._init_file_object Move the nested read/write permission condition into the new _is_access_denied() helper so the caller reads as a single check. --- pyfakefs/fake_open.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/pyfakefs/fake_open.py b/pyfakefs/fake_open.py index 021aa655..68e41f47 100644 --- a/pyfakefs/fake_open.py +++ b/pyfakefs/fake_open.py @@ -331,16 +331,8 @@ def _init_file_object( check_file_permission: bool, ) -> FakeFile: if file_object: - if ( - check_file_permission - and not is_root() - and ( - (open_modes.can_read and not file_object.has_permission(PERM_READ)) - or ( - open_modes.can_write - and not file_object.has_permission(PERM_WRITE) - ) - ) + if check_file_permission and self._is_access_denied( + file_object, open_modes ): self.filesystem.raise_os_error(errno.EACCES, file_path) if open_modes.can_write and open_modes.truncate: @@ -370,6 +362,16 @@ def _init_file_object( ) return file_object + @staticmethod + def _is_access_denied(file_object: FakeFile, open_modes: _OpenModes) -> bool: + """Return `True` if the current user lacks the permissions needed + to open `file_object` with the given modes.""" + if is_root(): + return False + if open_modes.can_read and not file_object.has_permission(PERM_READ): + return True + return open_modes.can_write and not file_object.has_permission(PERM_WRITE) + def _handle_file_arg( self, file_: AnyStr | int ) -> tuple[FakeFile | None, AnyStr | None, int | None, AnyStr | None, bool]: From 30ac804c800dd9043c19d2b26666898d18b280b8 Mon Sep 17 00:00:00 2001 From: kabilanpichaipillai Date: Sun, 22 Nov 2026 02:32:17 -0400 Subject: [PATCH 3/4] Introduce explaining variable for nested Patcher argument check Name the nine-part condition in Patcher.__init__ as has_custom_arguments so its intent is clear at the call site. --- pyfakefs/fake_filesystem_unittest.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index 9dba8157..c29db715 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -667,7 +667,7 @@ def __init__( if self.DOC_REF_COUNT > 0: return elif self.REF_COUNT > 0: - if ( + has_custom_arguments = bool( additional_skip_names or modules_to_reload or modules_to_patch @@ -677,7 +677,8 @@ def __init__( or patch_default_args or not use_cache or not use_dynamic_patch - ): + ) + if has_custom_arguments: warnings.warn( "Nested fake filesystem invocation using custom arguments - " "using the existing fake filesystem, discarding custom arguments!" From 3c26b23ac153811412ede845875d82967c634f44 Mon Sep 17 00:00:00 2001 From: kabilanpichaipillai Date: Sun, 22 Nov 2026 02:34:02 -0400 Subject: [PATCH 4/4] Rename Patcher._isStale to _is_stale Use snake_case like the surrounding Patcher attributes (PEP 8). --- pyfakefs/fake_filesystem_unittest.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index c29db715..f65bfd39 100644 --- a/pyfakefs/fake_filesystem_unittest.py +++ b/pyfakefs/fake_filesystem_unittest.py @@ -723,8 +723,8 @@ def __init__( self.fake_modules: dict[str, Any] = {} self.unfaked_modules: dict[str, Any] = {} - # _isStale is set by tearDown(), reset by _refresh() - self._isStale = True + # _is_stale is set by tearDown(), reset by _refresh() + self._is_stale = True self._dyn_patcher: DynamicPatcher | None = None self._patching = False self._paused = False @@ -1023,7 +1023,7 @@ def _find_modules(self) -> None: self.__class__.CACHED_MODULES.add(module) def _refresh(self) -> None: - """Renew the fake file system and set the _isStale flag to `False`.""" + """Renew the fake file system and set the _is_stale flag to `False`.""" if self._stubs is not None: self._stubs.smart_unset_all() self._stubs = mox3_stubout.StubOutForTesting() @@ -1043,7 +1043,7 @@ def _refresh(self) -> None: for name in self._unfaked_module_classes: self.unfaked_modules[name] = self._unfaked_module_classes[name]() - self._isStale = False + self._is_stale = False def setUp(self, doctester: Any = None) -> None: """Bind the file-related modules to the :py:mod:`pyfakefs` fake @@ -1160,7 +1160,7 @@ def patch_defaults(self) -> None: def replace_globs(self, globs_: dict[str, Any]) -> dict[str, Any]: globs = globs_.copy() - if self._isStale: + if self._is_stale: self._refresh() for name in self._fake_module_classes: if name in globs: @@ -1187,7 +1187,7 @@ def tearDown(self, doctester: Any = None): def stop_patching(self, temporary=False) -> None: if self._patching: - self._isStale = True + self._is_stale = True self._patching = False self._paused = temporary if self._stubs: