Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b6bd51d

Browse filesBrowse files
committed
gh-123085: Fix issue in inferred caller when resources package has no source.
From importlib_resources 6.4.3 (python/importlib_resources#314).
1 parent ac918cc commit b6bd51d
Copy full SHA for b6bd51d

File tree

3 files changed

+42
-4
lines changed
Filter options

3 files changed

+42
-4
lines changed

‎Lib/importlib/resources/_common.py

Copy file name to clipboardExpand all lines: Lib/importlib/resources/_common.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,13 @@ def _infer_caller():
9393
"""
9494

9595
def is_this_file(frame_info):
96-
return frame_info.filename == __file__
96+
return frame_info.filename == stack[0].filename
9797

9898
def is_wrapper(frame_info):
9999
return frame_info.function == 'wrapper'
100100

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

‎Lib/test/test_importlib/resources/test_files.py

Copy file name to clipboardExpand all lines: Lib/test/test_importlib/resources/test_files.py
+36-2Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import os
2+
import pathlib
3+
import py_compile
4+
import shutil
15
import textwrap
26
import unittest
37
import warnings
@@ -97,8 +101,8 @@ class ModuleFilesZipTests(DirectSpec, util.ZipSetup, ModulesFiles, unittest.Test
97101

98102
class ImplicitContextFiles:
99103
set_val = textwrap.dedent(
100-
"""
101-
import importlib.resources as res
104+
f"""
105+
import {resources.__name__} as res
102106
val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
103107
"""
104108
)
@@ -108,6 +112,10 @@ class ImplicitContextFiles:
108112
'submod.py': set_val,
109113
'res.txt': 'resources are the best',
110114
},
115+
'frozenpkg': {
116+
'__init__.py': set_val.replace(resources.__name__, 'c_resources'),
117+
'res.txt': 'resources are the best',
118+
},
111119
}
112120

113121
def test_implicit_files_package(self):
@@ -122,6 +130,32 @@ def test_implicit_files_submodule(self):
122130
"""
123131
assert importlib.import_module('somepkg.submod').val == 'resources are the best'
124132

133+
def _compile_importlib(self):
134+
"""
135+
Make a compiled-only copy of the importlib resources package.
136+
"""
137+
bin_site = self.fixtures.enter_context(os_helper.temp_dir())
138+
c_resources = pathlib.Path(bin_site, 'c_resources')
139+
sources = pathlib.Path(resources.__file__).parent
140+
shutil.copytree(sources, c_resources, ignore=lambda *_: ['__pycache__'])
141+
142+
for dirpath, _, filenames in os.walk(c_resources):
143+
for filename in filenames:
144+
source_path = pathlib.Path(dirpath) / filename
145+
cfile = source_path.with_suffix('.pyc')
146+
py_compile.compile(source_path, cfile)
147+
pathlib.Path.unlink(source_path)
148+
self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site))
149+
150+
def test_implicit_files_with_compiled_importlib(self):
151+
"""
152+
Caller detection works for compiled-only resources module.
153+
154+
python/cpython#123085
155+
"""
156+
self._compile_importlib()
157+
assert importlib.import_module('frozenpkg').val == 'resources are the best'
158+
125159

126160
class ImplicitContextFilesDiskTests(
127161
DirectSpec, util.DiskSetup, ImplicitContextFiles, unittest.TestCase
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
In a bare call to :func:`importlib.resources.files`, ensure the caller's
2+
frame is properly detected when ``importlib.resources`` is itself available
3+
as a compiled module only (no source).

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.