Title: missing global names inside a class body (calling `exec` with a `ChainMap`) · Issue #121306 · python/cpython · GitHub
Open Graph Title: missing global names inside a class body (calling `exec` with a `ChainMap`) · Issue #121306 · python/cpython
X Title: missing global names inside a class body (calling `exec` with a `ChainMap`) · Issue #121306 · python/cpython
Description: Bug report Bug description: I need to implement layered context running python code, so I choose ChainMap. But Python needs every globals passed to exec to be a dict, so I mixins it into ChainMap. This patch runs well as far, but in a cl...
Open Graph Description: Bug report Bug description: I need to implement layered context running python code, so I choose ChainMap. But Python needs every globals passed to exec to be a dict, so I mixins it into ChainMap. ...
X Description: Bug report Bug description: I need to implement layered context running python code, so I choose ChainMap. But Python needs every globals passed to exec to be a dict, so I mixins it into ChainMap. ...
Opengraph URL: https://github.com/python/cpython/issues/121306
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"missing global names inside a class body (calling `exec` with a `ChainMap`)","articleBody":"# Bug report\n\n### Bug description:\n\nI need to implement layered context running python code, so I choose `ChainMap`. But Python needs every `globals` passed to `exec` to be a `dict`, so I mixins it into `ChainMap`.\n\nThis patch runs well as far, but in a class it can't get the value from the outside.\n\nI think this may because in the class context CPython uses some function like `PyDict_Get` instead of `__getitem__`, so the values didn't get copied into the class's globals context.\n\nThis bug caused this code fails:\n\n```python\nfrom collections import ChainMap\n\nclass ChainMap(ChainMap, dict): # globals must be a real dict\n pass\n\nsource = \"\"\"\na = 1\n\nclass A:\n print(a)\n\"\"\"\n\nexec(source, ChainMap()) # this line raises NameError\n```\n\n\u003cdetails\u003e\u003csummary\u003eOther reproduction approaches\u003c/summary\u003e\n\u003cp\u003e\n\nThis is reproduceable in [`pyodide`](https://github.com/pyodide/pyodide) (a wasm port of CPython 3.12.1), so you can run the code above by just clicking one of the following link:\n\n- [Stackblitz](https://stackblitz.com/edit/chainmap-class-locals-issue?file=main.py) - you can edit code and the result will sync instantly\n- [Python Online](https://pyth-on-line.vercel.app/console#ZnJvbSBjb2xsZWN0aW9ucyBpbXBvcnQgQ2hhaW5NYXAKCgpjbGFzcyBDaGFpbk1hcChDaGFpbk1hcCwgZGljdCk6ICAjIGdsb2JhbHMgbXVzdCBiZSBhIHJlYWwgZGljdAogICAgcGFzcwoKCnNvdXJjZSA9ICIiIgoKY2xhc3MgQToKICAgIHByaW50KGEpCgoiIiIKCgpleGVjKHNvdXJjZSwgeyJhIjogMX0pCgoKZXhlYyhzb3VyY2UsIENoYWluTWFwKHsiYSI6IDJ9KSkgICMgdGhpcyBsaW5lIHJhaXNlcyBlcnJvcgo=) - this is faster but just has a console instead of an IDE\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n### CPython versions tested on:\n\n3.12\n\n### Operating systems tested on:\n\nLinux, Windows\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-121389\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/CNSeniorious000","@type":"Person","name":"CNSeniorious000"},"datePublished":"2024-07-03T06:59:48.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":19},"url":"https://github.com/121306/cpython/issues/121306"}
| 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:232c23a8-7129-46c2-8d02-22f794173294 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9122:137035:D431A:128FD2:696A02DA |
| html-safe-nonce | 233e2147effa2ec4cffc2a6f50bc311ba4e2e9980713f1842cf9aaad2af674d1 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5MTIyOjEzNzAzNTpENDMxQToxMjhGRDI6Njk2QTAyREEiLCJ2aXNpdG9yX2lkIjoiMTk5NjYxMDc0NzI2NDI3MTA2NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 35a9be58ce6a333e18ae82d26c11d7ae4c5bda1b14089a624d1994d5ad3fde4e |
| hovercard-subject-tag | issue:2387836244 |
| 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/121306/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a6ef44cd876342722d3a7038367b21d68f454cc89b89bd110969a363189c11c6/python/cpython/issues/121306 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a6ef44cd876342722d3a7038367b21d68f454cc89b89bd110969a363189c11c6/python/cpython/issues/121306 |
| og:image:alt | Bug report Bug description: I need to implement layered context running python code, so I choose ChainMap. But Python needs every globals passed to exec to be a dict, so I mixins it into ChainMap. ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | CNSeniorious000 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 578c119ff0247c8b2f2491fbf4fc0395cdf909d4df66598cebdc96ddfc4418dc |
| 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 | 671c2f67171dbced24284331f3133a613d08c366 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width