[3.11] gh-112716: Fix SystemError when __builtins__ is not a dict (GH-112770) (GH-113105)

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 1161c14e8c)
This commit is contained in:
Serhiy Storchaka 2023-12-14 14:59:33 +02:00 committed by GitHub
parent 08ff6fa23d
commit d4234937a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 9 deletions

View file

@ -7153,11 +7153,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;
@ -7407,9 +7404,9 @@ 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;
@ -7417,6 +7414,7 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame,
PyObject *locals = frame->f_locals;
/* Fast path for not overloaded __import__. */
if (import_func == tstate->interp->import_func) {
Py_DECREF(import_func);
int ilevel = _PyLong_AsInt(level);
if (ilevel == -1 && _PyErr_Occurred(tstate)) {
return NULL;
@ -7430,8 +7428,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;