Skip to content

Commit d937d13

Browse files
committed
Use is_called_from_skipped_module in fake_io and fake_open too
1 parent 35c8eec commit d937d13

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

pyfakefs/fake_io.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
from pyfakefs.fake_file import AnyFileWrapper
3535
from pyfakefs.fake_open import fake_open
36-
from pyfakefs.helpers import IS_PYPY
36+
from pyfakefs.helpers import IS_PYPY, is_called_from_skipped_module
3737

3838
if TYPE_CHECKING:
3939
from pyfakefs.fake_filesystem import FakeFilesystem
@@ -180,6 +180,14 @@ def lockf(
180180
) -> Any:
181181
pass
182182

183-
def __getattr__(self, name):
183+
def __getattribute__(self, name):
184184
"""Forwards any unfaked calls to the standard fcntl module."""
185-
return getattr(self._fcntl_module, name)
185+
filesystem = object.__getattribute__(self, "filesystem")
186+
fnctl_module = object.__getattribute__(self, "_fcntl_module")
187+
if filesystem.patcher:
188+
skip_names = filesystem.patcher._skip_names
189+
if is_called_from_skipped_module(skip_names=skip_names):
190+
# remove the `self` argument for FakeOsModule methods
191+
return getattr(fnctl_module, name)
192+
193+
return object.__getattribute__(self, name)

pyfakefs/fake_open.py

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
)
4545
from pyfakefs.helpers import (
4646
AnyString,
47+
is_called_from_skipped_module,
4748
is_root,
4849
PERM_READ,
4950
PERM_WRITE,
@@ -83,23 +84,7 @@ def fake_open(
8384
"""Redirect the call to FakeFileOpen.
8485
See FakeFileOpen.call() for description.
8586
"""
86-
# workaround for built-in open called from skipped modules (see #552)
87-
# as open is not imported explicitly, we cannot patch it for
88-
# specific modules; instead we check if the caller is a skipped
89-
# module (should work in most cases)
90-
stack = traceback.extract_stack(limit=3)
91-
# handle the case that we try to call the original `open_code`
92-
# and get here instead (since Python 3.12)
93-
from_open_code = (
94-
sys.version_info >= (3, 12)
95-
and stack[0].name == "open_code"
96-
and stack[0].line == "return self._io_module.open_code(path)"
97-
)
98-
module_name = os.path.splitext(stack[0].filename)[0]
99-
module_name = module_name.replace(os.sep, ".")
100-
if from_open_code or any(
101-
[module_name == sn or module_name.endswith("." + sn) for sn in skip_names]
102-
):
87+
if is_called_from_skipped_module(skip_names=skip_names):
10388
return io.open( # pytype: disable=wrong-arg-count
10489
file,
10590
mode,

pyfakefs/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ def is_called_from_skipped_module(skip_names: list) -> bool:
449449
(
450450
frame.filename
451451
for frame in stack[::-1]
452-
if not frame.filename.startswith("<frozen importlib")
452+
if not frame.filename.startswith("<frozen ")
453453
and not frame.filename.startswith(STDLIB_PATH)
454454
and (
455455
not frame.filename.startswith(PYFAKEFS_PATH)

0 commit comments

Comments
 (0)