PyImport_Import does not fall back to default builtins
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia dalle righe referenziate di import.c intorno a 3914-3921 e crea il riproduttore minimo in C con CPython 3.13. Verifica come PyImport_Import gestisce i globals senza builtins, quindi aggiungi un test di regressione che copra la configurazione con globals limitati. Il lavoro è completato quando l'importazione riesce usando i builtins predefiniti invece di fallire per l'assenza di builtins.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- c, python
- Ambito
- api, backend
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100