Skip to content

Commit 0ecbc3b

Browse files
authored
Merge pull request #314 from python/gh-123085/inferred-compiled
Fix issue in inferred caller when resourcse package has no source.
2 parents 21afd61 + 79fa62f commit 0ecbc3b

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

importlib_resources/_common.py

+3-2
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

importlib_resources/tests/test_files.py

+37-2
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
@@ -7,6 +11,7 @@
711
import importlib_resources as resources
812
from ..abc import Traversable
913
from . import util
14+
from .compat.py39 import os_helper, import_helper
1015

1116

1217
@contextlib.contextmanager
@@ -97,8 +102,8 @@ class ModuleFilesZipTests(DirectSpec, util.ZipSetup, ModulesFiles, unittest.Test
97102

98103
class ImplicitContextFiles:
99104
set_val = textwrap.dedent(
100-
"""
101-
import importlib_resources as res
105+
f"""
106+
import {resources.__name__} as res
102107
val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
103108
"""
104109
)
@@ -108,6 +113,10 @@ class ImplicitContextFiles:
108113
'submod.py': set_val,
109114
'res.txt': 'resources are the best',
110115
},
116+
'frozenpkg': {
117+
'__init__.py': set_val.replace(resources.__name__, 'c_resources'),
118+
'res.txt': 'resources are the best',
119+
},
111120
}
112121

113122
def test_implicit_files_package(self):
@@ -122,6 +131,32 @@ def test_implicit_files_submodule(self):
122131
"""
123132
assert importlib.import_module('somepkg.submod').val == 'resources are the best'
124133

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

126161
class ImplicitContextFilesDiskTests(
127162
DirectSpec, util.DiskSetup, ImplicitContextFiles, unittest.TestCase

newsfragments/+0f77c990.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When inferring the caller in ``files()`` correctly detect one's own module even when the resources package source is not present. (python/cpython#123085)

0 commit comments

Comments
 (0)