Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
Open
python/importlib_resources
#314Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Bug description:
If importlib library is compiled (in particular _common.py), then any package, that uses bare files() to get its resources, breaks, because _infer_caller returns wrong frame (as mentioned in #123037 (comment), __file__ in
| returnframe_info.filename==__file__ |
Reproducer:
importimportlibimportosimportpathlibimportpy_compileimportshutilimportsysimporttempfileimporttextwrapdefcompile(tempdir): target_dir=pathlib.Path(tempdir) /'cimportlib'souce_dir=pathlib.Path(importlib.__file__).parentshutil.copytree(souce_dir, target_dir, ignore=lambda*_: ['__pycache__']) fordirpath, _, filenamesinos.walk(target_dir): forfilenameinfilenames: iffilename!="_common.py": continuesource_path=pathlib.Path(dirpath) /filenamecfile=source_path.with_suffix('.pyc') py_compile.compile(source_path, cfile) pathlib.Path.unlink(source_path) 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())Expectation:
dataActual outcome:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpngnuw441/cimportlib/resources/resource.txt'It may be important for frozen python applications, as they don't include source code
CPython versions tested on:
3.12, CPython main branch
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-123085: Fix issue in inferred caller when resources package has no source #123102
- [3.12] gh-123085: Fix issue in inferred caller when resources package has no source (GH-123102) #124021
- [3.13] gh-123085: Fix issue in inferred caller when resource package has no source (GH-123102) #124024
- gh-123085: _compile_importlib: Avoid copying sources before compilation #124131
- [3.12] gh-123085: _compile_importlib: Avoid copying sources before compilation (GH-124131) #128581
- [3.12] gh-123085: Remove double 'import os' added by PR #124021 #128600
- [3.13] gh-123085: _compile_importlib: Avoid copying sources before compilation (GH-124131) #137914
Metadata
Metadata
Assignees
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error