python / python/cpython

PyImport_Import does not fall back to default builtins

未關閉
#130,272 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

還沒有人認領這個 Issue。

topic-C-API type-bug
主要語言
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

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 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

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。