PyImport_Import does not fall back to default builtins
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Bug report
Bug description:
When importing a module with PyImport_Import() inside an already running python which does have limited globals, the import fails due to missing __builtins__: https://github.com/python/cpython/blob/01ba7df49966eaf14f44962a77898840c70dda96/Python/import.c#L3914-L3921
It feels similar to this issue: https://github.com/python/cpython/pull/24564 but with a slightly different setup.
Originally reported for pyo3 here: https://github.com/PyO3/pyo3/issues/4913
Minimal reproducer below, fuller reproducer can be found here: https://gitlab.com/nertpinx/cpython_repr
#include <Python.h>
PyObject *
func(PyObject *self __attribute__((unused)),
PyObject *arg)
{
return PyImport_Import(arg);
}
static PyMethodDef method_def = {
.ml_name = "func",
.ml_meth = func,
.ml_flags = METH_NOARGS,
.ml_doc = "Reproducer function",
};
int
main(int argc __attribute__((unused)),
char **argv __attribute__((unused)))
{
Py_InitializeEx(0);
PyObject *program = NULL;
PyObject *globals = NULL;
PyObject *supermodule = NULL;
PyObject *py_func = NULL;
supermodule = PyUnicode_FromString("supermodule");
py_func = PyCFunction_NewEx(&method_def, NULL, supermodule);
globals = PyDict_New();
PyDict_SetItemString(globals, "func", py_func);
program = Py_CompileString("func()", "<string>", Py_file_input);
PyEval_EvalCode(program, globals, NULL);
if (PyErr_Occurred()) {
PyErr_Print();
return 1;
}
return 0;
}
CPython versions tested on:
3.13
Operating systems tested on:
Linux
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
从 import.c 中约 3914-3921 行的参考位置开始,针对 CPython 3.13 构建最小 C 复现程序。检查 PyImport_Import 如何处理不包含 builtins 的 globals,然后添加一个覆盖 limited-globals 设置的回归测试。完成的标准是:import 使用默认 builtins 成功,而不是因缺少 builtins 而失败。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- c, python
- 领域
- api, backend
- Issue 类型
- 缺陷
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100