Repository navigation
importlib.resources.files() doesn't work correctly when importlib library is compiled #123085
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Aug 17, 2024 - changed the title
[-]importlib.files() doesn't work correctly when importlib library is compiled[/-][+]importlib.resources.files() doesn't work correctly when importlib library is compiled[/+]on Aug 17, 2024 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Aug 17, 2024 It may be important for frozen python applications
Did you encounter this issue in practice, or did you theorize the concern it while reviewing the code?
Did you encounter this issue in practice, or did you theorize the concern it while reviewing the code?
I encountered it in practice
- added a commit that references this issue
on Aug 19, 2024 - added a commit that references this issue
on Aug 21, 2024 - added a commit that references this issue
on Aug 22, 2024 - added a commit that references this issue
on Aug 27, 2024 - added a commit that references this issue
on Aug 28, 2024 5 remaining items
Some buildbots started failing when #123102 was merged, see:
- https://buildbot.python.org/#/builders/14/builds/6408/steps/8/logs/stdio
- https://buildbot.python.org/#/builders/350/builds/6572/steps/8/logs/stdio
I'll probably not have to investigate this week, but, my first guess would be that shutil.copytree copied some security metadata that says “this shouldn't be in
/tmp”.In the first log, I see:
0:01:29 load avg: 11.57 [1/1/1] test_importlib failed (2 errors) Re-running test_importlib in verbose mode (matching: test_implicit_files_with_compiled_importlib, test_implicit_files_with_compiled_importlib) test_implicit_files_with_compiled_importlib (test.test_importlib.resources.test_files.ImplicitContextFilesDiskTests.test_implicit_files_with_compiled_importlib) Caller detection works for compiled-only resources module. ... ERROR test_implicit_files_with_compiled_importlib (test.test_importlib.resources.test_files.ImplicitContextFilesZipTests.test_implicit_files_with_compiled_importlib) Caller detection works for compiled-only resources module. ... ERROR ====================================================================== ERROR: test_implicit_files_with_compiled_importlib (test.test_importlib.resources.test_files.ImplicitContextFilesDiskTests.test_implicit_files_with_compiled_importlib) Caller detection works for compiled-only resources module. ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/buildbot/buildarea/3.x.cstratak-fedora-stable-aarch64.clang-installed/build/target/lib/python3.14/test/test_importlib/resources/test_files.py", line 157, in test_implicit_files_with_compiled_importlib self._compile_importlib() ~~~~~~~~~~~~~~~~~~~~~~~^^ File "/home/buildbot/buildarea/3.x.cstratak-fedora-stable-aarch64.clang-installed/build/target/lib/python3.14/test/test_importlib/resources/test_files.py", line 147, in _compile_importlib py_compile.compile(source_path, cfile) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^ File "/home/buildbot/buildarea/3.x.cstratak-fedora-stable-aarch64.clang-installed/build/target/lib/python3.14/py_compile.py", line 172, in compile importlib._bootstrap_external._write_atomic(cfile, bytecode, mode) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap_external>", line 206, in _write_atomic PermissionError: [Errno 13] Permission denied: '/tmp/test_python_ytm0gb4y/tmpw7y1yd3q/c_resources/abc.pyc.281473105247840' ====================================================================== ERROR: test_implicit_files_with_compiled_importlib (test.test_importlib.resources.test_files.ImplicitContextFilesZipTests.test_implicit_files_with_compiled_importlib) Caller detection works for compiled-only resources module. ---------------------------------------------------------------------- Traceback (most recent call last): File "/home/buildbot/buildarea/3.x.cstratak-fedora-stable-aarch64.clang-installed/build/target/lib/python3.14/test/test_importlib/resources/test_files.py", line 157, in test_implicit_files_with_compiled_importlib self._compile_importlib() ~~~~~~~~~~~~~~~~~~~~~~~^^ File "/home/buildbot/buildarea/3.x.cstratak-fedora-stable-aarch64.clang-installed/build/target/lib/python3.14/test/test_importlib/resources/test_files.py", line 147, in _compile_importlib py_compile.compile(source_path, cfile) ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^ File "/home/buildbot/buildarea/3.x.cstratak-fedora-stable-aarch64.clang-installed/build/target/lib/python3.14/py_compile.py", line 172, in compile importlib._bootstrap_external._write_atomic(cfile, bytecode, mode) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^ File "<frozen importlib._bootstrap_external>", line 206, in _write_atomic PermissionError: [Errno 13] Permission denied: '/tmp/test_python_ytm0gb4y/tmp52oghnez/c_resources/abc.pyc.281473106346832' ---------------------------------------------------------------------- Ran 2 tests in 0.020sThe error is the same on the other buildbot. Both are Fedora, so perhaps that's a factor.
It's failing on this open call:
cpython/Lib/importlib/_bootstrap_external.py
Lines 206 to 207 in bbb36c0
fd = _os.open(path_tmp, _os.O_EXCL | _os.O_CREAT | _os.O_WRONLY, mode & 0o666) It's conceivable the copy,
shutil.copytree(sources, c_resources, ignore=lambda *_: ['__pycache__']) while it excludes
__pycache__could still be copying extant.pycfiles and those are failing to write atomically. Although, that's probably not it, because the failure is happening when writing out the temp file.my first guess would be that shutil.copytree copied some security metadata that says “this shouldn't be in
/tmp”.I'm not familiar with any OS that has such behaviors. I'd not have considered it. I did read up a bit on the
noexecmount option. At first blush, I wouldn't expect such an option to conflict with_os.O_EXCL | _os.O_CREAT | _os.O_WRONLY.I don't think I'll make much more progress on this without a local reproducer. Do you think this error can be reproduced in a Docker container? In the meantime, is the buildbot breakage worth putting in a check to allow these tests to skip or xfail when they cannot write out the compiled files?
Do you think this error can be reproduced in a Docker container?
I couldn't reproduce it locally. It might need some specific VM settings :(
In the meantime, is the buildbot breakage worth putting in a check to allow these tests to skip or xfail when they cannot write out the compiled files?
First let me try an implementation that avoids the initial copy, which should be a bit faster too: #124131
- added a commit that references this issue
on Sep 18, 2024 - added a commit that references this issue
on Jan 5, 2025 - added a commit that references this issue
on Jan 7, 2025 I've also encountered this issue in practice. It causes a failure when the stdlib is zipped, and
(importlib.resources.files() / "datafile.txt").read_text()
fails with
FileNotFoundError: /opt/python/lib/python313.zip/importlib/resources/datafile.txt- added a commit that references this issue
on Aug 15, 2025 - added a commit that references this issue
on Aug 18, 2025
Bug report
Bug description:
If
importliblibrary is compiled (in particular_common.py), then any package, that uses barefiles()to get its resources, breaks, because_infer_callerreturns wrong frame (as mentioned in #123037 (comment),__file__incpython/Lib/importlib/resources/_common.py
Line 96 in 35d8ac7
Reproducer:
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