Skip to content
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

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

Open
Gatsik opened this issue Aug 17, 2024 · 5 comments · Fixed by python/importlib_resources#314
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@Gatsik
Copy link

Gatsik commented Aug 17, 2024

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

return frame_info.filename == __file__
is not valid)

Reproducer:

import importlib
import os
import pathlib
import py_compile
import shutil
import sys
import tempfile
import textwrap


def compile(tempdir):
    target_dir = pathlib.Path(tempdir) / 'cimportlib'
    souce_dir = pathlib.Path(importlib.__file__).parent
    shutil.copytree(souce_dir, target_dir, ignore=lambda *_: ['__pycache__'])

    for dirpath, _, filenames in os.walk(target_dir):
        for filename in filenames:
            if filename != "_common.py":
                continue
            source_path = pathlib.Path(dirpath) / filename
            cfile = source_path.with_suffix('.pyc')
            py_compile.compile(source_path, cfile)
            pathlib.Path.unlink(source_path)


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())

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

@Gatsik Gatsik added the type-bug An unexpected behavior, bug, or error label Aug 17, 2024
@Gatsik Gatsik 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 Aug 17, 2024
@picnixz picnixz added the stdlib Python modules in the Lib dir label Aug 17, 2024
@jaraco
Copy link
Member

jaraco commented 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?

@Gatsik
Copy link
Author

Gatsik commented Aug 17, 2024

Did you encounter this issue in practice, or did you theorize the concern it while reviewing the code?

I encountered it in practice

kai687 pushed a commit to kai687/sphinxawesome-theme that referenced this issue Aug 19, 2024
Bumps
[importlib-resources](https://github.com/python/importlib_resources)
from 6.4.0 to 6.4.3.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/python/importlib_resources/blob/main/NEWS.rst">importlib-resources's
changelog</a>.</em></p>
<blockquote>
<h1>v6.4.3</h1>
<h2>Bugfixes</h2>
<ul>
<li>When inferring the caller in
<code>files()</code><code>python/cpython#123085</code></li>
</ul>
<h1>v6.4.2</h1>
<h2>Bugfixes</h2>
<ul>
<li>Merged fix for UTF-16 BOM handling in functional tests. (<a
href="https://redirect.github.com/python/importlib_resources/issues/312">#312</a>)</li>
</ul>
<h1>v6.4.1</h1>
<h2>Bugfixes</h2>
<ul>
<li><code>python/cpython#121735</code></li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/python/importlib_resources/commit/d02141768b62468e46064614276036ea5c746056"><code>d021417</code></a>
Finalize</li>
<li><a
href="https://github.com/python/importlib_resources/commit/0ecbc3b374ae84ae10ded5e1ad1d8775e12c2dd7"><code>0ecbc3b</code></a>
Merge pull request <a
href="https://redirect.github.com/python/importlib_resources/issues/314">#314</a>
from python/<a
href="https://redirect.github.com/python/importlib_resources/issues/123085">gh-123085</a>/inferred-compiled</li>
<li><a
href="https://github.com/python/importlib_resources/commit/79fa62f4b5cbf8f358560651a714b282aee2226c"><code>79fa62f</code></a>
Add docstring and reference to the issue.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/90c0e420ef15256f116342c97ea984a2fa604cc3"><code>90c0e42</code></a>
Rely on <code>resources.__name__</code> for easier portability.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/d618902dbe0e9f94e634a95bc9bbaf941236fc0c"><code>d618902</code></a>
Add news fragment.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/ebc5b97ffe1d20eb6ebc536dd1f6b385f83652a5"><code>ebc5b97</code></a>
Extract the filename from the topmost frame of the stack.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/4ea81bf920f6cc6377ccc7fbc1f4f343927f4f20"><code>4ea81bf</code></a>
Extract a function for computing 'this filename' once.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/cba8dce7839977c66806ef05e122cf38ed14a113"><code>cba8dce</code></a>
Adapt changes for new fixtures.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/198adec064edf0c10aac45ab85c68535ba038a59"><code>198adec</code></a>
<a
href="https://redirect.github.com/python/importlib_resources/issues/121735">gh-121735</a>:
Fix inferring caller when resolving importlib.resources.files()</li>
<li><a
href="https://github.com/python/importlib_resources/commit/21afd614ce6976b43185a0931f988ea81862eb8d"><code>21afd61</code></a>
Merge changes to syncronize the 6.4 release with downstream CPython
changes.</li>
<li>Additional commits viewable in <a
href="https://github.com/python/importlib_resources/compare/v6.4.0...v6.4.3">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=importlib-resources&package-manager=pip&previous-version=6.4.0&new-version=6.4.3)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
github-actions bot pushed a commit to aio-libs/aiohttp that referenced this issue Aug 21, 2024
Bumps
[importlib-resources](https://github.com/python/importlib_resources)
from 6.1.1 to 6.4.3.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/python/importlib_resources/blob/main/NEWS.rst">importlib-resources's
changelog</a>.</em></p>
<blockquote>
<h1>v6.4.3</h1>
<h2>Bugfixes</h2>
<ul>
<li>When inferring the caller in
<code>files()</code><code>python/cpython#123085</code></li>
</ul>
<h1>v6.4.2</h1>
<h2>Bugfixes</h2>
<ul>
<li>Merged fix for UTF-16 BOM handling in functional tests. (<a
href="https://redirect.github.com/python/importlib_resources/issues/312">#312</a>)</li>
</ul>
<h1>v6.4.1</h1>
<h2>Bugfixes</h2>
<ul>
<li><code>python/cpython#121735</code></li>
</ul>
<h1>v6.4.0</h1>
<h2>Features</h2>
<ul>
<li>The functions
<code>is_resource()</code>,
<code>open_binary()</code>,
<code>open_text()</code>,
<code>path()</code>,
<code>read_binary()</code>, and
<code>read_text()</code> are un-deprecated, and support
subdirectories via multiple positional arguments.
The <code>contents()</code> function also allows subdirectories,
but remains deprecated. (<a
href="https://redirect.github.com/python/importlib_resources/issues/303">#303</a>)</li>
<li><code>python/cpython#109829</code></li>
</ul>
<h1>v6.3.2</h1>
<p>Bugfixes</p>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/python/importlib_resources/commit/d02141768b62468e46064614276036ea5c746056"><code>d021417</code></a>
Finalize</li>
<li><a
href="https://github.com/python/importlib_resources/commit/0ecbc3b374ae84ae10ded5e1ad1d8775e12c2dd7"><code>0ecbc3b</code></a>
Merge pull request <a
href="https://redirect.github.com/python/importlib_resources/issues/314">#314</a>
from python/<a
href="https://redirect.github.com/python/importlib_resources/issues/123085">gh-123085</a>/inferred-compiled</li>
<li><a
href="https://github.com/python/importlib_resources/commit/79fa62f4b5cbf8f358560651a714b282aee2226c"><code>79fa62f</code></a>
Add docstring and reference to the issue.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/90c0e420ef15256f116342c97ea984a2fa604cc3"><code>90c0e42</code></a>
Rely on <code>resources.__name__</code> for easier portability.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/d618902dbe0e9f94e634a95bc9bbaf941236fc0c"><code>d618902</code></a>
Add news fragment.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/ebc5b97ffe1d20eb6ebc536dd1f6b385f83652a5"><code>ebc5b97</code></a>
Extract the filename from the topmost frame of the stack.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/4ea81bf920f6cc6377ccc7fbc1f4f343927f4f20"><code>4ea81bf</code></a>
Extract a function for computing 'this filename' once.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/cba8dce7839977c66806ef05e122cf38ed14a113"><code>cba8dce</code></a>
Adapt changes for new fixtures.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/198adec064edf0c10aac45ab85c68535ba038a59"><code>198adec</code></a>
<a
href="https://redirect.github.com/python/importlib_resources/issues/121735">gh-121735</a>:
Fix inferring caller when resolving importlib.resources.files()</li>
<li><a
href="https://github.com/python/importlib_resources/commit/21afd614ce6976b43185a0931f988ea81862eb8d"><code>21afd61</code></a>
Merge changes to syncronize the 6.4 release with downstream CPython
changes.</li>
<li>Additional commits viewable in <a
href="https://github.com/python/importlib_resources/compare/v6.1.1...v6.4.3">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=importlib-resources&package-manager=pip&previous-version=6.1.1&new-version=6.4.3)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
shaldengeki added a commit to shaldengeki/monorepo that referenced this issue Aug 22, 2024
Bumps
[importlib-resources](https://github.com/python/importlib_resources)
from 6.4.0 to 6.4.3.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/python/importlib_resources/blob/main/NEWS.rst">importlib-resources's
changelog</a>.</em></p>
<blockquote>
<h1>v6.4.3</h1>
<h2>Bugfixes</h2>
<ul>
<li>When inferring the caller in
<code>files()</code><code>python/cpython#123085</code></li>
</ul>
<h1>v6.4.2</h1>
<h2>Bugfixes</h2>
<ul>
<li>Merged fix for UTF-16 BOM handling in functional tests. (<a
href="https://redirect.github.com/python/importlib_resources/issues/312">#312</a>)</li>
</ul>
<h1>v6.4.1</h1>
<h2>Bugfixes</h2>
<ul>
<li><code>python/cpython#121735</code></li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/python/importlib_resources/commit/d02141768b62468e46064614276036ea5c746056"><code>d021417</code></a>
Finalize</li>
<li><a
href="https://github.com/python/importlib_resources/commit/0ecbc3b374ae84ae10ded5e1ad1d8775e12c2dd7"><code>0ecbc3b</code></a>
Merge pull request <a
href="https://redirect.github.com/python/importlib_resources/issues/314">#314</a>
from python/<a
href="https://redirect.github.com/python/importlib_resources/issues/123085">gh-123085</a>/inferred-compiled</li>
<li><a
href="https://github.com/python/importlib_resources/commit/79fa62f4b5cbf8f358560651a714b282aee2226c"><code>79fa62f</code></a>
Add docstring and reference to the issue.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/90c0e420ef15256f116342c97ea984a2fa604cc3"><code>90c0e42</code></a>
Rely on <code>resources.__name__</code> for easier portability.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/d618902dbe0e9f94e634a95bc9bbaf941236fc0c"><code>d618902</code></a>
Add news fragment.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/ebc5b97ffe1d20eb6ebc536dd1f6b385f83652a5"><code>ebc5b97</code></a>
Extract the filename from the topmost frame of the stack.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/4ea81bf920f6cc6377ccc7fbc1f4f343927f4f20"><code>4ea81bf</code></a>
Extract a function for computing 'this filename' once.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/cba8dce7839977c66806ef05e122cf38ed14a113"><code>cba8dce</code></a>
Adapt changes for new fixtures.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/198adec064edf0c10aac45ab85c68535ba038a59"><code>198adec</code></a>
<a
href="https://redirect.github.com/python/importlib_resources/issues/121735">gh-121735</a>:
Fix inferring caller when resolving importlib.resources.files()</li>
<li><a
href="https://github.com/python/importlib_resources/commit/21afd614ce6976b43185a0931f988ea81862eb8d"><code>21afd61</code></a>
Merge changes to syncronize the 6.4 release with downstream CPython
changes.</li>
<li>Additional commits viewable in <a
href="https://github.com/python/importlib_resources/compare/v6.4.0...v6.4.3">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=importlib-resources&package-manager=pip&previous-version=6.4.0&new-version=6.4.3)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Charles Guo <[email protected]>
github-actions bot pushed a commit to aio-libs/aiohttp that referenced this issue Aug 27, 2024
Bumps
[importlib-resources](https://github.com/python/importlib_resources)
from 6.1.1 to 6.4.4.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/python/importlib_resources/blob/main/NEWS.rst">importlib-resources's
changelog</a>.</em></p>
<blockquote>
<h1>v6.4.4</h1>
<p>No significant changes.</p>
<h1>v6.4.3</h1>
<h2>Bugfixes</h2>
<ul>
<li>When inferring the caller in
<code>files()</code><code>python/cpython#123085</code></li>
</ul>
<h1>v6.4.2</h1>
<h2>Bugfixes</h2>
<ul>
<li>Merged fix for UTF-16 BOM handling in functional tests. (<a
href="https://redirect.github.com/python/importlib_resources/issues/312">#312</a>)</li>
</ul>
<h1>v6.4.1</h1>
<h2>Bugfixes</h2>
<ul>
<li><code>python/cpython#121735</code></li>
</ul>
<h1>v6.4.0</h1>
<h2>Features</h2>
<ul>
<li>The functions
<code>is_resource()</code>,
<code>open_binary()</code>,
<code>open_text()</code>,
<code>path()</code>,
<code>read_binary()</code>, and
<code>read_text()</code> are un-deprecated, and support
subdirectories via multiple positional arguments.
The <code>contents()</code> function also allows subdirectories,
but remains deprecated. (<a
href="https://redirect.github.com/python/importlib_resources/issues/303">#303</a>)</li>
<li><code>python/cpython#109829</code></li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/python/importlib_resources/commit/9689f8f82ca838cd58d3d0d80828785ada7798c9"><code>9689f8f</code></a>
Finalize</li>
<li><a
href="https://github.com/python/importlib_resources/commit/f3ad28a14feb85dc2631a8e3658f090ed0b7522d"><code>f3ad28a</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/045dde42b16581b99539667a87d61252bf4d08ac"><code>045dde4</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/2fecb5e596c7b6ff2d3f98af0ac63c9f38401c5a"><code>2fecb5e</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/75b301c95ce3d75626c949a26e650deceba8e62c"><code>75b301c</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/45167451781dd8fc03e5568da40144dd8a4f790d"><code>4516745</code></a>
Merge <a
href="https://github.com/jaraco/skeleton">https://github.com/jaraco/skeleton</a></li>
<li><a
href="https://github.com/python/importlib_resources/commit/f1350e413775a9e79e20779cc9705e28a1c55900"><code>f1350e4</code></a>
Add upstream and local sections for 'type' extra, since many projects
will ha...</li>
<li><a
href="https://github.com/python/importlib_resources/commit/d02141768b62468e46064614276036ea5c746056"><code>d021417</code></a>
Finalize</li>
<li><a
href="https://github.com/python/importlib_resources/commit/0ecbc3b374ae84ae10ded5e1ad1d8775e12c2dd7"><code>0ecbc3b</code></a>
Merge pull request <a
href="https://redirect.github.com/python/importlib_resources/issues/314">#314</a>
from python/<a
href="https://redirect.github.com/python/importlib_resources/issues/123085">gh-123085</a>/inferred-compiled</li>
<li><a
href="https://github.com/python/importlib_resources/commit/79fa62f4b5cbf8f358560651a714b282aee2226c"><code>79fa62f</code></a>
Add docstring and reference to the issue.</li>
<li>Additional commits viewable in <a
href="https://github.com/python/importlib_resources/compare/v6.1.1...v6.4.4">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=importlib-resources&package-manager=pip&previous-version=6.1.1&new-version=6.4.4)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
jesse-c added a commit to climatepolicyradar/navigator-data-ingest that referenced this issue Aug 28, 2024
Bumps
[importlib-resources](https://github.com/python/importlib_resources)
from 5.13.0 to 6.4.4.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a
href="https://github.com/python/importlib_resources/blob/main/NEWS.rst">importlib-resources's
changelog</a>.</em></p>
<blockquote>
<h1>v6.4.4</h1>
<p>No significant changes.</p>
<h1>v6.4.3</h1>
<h2>Bugfixes</h2>
<ul>
<li>When inferring the caller in
<code>files()</code><code>python/cpython#123085</code></li>
</ul>
<h1>v6.4.2</h1>
<h2>Bugfixes</h2>
<ul>
<li>Merged fix for UTF-16 BOM handling in functional tests. (<a
href="https://redirect.github.com/python/importlib_resources/issues/312">#312</a>)</li>
</ul>
<h1>v6.4.1</h1>
<h2>Bugfixes</h2>
<ul>
<li><code>python/cpython#121735</code></li>
</ul>
<h1>v6.4.0</h1>
<h2>Features</h2>
<ul>
<li>The functions
<code>is_resource()</code>,
<code>open_binary()</code>,
<code>open_text()</code>,
<code>path()</code>,
<code>read_binary()</code>, and
<code>read_text()</code> are un-deprecated, and support
subdirectories via multiple positional arguments.
The <code>contents()</code> function also allows subdirectories,
but remains deprecated. (<a
href="https://redirect.github.com/python/importlib_resources/issues/303">#303</a>)</li>
<li><code>python/cpython#109829</code></li>
</ul>
<!-- raw HTML omitted -->
</blockquote>
<p>... (truncated)</p>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a
href="https://github.com/python/importlib_resources/commit/9689f8f82ca838cd58d3d0d80828785ada7798c9"><code>9689f8f</code></a>
Finalize</li>
<li><a
href="https://github.com/python/importlib_resources/commit/f3ad28a14feb85dc2631a8e3658f090ed0b7522d"><code>f3ad28a</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/045dde42b16581b99539667a87d61252bf4d08ac"><code>045dde4</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/2fecb5e596c7b6ff2d3f98af0ac63c9f38401c5a"><code>2fecb5e</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/75b301c95ce3d75626c949a26e650deceba8e62c"><code>75b301c</code></a>
🧎‍♀️ Genuflect to the types.</li>
<li><a
href="https://github.com/python/importlib_resources/commit/45167451781dd8fc03e5568da40144dd8a4f790d"><code>4516745</code></a>
Merge <a
href="https://github.com/jaraco/skeleton">https://github.com/jaraco/skeleton</a></li>
<li><a
href="https://github.com/python/importlib_resources/commit/f1350e413775a9e79e20779cc9705e28a1c55900"><code>f1350e4</code></a>
Add upstream and local sections for 'type' extra, since many projects
will ha...</li>
<li><a
href="https://github.com/python/importlib_resources/commit/d02141768b62468e46064614276036ea5c746056"><code>d021417</code></a>
Finalize</li>
<li><a
href="https://github.com/python/importlib_resources/commit/0ecbc3b374ae84ae10ded5e1ad1d8775e12c2dd7"><code>0ecbc3b</code></a>
Merge pull request <a
href="https://redirect.github.com/python/importlib_resources/issues/314">#314</a>
from python/<a
href="https://redirect.github.com/python/importlib_resources/issues/123085">gh-123085</a>/inferred-compiled</li>
<li><a
href="https://github.com/python/importlib_resources/commit/79fa62f4b5cbf8f358560651a714b282aee2226c"><code>79fa62f</code></a>
Add docstring and reference to the issue.</li>
<li>Additional commits viewable in <a
href="https://github.com/python/importlib_resources/compare/v5.13.0...v6.4.4">compare
view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility
score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=importlib-resources&package-manager=pip&previous-version=5.13.0&new-version=6.4.4)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)

Dependabot will resolve any conflicts with this PR as long as you don't
alter it yourself. You can also trigger a rebase manually by commenting
`@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits
that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after
your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge
and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating
it. You can achieve the same result by closing it manually
- `@dependabot show <dependency name> ignore conditions` will show all
of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop
Dependabot creating any more for this major version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop
Dependabot creating any more for this minor version (unless you reopen
the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop
Dependabot creating any more for this dependency (unless you reopen the
PR or upgrade to it yourself)


</details>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Jesse Claven <[email protected]>
jaraco added a commit to jaraco/cpython that referenced this issue Sep 12, 2024
jaraco added a commit to jaraco/cpython that referenced this issue Sep 12, 2024
jaraco added a commit to jaraco/cpython that referenced this issue Sep 12, 2024
jaraco added a commit to jaraco/cpython that referenced this issue Sep 12, 2024
jaraco added a commit that referenced this issue Sep 12, 2024
… 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).
jaraco added a commit to jaraco/cpython that referenced this issue Sep 12, 2024
…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]>
jaraco added a commit to jaraco/cpython that referenced this issue Sep 12, 2024
@encukou
Copy link
Member

encukou commented Sep 13, 2024

Some buildbots started failing when #123102 was merged, see:

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”.

@jaraco
Copy link
Member

jaraco commented Sep 13, 2024

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.020s

The error is the same on the other buildbot. Both are Fedora, so perhaps that's a factor.

It's failing on this open call:

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 .pyc files 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 noexec mount 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?

@encukou
Copy link
Member

encukou commented Sep 17, 2024

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

encukou added a commit that referenced this issue Sep 18, 2024
savannahostrowski pushed a commit to savannahostrowski/cpython that referenced this issue Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants