Title: LOAD_ATTR_MODULE bypasses built-in data descriptors on exact module objects · Issue #162565 · python/cpython · GitHub
Open Graph Title: LOAD_ATTR_MODULE bypasses built-in data descriptors on exact module objects · Issue #162565 · python/cpython
X Title: LOAD_ATTR_MODULE bypasses built-in data descriptors on exact module objects · Issue #162565 · python/cpython
Description: Bug report On current main at 79494e2, warmed attribute reads on an exact types.ModuleType object can return dictionary entries named __dict__ or __class__ instead of the built-in data descriptor results. Cold reads and getattr() return ...
Open Graph Description: Bug report On current main at 79494e2, warmed attribute reads on an exact types.ModuleType object can return dictionary entries named __dict__ or __class__ instead of the built-in data descriptor r...
X Description: Bug report On current main at 79494e2, warmed attribute reads on an exact types.ModuleType object can return dictionary entries named __dict__ or __class__ instead of the built-in data descriptor r...
Opengraph URL: https://github.com/python/cpython/issues/162565
X: @github
Domain: github.com
IPv4 address: 140.82.112.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"LOAD_ATTR_MODULE bypasses built-in data descriptors on exact module objects","articleBody":"### Bug report\n\nOn current `main` at 79494e2b0f7e9e1cc3a39fe035e67cea33c7ef0a, warmed attribute reads on an **exact** `types.ModuleType` object can return dictionary entries named `__dict__` or `__class__` instead of the built-in data descriptor results. Cold reads and `getattr()` return the descriptor results on the same object.\n\n```python\nimport dis\nimport types\n\nmodule = types.ModuleType(\"example\")\nnamespace = vars(module)\nnamespace[\"__dict__\"] = \"shadow dict\"\nnamespace[\"__class__\"] = \"shadow class\"\n\n\ndef read_dict(owner):\n return owner.__dict__\n\n\ndef read_class(owner):\n return owner.__class__\n\n\nprint(\"Exact module:\", type(module) is types.ModuleType)\nfor name, read, expected in (\n (\"__dict__\", read_dict, namespace),\n (\"__class__\", read_class, types.ModuleType),\n):\n print(name)\n print(\" cold uses descriptor:\", read(module) is expected)\n for _ in range(100):\n read(module)\n print(\" warm uses descriptor:\", read(module) is expected)\n print(\" warm returns shadow:\", read(module) is namespace[name])\n print(\" getattr uses descriptor:\", getattr(module, name) is expected)\n print(\" attribute opcodes:\", [\n op.opname for op in dis.get_instructions(read, adaptive=True)\n if \"LOAD_ATTR\" in op.opname\n ])\n```\n\nObserved output:\n\n```text\nExact module: True\n__dict__\n cold uses descriptor: True\n warm uses descriptor: False\n warm returns shadow: True\n getattr uses descriptor: True\n attribute opcodes: ['LOAD_ATTR_MODULE']\n__class__\n cold uses descriptor: True\n warm uses descriptor: False\n warm returns shadow: True\n getattr uses descriptor: True\n attribute opcodes: ['LOAD_ATTR_MODULE']\n```\n\nExpected: both cold and warmed reads preserve descriptor precedence. `__dict__` should return the module namespace and `__class__` should return `types.ModuleType`, regardless of these dictionary entries. Thus `warm uses descriptor` should be `True`, and `warm returns shadow` should be `False` for both names.\n\nThis is distinct from #156399: no module subclass or user-defined descriptor is involved. Its fix, #156474, is already an ancestor of the tested commit and restricts module specialization to exact modules. The existing `test_load_attr_module_subclass_with_data_descriptor` passes on this build while the example above still produces the wrong warmed results.\n\nIn the tested source, [module specialization](https://github.com/python/cpython/blob/79494e2b0f7e9e1cc3a39fe035e67cea33c7ef0a/Python/specialize.c#L413-L446) selects the dictionary entry without checking for a data descriptor on the module type. The [specialized instruction](https://github.com/python/cpython/blob/79494e2b0f7e9e1cc3a39fe035e67cea33c7ef0a/Python/bytecodes.c#L2893-L2916) then reads that entry directly.\n\n### CPython versions tested on\n\nCPython main branch, `3.16.0a2+dev`, clean checkout at [79494e2b0f7](https://github.com/python/cpython/commit/79494e2b0f7e9e1cc3a39fe035e67cea33c7ef0a), fetched and built on 2026-11-24. Released versions were not checked for this report.\n\n### Operating systems tested on\n\nmacOS 27.2.1, ARM64, Apple Clang 21.0.0. Fresh debug build configured with `--with-pydebug --without-ensurepip`; GIL enabled, JIT unavailable. The example was run with `./python.exe -I repro.py`.\n\nThe related subclass control was run with:\n\n```sh\n./python.exe -I -m test test_opcache -m test_load_attr_module_subclass_with_data_descriptor -v\n```\n\nResult: one test passed; this was a focused check, not a full test-suite run.\n\nPrepared with Codex assistance; the included reproducer and subclass control were executed on the stated upstream checkout.\n\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162568\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/lagergren-oai","@type":"Person","name":"lagergren-oai"},"datePublished":"2026-11-24T16:29:54Z","url":"https://github.com/python/cpython/issues/162565"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| 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:3610764d-e9a3-9b8a-4b9e-7dd2f63f21d2 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D210:3FD98D:B744B5:F39DB7:6B06420C |
| html-safe-nonce | 01a3a14b1ed817f13fb03fff00784ef128ef615b9912478f661c2371e801a46c |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMjEwOjNGRDk4RDpCNzQ0QjU6RjM5REI3OjZCMDY0MjBDIiwidmlzaXRvcl9pZCI6Ijc1Nzk4Mjk4NDQyMjY0OTA4OTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 26eaf6c370540ae6bb51e671d6b88a40e45ad5993b8b18bc4abd4e1a5005af1c |
| hovercard-subject-tag | issue:6586542985 |
| 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/162565/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a75cc9c82d1d2dc440c81b577d6eded294a486c113ba04df27ed8a7ba40fa22f/python/cpython/issues/162565 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a75cc9c82d1d2dc440c81b577d6eded294a486c113ba04df27ed8a7ba40fa22f/python/cpython/issues/162565 |
| og:image:alt | Bug report On current main at 79494e2, warmed attribute reads on an exact types.ModuleType object can return dictionary entries named __dict__ or __class__ instead of the built-in data descriptor r... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | lagergren-oai |
| hostname | github.com |
| expected-hostname | github.com |
| None | 1bff4f6bfaa96dc9c42cacf315424c24ebb548b06142ced5e3aaccb2a42dd652 |
| 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 | ac613d3698d3d8565651455086e1f74da07bf277 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width