diff --git a/Objects/frameobject.c b/Objects/frameobject.c index a66ca175d2cc2a..8004f9fc5edec7 100644 --- a/Objects/frameobject.c +++ b/Objects/frameobject.c @@ -2106,11 +2106,13 @@ PyObject* _PyEval_BuiltinsFromGlobals(PyThreadState *tstate, PyObject *globals) { PyObject *builtins; - int has_builtins = PyMapping_GetOptionalItem(globals, &_Py_ID(__builtins__), &builtins); + int has_builtins = PyMapping_GetOptionalItem( + globals, &_Py_ID(__builtins__), &builtins); if (has_builtins < 0) { return NULL; } if (has_builtins) { + /* release reference right away since we mean to only borrow it */ Py_DECREF(builtins); if (PyModule_Check(builtins)) { builtins = _PyModule_GetDict(builtins); diff --git a/Python/bytecodes.c b/Python/bytecodes.c index dd06d8c869633e..61850aff1ab722 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1476,7 +1476,8 @@ dummy_func( int err = PyMapping_DelItem(GLOBALS(), name); // Can't use ERROR_IF here. if (err < 0) { - if (PyErr_ExceptionMatches(PyExc_KeyError)) { + if (_PyErr_Occurred(tstate) && + _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, NAME_ERROR_MSG, name); } diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index a1b9accb8e7e7d..6cf910afc52ad5 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -1524,7 +1524,8 @@ int err = PyMapping_DelItem(GLOBALS(), name); // Can't use ERROR_IF here. if (err < 0) { - if (PyErr_ExceptionMatches(PyExc_KeyError)) { + if (_PyErr_Occurred(tstate) && + _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, NAME_ERROR_MSG, name); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 0291d81298b044..760d759402e1bb 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2781,7 +2781,8 @@ int err = PyMapping_DelItem(GLOBALS(), name); // Can't use ERROR_IF here. if (err < 0) { - if (PyErr_ExceptionMatches(PyExc_KeyError)) { + if (_PyErr_Occurred(tstate) && + _PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, NAME_ERROR_MSG, name); }