Skip to content

Commit ebc5b97

Browse files
committed
Extract the filename from the topmost frame of the stack.
1 parent 4ea81bf commit ebc5b97

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

importlib_resources/_common.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,24 +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-
9690
def _infer_caller():
9791
"""
9892
Walk the stack and find the frame of the first caller not in this module.
9993
"""
10094

10195
def is_this_file(frame_info):
102-
return frame_info.filename == _this_filename()
96+
return frame_info.filename == stack[0].filename
10397

10498
def is_wrapper(frame_info):
10599
return frame_info.function == 'wrapper'
106100

107-
not_this_file = itertools.filterfalse(is_this_file, inspect.stack())
101+
stack = inspect.stack()
102+
not_this_file = itertools.filterfalse(is_this_file, stack)
108103
# also exclude 'wrapper' due to singledispatch in the call stack
109104
callers = itertools.filterfalse(is_wrapper, not_this_file)
110105
return next(callers).frame

0 commit comments

Comments
 (0)