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