`exec` and `eval` incorrectly accepting subclass of `dict` as `globals`
@rhettinger 已经在做这个了。
开始于 2025年3月31日。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
According to Python docs, exec and eval take as their globals argument an exact dict, and not a subclass of it.
If only globals is provided, it must be a dictionary (and not a subclass of dictionary), which will be used for both the global and the local variables. If globals and locals are given, they are used for the global and local variables, respectively. If provided, locals can be any mapping object.
However, builtin_exec_impl allows subclasses of dict because it checks the argument with PyDict_Check instead of PyDict_CheckExact (see here).
builtin_eval_impl makes the same, wrong, validation, with a slightly different error message. (see here).
This is a problem because it allows developers to pass a dictionary with a custom __getitem__ method which will never actually get called. It will not get called because when opcode LOAD_NAME is executed, it assumes globals is a dict, and accesses it using PyDict_GetItemRef instead of PyMapping_GetOptionalItem (see here).
Furthermore, opcode LOAD_GLOBAL behaves slightly different, first checking if globals is an exact dict and then choosing how to access it.
Here is an example of the inconsistent and unexpected behavior:
class mydict(dict):
def __getitem__(self, key):
print('accessing', key)
if key == 'f': return "hi!"
return super().__getitem__(key)
exec('global f; print(f)', mydict(), {}) # This works, it finds f
exec('print(f)', mydict(), {}) # This, surprisingly, does not work
exec('print(f)', mydict()) # But this does work, because locals is a copy of globals and it supports the custom __getitem__
There even is a test (test_builtin.py, see here) that says "custom globals or builtins can raise errors on item access". The test passes only because a locals is not provided to exec, so exec copies it from globals. A custom globals cannot raise errors on item access.
If one applies the change I think must be made (Changing PyDict_Check to PyDict_CheckExact in exec and eval's implementations) 4 more tests break.
test_getpathpasses custom dictionaries toexec(which, remember, the documentation forbids), and should be rewritten but does not seem to be very complex to do.test_descrtutshould changeexec("x = 3; print(x)", a)toexec("x = 3; print(x)", {}, a), and then not expect 'builtins' to appear ina.keys()test_dynamic'stest_load_global_specialization_failure_keeps_opargshould be rewritten to, instead of creating a class with__missing__, create a dictionary with data (for i in range(variables): my_globals[f"_number_{i}"] = i)test_type_aliases.test_exec_with_unusual_globalsfails, but I do not understand how to fix it.
Alternatively, _PyEval_LoadName could be modified (and the documentation updated) so subclasses of dictionaries are supported as globals, but this is a hot path and I have not measured any possible performance implications. Modifying exec to comply with the docs makes more sense, IMHO.
CPython versions tested on:
CPython main branch, 3.12, 3.10, 3.9
Operating systems tested on:
Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
评估
这个 Issue 还没有评估数据。