python / python/cpython

PyImport_Import does not fall back to default builtins

Offen
#130,272 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

topic-C-API type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne bei den referenzierten Zeilen in import.c um 3914–3921 und erstelle den minimalen C-Reproducer für CPython 3.13. Prüfe, wie PyImport_Import Globals ohne builtins behandelt, und füge einen Regressionstest hinzu, der das Setup mit eingeschränkten Globals abdeckt. Als erledigt gilt die Aufgabe, wenn der Import die Standard-Builtins verwendet, statt aufgrund von fehlendem builtins fehlzuschlagen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
c, python
Bereich
api, backend
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.