python / python/cpython

PyImport_Import does not fall back to default builtins

オープン
#130,272 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

import.c の 3914-3921 行付近にある参照箇所から始め、CPython 3.13 に対する最小限の C 再現コードを作成します。PyImport_Import が builtins のない globals をどのように処理するかを確認し、limited-globals のセットアップを対象とする回帰テストを追加します。完了条件は、builtins がないために失敗するのではなく、デフォルトの builtins を使って import が成功することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
c, python
領域
api, backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。