[3.12] gh-123085: Fix issue in inferred caller when resources package has no source (GH-123102) (#124021)

gh-123085: Fix issue in inferred caller when resources package has no source.

From importlib_resources 6.4.3 (python/importlib_resourcesGH-314).
(cherry picked from commit a53812df12)
This commit is contained in:
Jason R. Coombs 2025-01-05 16:53:55 -05:00 committed by GitHub
parent 09f7d88cad
commit d712ece43f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 50 additions and 4 deletions

View file

@ -93,12 +93,13 @@ def _infer_caller():
"""
def is_this_file(frame_info):
return frame_info.filename == __file__
return frame_info.filename == stack[0].filename
def is_wrapper(frame_info):
return frame_info.function == 'wrapper'
not_this_file = itertools.filterfalse(is_this_file, inspect.stack())
stack = inspect.stack()
not_this_file = itertools.filterfalse(is_this_file, stack)
# also exclude 'wrapper' due to singledispatch in the call stack
callers = itertools.filterfalse(is_wrapper, not_this_file)
return next(callers).frame