Title: importlib.resources.files() doesn't work correctly when importlib library is compiled · Issue #123085 · python/cpython · GitHub
Open Graph Title: importlib.resources.files() doesn't work correctly when importlib library is compiled · Issue #123085 · python/cpython
X Title: importlib.resources.files() doesn't work correctly when importlib library is compiled · Issue #123085 · python/cpython
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),...
Open Graph 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 f...
X 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 f...
Opengraph URL: https://github.com/python/cpython/issues/123085
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"importlib.resources.files() doesn't work correctly when importlib library is compiled","articleBody":"# Bug report\r\n\r\n### Bug description:\r\n\r\nIf `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 https://github.com/python/cpython/pull/123037#issuecomment-2294431430, `__file__` in https://github.com/python/cpython/blob/35d8ac7cd7ed6cd3d84af721dce970da59bd5f68/Lib/importlib/resources/_common.py#L96 is not valid)\r\n\r\nReproducer:\r\n```python\r\nimport importlib\r\nimport os\r\nimport pathlib\r\nimport py_compile\r\nimport shutil\r\nimport sys\r\nimport tempfile\r\nimport textwrap\r\n\r\n\r\ndef compile(tempdir):\r\n target_dir = pathlib.Path(tempdir) / 'cimportlib'\r\n souce_dir = pathlib.Path(importlib.__file__).parent\r\n shutil.copytree(souce_dir, target_dir, ignore=lambda *_: ['__pycache__'])\r\n\r\n for dirpath, _, filenames in os.walk(target_dir):\r\n for filename in filenames:\r\n if filename != \"_common.py\":\r\n continue\r\n source_path = pathlib.Path(dirpath) / filename\r\n cfile = source_path.with_suffix('.pyc')\r\n py_compile.compile(source_path, cfile)\r\n pathlib.Path.unlink(source_path)\r\n\r\n\r\ndef create_package(tempdir):\r\n package_dir = pathlib.Path(tempdir) / 'somepkg'\r\n package_dir.mkdir()\r\n contents = {\r\n \"__init__.py\": textwrap.dedent(\r\n \"\"\"\r\n import cimportlib.resources as res\r\n val = res.files().joinpath('resource.txt').read_text(encoding='utf-8')\r\n \"\"\"\r\n ),\r\n \"resource.txt\": \"data\",\r\n }\r\n\r\n for file, content in contents.items():\r\n path = pathlib.Path(package_dir) / file\r\n path.write_text(content)\r\n\r\n\r\ndef main():\r\n with tempfile.TemporaryDirectory() as tempdir:\r\n compile(tempdir)\r\n create_package(tempdir)\r\n sys.path.insert(0, str(tempdir))\r\n print(importlib.import_module('somepkg').val)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n raise SystemExit(main())\r\n\r\n```\r\nExpectation:\r\n```console\r\ndata\r\n```\r\nActual outcome:\r\n```console\r\nFileNotFoundError: [Errno 2] No such file or directory: '/tmp/tmpngnuw441/cimportlib/resources/resource.txt'\r\n```\r\nIt may be important for frozen python applications, as they don't include source code\r\n\r\n### CPython versions tested on:\r\n\r\n3.12, CPython main branch\r\n\r\n### Operating systems tested on:\r\n\r\nLinux, Windows\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-123102\n* gh-124021\n* gh-124024\n* gh-124131\n* gh-128581\n* gh-128600\n* gh-137914\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/Gatsik","@type":"Person","name":"Gatsik"},"datePublished":"2024-08-17T00:24:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":6},"url":"https://github.com/123085/cpython/issues/123085"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:384d7388-f016-bdee-d87c-cb73a350d3f7 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B0AC:2F92B0:AB57D:F47DB:696A0A76 |
| html-safe-nonce | 44ebfb69ed1b685b096d8f0cfebd22dd86a9f0e8cfa6421fdd657710492003db |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMEFDOjJGOTJCMDpBQjU3RDpGNDdEQjo2OTZBMEE3NiIsInZpc2l0b3JfaWQiOiI3MDk2NjI4MTU2MzM1NTI0NDcwIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | e4a169714df891cd66bb1b22570dc89bee870942614e39b1bae0ffd0c9a0ac7c |
| hovercard-subject-tag | issue:2471232696 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/123085/issue_layout |
| twitter:image | https://opengraph.githubassets.com/044e917ec4dab8f85fcdd88159fa06ae2cf7bbec868a0c9c52ce325033850e52/python/cpython/issues/123085 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/044e917ec4dab8f85fcdd88159fa06ae2cf7bbec868a0c9c52ce325033850e52/python/cpython/issues/123085 |
| og:image:alt | 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 f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Gatsik |
| hostname | github.com |
| expected-hostname | github.com |
| None | 699227a00bbb7fe1eec276d2ae1c3a93068bc5ba483bd9dc4b2a27a8f4f2f595 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7266b2d935baa1c6474b16dd9feaa5ca30607261 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width