Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.12] gh-112716: Fix SystemError when __builtins__ is not a dict (GH-112770) #113103

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[3.12] gh-112716: Fix SystemError when __builtins__ is not a dict (GH…
…-112770)

It was raised in two cases:
* in the import statement when looking up __import__
* in pickling some builtin type when looking up built-ins iter, getattr, etc.
(cherry picked from commit 1161c14)

Co-authored-by: Serhiy Storchaka <[email protected]>
  • Loading branch information
serhiy-storchaka committed Dec 14, 2023
commit 40b42e1bba74db0f49b9c6b821981a32cc285a46
26 changes: 26 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,32 @@ class customdict(dict): # this one should not do anything fancy
self.assertRaisesRegex(NameError, "name 'superglobal' is not defined",
exec, code, {'__builtins__': customdict()})

def test_eval_builtins_mapping(self):
code = compile("superglobal", "test", "eval")
# works correctly
ns = {'__builtins__': types.MappingProxyType({'superglobal': 1})}
self.assertEqual(eval(code, ns), 1)
# custom builtins mapping is missing key
ns = {'__builtins__': types.MappingProxyType({})}
self.assertRaisesRegex(NameError, "name 'superglobal' is not defined",
eval, code, ns)

def test_exec_builtins_mapping_import(self):
code = compile("import foo.bar", "test", "exec")
ns = {'__builtins__': types.MappingProxyType({})}
self.assertRaisesRegex(ImportError, "__import__ not found", exec, code, ns)
ns = {'__builtins__': types.MappingProxyType({'__import__': lambda *args: args})}
exec(code, ns)
self.assertEqual(ns['foo'], ('foo.bar', ns, ns, None, 0))

def test_eval_builtins_mapping_reduce(self):
# list_iterator.__reduce__() calls _PyEval_GetBuiltin("iter")
code = compile("x.__reduce__()", "test", "eval")
ns = {'__builtins__': types.MappingProxyType({}), 'x': iter([1, 2])}
self.assertRaisesRegex(AttributeError, "iter", eval, code, ns)
ns = {'__builtins__': types.MappingProxyType({'iter': iter}), 'x': iter([1, 2])}
self.assertEqual(eval(code, ns), (iter, ([1, 2],), 0))

def test_exec_redirected(self):
savestdout = sys.stdout
sys.stdout = None # Whatever that cannot flush()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix SystemError in the ``import`` statement and in ``__reduce__()`` methods
of builtin types when ``__builtins__`` is not a dict.
14 changes: 5 additions & 9 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -2308,11 +2308,8 @@ PyObject *
_PyEval_GetBuiltin(PyObject *name)
{
PyThreadState *tstate = _PyThreadState_GET();
PyObject *attr = PyDict_GetItemWithError(PyEval_GetBuiltins(), name);
if (attr) {
Py_INCREF(attr);
}
else if (!_PyErr_Occurred(tstate)) {
PyObject *attr = PyObject_GetItem(PyEval_GetBuiltins(), name);
if (attr == NULL && _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
_PyErr_SetObject(tstate, PyExc_AttributeError, name);
}
return attr;
Expand Down Expand Up @@ -2467,16 +2464,17 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
PyObject *import_func, *res;
PyObject* stack[5];

import_func = _PyDict_GetItemWithError(frame->f_builtins, &_Py_ID(__import__));
import_func = PyObject_GetItem(frame->f_builtins, &_Py_ID(__import__));
if (import_func == NULL) {
if (!_PyErr_Occurred(tstate)) {
if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) {
_PyErr_SetString(tstate, PyExc_ImportError, "__import__ not found");
}
return NULL;
}
PyObject *locals = frame->f_locals;
/* Fast path for not overloaded __import__. */
if (_PyImport_IsDefaultImportFunc(tstate->interp, import_func)) {
Py_DECREF(import_func);
int ilevel = _PyLong_AsInt(level);
if (ilevel == -1 && _PyErr_Occurred(tstate)) {
return NULL;
Expand All @@ -2490,8 +2488,6 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
return res;
}

Py_INCREF(import_func);

stack[0] = name;
stack[1] = frame->f_globals;
stack[2] = locals == NULL ? Py_None : locals;
Expand Down
Loading