Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

importlib.resources.files() doesn't work correctly when importlib library is compiled #123085

Open
python/importlib_resources
#314
Labels
stdlibPython modules in the Lib dirtopic-importlibtype-bugAn unexpected behavior, bug, or error
@Gatsik

Description

@Gatsik

Bug report

Bug description:

Ifimportlib library is compiled (in particular_common.py), then any package, that uses barefiles() to get its resources, breaks, because_infer_caller returns wrong frame (as mentioned in#123037 (comment),__file__ in

returnframe_info.filename==__file__
is not valid)

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:

data

Actual 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtopic-importlibtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp