Skip to content

Commit 69109c6

Browse files
Add _PyEval_EnsureBuiltinsWithModule().
1 parent 2bf3a4b commit 69109c6

File tree

4 files changed

+70
-31
lines changed

4 files changed

+70
-31
lines changed

Include/internal/pycore_ceval.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,10 @@ extern PyObject * _PyEval_GetGlobalsFromRunningMain(PyThreadState *);
243243
extern int _PyEval_EnsureBuiltins(
244244
PyThreadState *,
245245
PyObject *,
246-
int usemod,
246+
PyObject **p_builtins);
247+
extern int _PyEval_EnsureBuiltinsWithModule(
248+
PyThreadState *,
249+
PyObject *,
247250
PyObject **p_builtins);
248251

249252
PyAPI_FUNC(PyObject *)_Py_MakeCoro(PyFunctionObject *func);

Python/bltinmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
10101010
locals = Py_NewRef(globals);
10111011
}
10121012

1013-
if (_PyEval_EnsureBuiltins(tstate, globals, 0, NULL) < 0) {
1013+
if (_PyEval_EnsureBuiltins(tstate, globals, NULL) < 0) {
10141014
goto error;
10151015
}
10161016

@@ -1134,7 +1134,7 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
11341134
goto error;
11351135
}
11361136

1137-
if (_PyEval_EnsureBuiltins(tstate, globals, 0, NULL) < 0) {
1137+
if (_PyEval_EnsureBuiltins(tstate, globals, NULL) < 0) {
11381138
goto error;
11391139
}
11401140

Python/ceval.c

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,50 +2779,86 @@ _PyEval_GetGlobalsFromRunningMain(PyThreadState *tstate)
27792779
return globals;
27802780
}
27812781

2782-
int
2783-
_PyEval_EnsureBuiltins(PyThreadState *tstate, PyObject *globals, int usemod,
2784-
PyObject **p_builtins)
2782+
static PyObject *
2783+
get_globals_builtins(PyObject *globals)
27852784
{
27862785
PyObject *builtins = NULL;
27872786
if (PyDict_Check(globals)) {
27882787
if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) {
2789-
return -1;
2788+
return NULL;
27902789
}
27912790
}
27922791
else {
27932792
if (PyMapping_GetOptionalItem(
27942793
globals, &_Py_ID(__builtins__), &builtins) < 0)
27952794
{
2795+
return NULL;
2796+
}
2797+
}
2798+
return builtins;
2799+
}
2800+
2801+
static int
2802+
set_globals_builtins(PyObject *globals, PyObject *builtins)
2803+
{
2804+
if (PyDict_Check(globals)) {
2805+
if (PyDict_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) {
2806+
return -1;
2807+
}
2808+
}
2809+
else {
2810+
if (PyObject_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) {
27962811
return -1;
27972812
}
27982813
}
2814+
return 0;
2815+
}
2816+
2817+
int
2818+
_PyEval_EnsureBuiltins(PyThreadState *tstate, PyObject *globals,
2819+
PyObject **p_builtins)
2820+
{
2821+
PyObject *builtins = get_globals_builtins(globals);
27992822
if (builtins == NULL) {
2800-
if (usemod) {
2801-
builtins =
2802-
PyImport_ImportModuleLevel("builtins", NULL, NULL, NULL, 0);
2803-
if (builtins == NULL) {
2804-
return -1;
2805-
}
2823+
if (_PyErr_Occurred(tstate)) {
2824+
return -1;
28062825
}
2807-
else {
2808-
builtins = PyEval_GetBuiltins();
2809-
if (builtins == NULL) {
2810-
assert(_PyErr_Occurred(tstate));
2811-
return -1;
2812-
}
2813-
Py_INCREF(builtins);
2826+
builtins = PyEval_GetBuiltins(); // borrowed
2827+
if (builtins == NULL) {
2828+
assert(_PyErr_Occurred(tstate));
2829+
return -1;
28142830
}
2815-
if (PyDict_Check(globals)) {
2816-
if (PyDict_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) {
2817-
Py_DECREF(builtins);
2818-
return -1;
2819-
}
2831+
Py_INCREF(builtins);
2832+
if (set_globals_builtins(globals, builtins) < 0) {
2833+
Py_DECREF(builtins);
2834+
return -1;
28202835
}
2821-
else {
2822-
if (PyObject_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) {
2823-
Py_DECREF(builtins);
2824-
return -1;
2825-
}
2836+
}
2837+
if (p_builtins != NULL) {
2838+
*p_builtins = builtins;
2839+
}
2840+
else {
2841+
Py_DECREF(builtins);
2842+
}
2843+
return 0;
2844+
}
2845+
2846+
int
2847+
_PyEval_EnsureBuiltinsWithModule(PyThreadState *tstate, PyObject *globals,
2848+
PyObject **p_builtins)
2849+
{
2850+
PyObject *builtins = get_globals_builtins(globals);
2851+
if (builtins == NULL) {
2852+
if (_PyErr_Occurred(tstate)) {
2853+
return -1;
2854+
}
2855+
builtins = PyImport_ImportModuleLevel("builtins", NULL, NULL, NULL, 0);
2856+
if (builtins == NULL) {
2857+
return -1;
2858+
}
2859+
if (set_globals_builtins(globals, builtins) < 0) {
2860+
Py_DECREF(builtins);
2861+
return -1;
28262862
}
28272863
}
28282864
if (p_builtins != NULL) {

Python/import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3979,7 +3979,7 @@ PyImport_Import(PyObject *module_name)
39793979
if (globals == NULL) {
39803980
goto err;
39813981
}
3982-
if (_PyEval_EnsureBuiltins(tstate, globals, 1, &builtins) < 0) {
3982+
if (_PyEval_EnsureBuiltinsWithModule(tstate, globals, &builtins) < 0) {
39833983
goto err;
39843984
}
39853985
}

0 commit comments

Comments
 (0)