-
-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix issue in inferred caller when resourcse package has no source. #314
Conversation
@Gatsik I've cherry-picked your commit here and adapted it for the recent changes and to work with |
@jaraco Great! Thanks for including me as an author of one of the commits! My concern is that compiled python files 'freeze' their source filename, and this 'frozen' filename is referenced in frame, but not in Reproducer, which fails with textual substitution: import importlib
import os
import pathlib
import py_compile
import shutil
import sys
import tempfile
import textwrap
def compile(tempdir):
orig_importlib = pathlib.Path(importlib.__file__).parent
source_dir = pathlib.Path(tempdir) / "source_importlib"
target_dir = pathlib.Path(tempdir) / 'cimportlib'
shutil.copytree(orig_importlib, source_dir, ignore=lambda *_: ['__pycache__'])
for dirpath, _, filenames in os.walk(source_dir):
for filename in filenames:
source_path = pathlib.Path(dirpath) / filename
cfilename = source_path.with_suffix('.pyc').name
cfile = target_dir / pathlib.Path(dirpath).name / cfilename
py_compile.compile(source_path, cfile)
def create_package(tempdir):
package_dir = pathlib.Path(tempdir) / 'somepkg'
package_dir.mkdir()
contents = {
"__init__.py": textwrap.dedent(
"""
import cimportlib.resources as res
val = res.files().joinpath('resource.txt').read_text(encoding='utf-8')
"""
),
"resource.txt": "data",
}
for file, content in contents.items():
path = pathlib.Path(package_dir) / file
path.write_text(content)
def main():
with tempfile.TemporaryDirectory() as tempdir:
compile(tempdir)
create_package(tempdir)
sys.path.insert(0, str(tempdir))
print(importlib.import_module('somepkg').val)
if __name__ == "__main__":
raise SystemExit(main()) |
792da5a
to
4ea81bf
Compare
Thanks for clarifying. That makes sense. I've pushed that last commit to refs/archive/314-text-sub and force-pushed the previous commit (retaining the frame-based check). |
The next thing I need to determine - is this a bugfix or a new feature. On one hand, it's the correction of a behavior that was presumed to work broadly but didn't. On the other hand, it's expanding the scope of tested/supported scenarios. I see you've tagged the downstream issue as "type-bug". Would you like to advocate for it being a bugfix and not a feature? What use cases would be affected if this behavior were only available here and in Python 3.14+? |
5c3e385
to
d618902
Compare
Releasing as v6.4.3. Please give it a try and let me know if you encounter any issues. This will get ported to CPython as well. |
…has no source. From importlib_resources 6.4.3 (python/importlib_resources#314).
In my opinion, it's a bug, because previously
Applications, which use libraries, which use bare |
…has no source. From importlib_resources 6.4.3 (python/importlib_resources#314).
…has no source. From importlib_resources 6.4.3 (python/importlib_resources#314).
… source (#123102) gh-123085: Fix issue in inferred caller when resources package has no source. From importlib_resources 6.4.3 (python/importlib_resources#314).
…ackage has no source (pythonGH-123102) pythongh-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 a53812d) Co-authored-by: Jason R. Coombs <[email protected]>
Closes python/cpython#123085