diff --git a/pyfakefs/fake_filesystem_unittest.py b/pyfakefs/fake_filesystem_unittest.py index 9dba8157..f65bfd39 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!" @@ -722,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 @@ -1022,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() @@ -1042,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 @@ -1159,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: @@ -1186,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: diff --git a/pyfakefs/fake_open.py b/pyfakefs/fake_open.py index b3f8e534..68e41f47 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 @@ -328,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: @@ -367,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]: