Uh oh!
There was an error while loading. Please reload this page.
Fix issue in inferred caller when resourcse package has no source. - #314
Conversation
jaraco
commented
Aug 17, 2024
@Gatsik I've cherry-picked your commit here and adapted it for the recent changes and to work with |
Gatsik
commented
Aug 17, 2024
@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: importimportlibimportosimportpathlibimportpy_compileimportshutilimportsysimporttempfileimporttextwrapdefcompile(tempdir):
orig_importlib=pathlib.Path(importlib.__file__).parentsource_dir=pathlib.Path(tempdir) /"source_importlib"target_dir=pathlib.Path(tempdir) /'cimportlib'shutil.copytree(orig_importlib, source_dir, ignore=lambda*_: ['__pycache__'])
fordirpath, _, filenamesinos.walk(source_dir):
forfilenameinfilenames:
source_path=pathlib.Path(dirpath) /filenamecfilename=source_path.with_suffix('.pyc').namecfile=target_dir/pathlib.Path(dirpath).name/cfilenamepy_compile.compile(source_path, cfile)
defcreate_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",
}
forfile, contentincontents.items():
path=pathlib.Path(package_dir) /filepath.write_text(content)
defmain():
withtempfile.TemporaryDirectory() astempdir:
compile(tempdir)
create_package(tempdir)
sys.path.insert(0, str(tempdir))
print(importlib.import_module('somepkg').val)
if__name__=="__main__":
raiseSystemExit(main()) |
792da5a to
4ea81bfComparejaraco
commented
Aug 17, 2024
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). |
jaraco
commented
Aug 17, 2024
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+? |
Uh oh!
There was an error while loading. Please reload this page.
5c3e385 to
d618902Comparejaraco
commented
Aug 17, 2024
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).
Gatsik
commented
Aug 17, 2024
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 <jaraco@jaraco.com>
… 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 a53812d)
… 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 a53812df126b99bca25187441a123c7785ee82a0) CPython-sync-commit-latest: d712ece43feeed7f930954d40b046cdaeed7c414
Closespython/cpython#123085