missing global names inside a class body (calling `exec` with a `ChainMap`)
還沒有人認領這個 Issue。
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 35.9k
- PR 合併指標
- PR 指標待擷取
描述
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 class it can't get the value from the outside.
I 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.
This bug caused this code fails:
from collections import ChainMap
class ChainMap(ChainMap, dict): # globals must be a real dict
pass
source = """
a = 1
class A:
print(a)
"""
exec(source, ChainMap()) # this line raises NameError
Other reproduction approaches
This is reproduceable in pyodide (a wasm port of CPython 3.12.1), so you can run the code above by just clicking one of the following link:
- Stackblitz - you can edit code and the result will sync instantly
- Python Online - this is faster but just has a console instead of an IDE
CPython versions tested on:
3.12
Operating systems tested on:
Linux, Windows
Linked PRs
- gh-121389
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
首先,在 CPython 3.12 上使用類別 A 範例重現 exec(source, ChainMap()) 處的失敗,然後檢查類別本體名稱查找路徑。將其行為與真正的 dict 進行比較,並檢視連結的 PR gh-121389;完成的標準是外部名稱可在類別本體內使用,同時不回歸現有的 exec 行為。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- compilers
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 停滯
- 描述清晰度
- 基本清楚
- 新手友好度
- 25/100