https://github.com/python/cpython/commit/0607fdd56c7c5e7f0949f9a6ef95609e9437b1eb
commit: 0607fdd56c7c5e7f0949f9a6ef95609e9437b1eb
branch: 3.13
author: Jason R. Coombs <[email protected]>
committer: jaraco <[email protected]>
date: 2025-08-15T14:21:16Z
summary:
[3.13] gh-123085: Fix issue in inferred caller when resource package has no
source (GH-123102) (#124024)
files:
A Misc/NEWS.d/next/Library/2024-08-17-08-17-20.gh-issue-123085.7Io2yH.rst
M Lib/importlib/resources/_common.py
M Lib/test/test_importlib/resources/test_files.py
diff --git a/Lib/importlib/resources/_common.py
b/Lib/importlib/resources/_common.py
index 171a7f29249943..cae4699f410b3a 100644
--- a/Lib/importlib/resources/_common.py
+++ b/Lib/importlib/resources/_common.py
@@ -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
diff --git a/Lib/test/test_importlib/resources/test_files.py
b/Lib/test/test_importlib/resources/test_files.py
index ef7b57959974ee..f903d3bba85ebc 100644
--- a/Lib/test/test_importlib/resources/test_files.py
+++ b/Lib/test/test_importlib/resources/test_files.py
@@ -1,3 +1,7 @@
+import os
+import pathlib
+import py_compile
+import shutil
import textwrap
import unittest
import warnings
@@ -7,6 +11,7 @@
from importlib import resources
from importlib.resources.abc import Traversable
from . import util
+from test.support import os_helper, import_helper
@contextlib.contextmanager
@@ -117,8 +122,8 @@ class ModuleFilesZipTests(DirectSpec, util.ZipSetup,
ModulesFiles, unittest.Test
class ImplicitContextFiles:
set_val = textwrap.dedent(
- """
- import importlib.resources as res
+ f"""
+ import {resources.__name__} as res
val = res.files().joinpath('res.txt').read_text(encoding='utf-8')
"""
)
@@ -128,6 +133,10 @@ class ImplicitContextFiles:
'submod.py': set_val,
'res.txt': 'resources are the best',
},
+ 'frozenpkg': {
+ '__init__.py': set_val.replace(resources.__name__, 'c_resources'),
+ 'res.txt': 'resources are the best',
+ },
}
def test_implicit_files_package(self):
@@ -142,6 +151,32 @@ def test_implicit_files_submodule(self):
"""
assert importlib.import_module('somepkg.submod').val == 'resources are
the best'
+ def _compile_importlib(self):
+ """
+ Make a compiled-only copy of the importlib resources package.
+ """
+ bin_site = self.fixtures.enter_context(os_helper.temp_dir())
+ c_resources = pathlib.Path(bin_site, 'c_resources')
+ sources = pathlib.Path(resources.__file__).parent
+ shutil.copytree(sources, c_resources, ignore=lambda *_:
['__pycache__'])
+
+ for dirpath, _, filenames in os.walk(c_resources):
+ for filename in filenames:
+ source_path = pathlib.Path(dirpath) / filename
+ cfile = source_path.with_suffix('.pyc')
+ py_compile.compile(source_path, cfile)
+ pathlib.Path.unlink(source_path)
+ self.fixtures.enter_context(import_helper.DirsOnSysPath(bin_site))
+
+ def test_implicit_files_with_compiled_importlib(self):
+ """
+ Caller detection works for compiled-only resources module.
+
+ python/cpython#123085
+ """
+ self._compile_importlib()
+ assert importlib.import_module('frozenpkg').val == 'resources are the
best'
+
class ImplicitContextFilesDiskTests(
DirectSpec, util.DiskSetup, ImplicitContextFiles, unittest.TestCase
diff --git
a/Misc/NEWS.d/next/Library/2024-08-17-08-17-20.gh-issue-123085.7Io2yH.rst
b/Misc/NEWS.d/next/Library/2024-08-17-08-17-20.gh-issue-123085.7Io2yH.rst
new file mode 100644
index 00000000000000..2e09401ceb5b56
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-08-17-08-17-20.gh-issue-123085.7Io2yH.rst
@@ -0,0 +1,3 @@
+In a bare call to :func:`importlib.resources.files`, ensure the caller's
+frame is properly detected when ``importlib.resources`` is itself available
+as a compiled module only (no source).
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]