diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2023-12-14 12:54:25 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-14 12:54:25 (GMT) |
commit | 59a22d3e50be0900a74fb7a398c3622e2a43b11e (patch) | |
tree | 0f1b5dfc0ecc4a431a4fd64203caed84b71bf7c3 /Python | |
parent | 913051d2a00eaa98c35fe06675e40acd6d87393f (diff) | |
download | cpython-59a22d3e50be0900a74fb7a398c3622e2a43b11e.zip cpython-59a22d3e50be0900a74fb7a398c3622e2a43b11e.tar.gz cpython-59a22d3e50be0900a74fb7a398c3622e2a43b11e.tar.bz2 |
[3.12] gh-112716: Fix SystemError when __builtins__ is not a dict (GH-112770) (GH-113103)
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 1161c14e8c68296fc465cd48970b32be9bee012e)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/ceval.c | 14 |
1 files changed, 5 insertions, 9 deletions
diff --git a/Python/ceval.c b/Python/ceval.c index 4845ec0..6110883 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -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; @@ -2467,9 +2464,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; @@ -2477,6 +2474,7 @@ import_name(PyThreadState *tstate, _PyInterpreterFrame *frame, 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; @@ -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; |