Skip to content

Commit 4ea81bf

Browse files
committed
Extract a function for computing 'this filename' once.
1 parent cba8dce commit 4ea81bf

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

importlib_resources/_common.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,18 +87,19 @@ def _(cand: None) -> types.ModuleType:
8787
return resolve(_infer_caller().f_globals['__name__'])
8888

8989

90+
@functools.lru_cache
91+
def _this_filename():
92+
frame = inspect.currentframe()
93+
return __file__ if frame is None else inspect.getframeinfo(frame).filename
94+
95+
9096
def _infer_caller():
9197
"""
9298
Walk the stack and find the frame of the first caller not in this module.
9399
"""
94-
this_frame = inspect.currentframe()
95-
if this_frame is None:
96-
this_file = __file__
97-
else:
98-
this_file = inspect.getframeinfo(this_frame).filename
99100

100101
def is_this_file(frame_info):
101-
return frame_info.filename == this_file
102+
return frame_info.filename == _this_filename()
102103

103104
def is_wrapper(frame_info):
104105
return frame_info.function == 'wrapper'

0 commit comments

Comments
 (0)