Skip to content

Commit c60987c

Browse files
committed
Optimization: only analyze traceback if needed
1 parent 956d7af commit c60987c

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

pyfakefs/fake_open.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ def fake_open(
8989
if is_called_from_skipped_module(
9090
skip_names=skip_names,
9191
case_sensitive=filesystem.is_case_sensitive,
92+
check_open_code=sys.version_info >= (3, 12),
9293
):
9394
return io_open( # pytype: disable=wrong-arg-count
9495
file,

pyfakefs/helpers.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,12 +478,19 @@ def putvalue(self, value: bytes) -> None:
478478
self._bytestream.write(value)
479479

480480

481-
def is_called_from_skipped_module(skip_names: list, case_sensitive: bool) -> bool:
481+
def is_called_from_skipped_module(
482+
skip_names: list, case_sensitive: bool, check_open_code: bool = False
483+
) -> bool:
482484
def starts_with(path, string):
483485
if case_sensitive:
484486
return path.startswith(string)
485487
return path.lower().startswith(string.lower())
486488

489+
# in most cases we don't have skip names and won't need the overhead
490+
# of analyzing the traceback, except when checking for open_code
491+
if not skip_names and not check_open_code:
492+
return False
493+
487494
stack = traceback.extract_stack()
488495

489496
# handle the case that we try to call the original `open_code`
@@ -494,12 +501,15 @@ def starts_with(path, string):
494501
# -3: fake_io.open: 'return fake_open('
495502
# -4: fake_io.open_code : 'return self._io_module.open_code(path)'
496503
if (
497-
sys.version_info >= (3, 12)
504+
check_open_code
498505
and stack[-4].name == "open_code"
499506
and stack[-4].line == "return self._io_module.open_code(path)"
500507
):
501508
return True
502509

510+
if not skip_names:
511+
return False
512+
503513
caller_filename = next(
504514
(
505515
frame.filename

0 commit comments

Comments
 (0)