From b933bf81f8c16de1e2f57e7cd289e160fd438ec7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 6 Mar 2024 23:31:31 +0200 Subject: [PATCH 1/3] gh-116437: Use new C API PyDict_Pop() to simplify the code --- Modules/_asynciomodule.c | 28 ++++++++++++++-------------- Modules/_csv.c | 5 +++-- Modules/_elementtree.c | 16 +++++----------- Modules/_threadmodule.c | 6 +----- Modules/posixmodule.c | 8 ++++---- Modules/timemodule.c | 28 ++++++++++++++-------------- Objects/moduleobject.c | 5 +++-- Objects/typeobject.c | 32 +++++++++++++++++--------------- Parser/asdl_c.py | 18 +++++++++--------- Python/Python-ast.c | 18 +++++++++--------- Python/bltinmodule.c | 5 +---- Python/bytecodes.c | 7 +++---- Python/executor_cases.c.h | 7 +++---- Python/generated_cases.c.h | 7 +++---- Python/pythonrun.c | 10 +++++----- 15 files changed, 94 insertions(+), 106 deletions(-) diff --git a/Modules/_asynciomodule.c b/Modules/_asynciomodule.c index c1aa849ecf1aad..29246cfa6afd00 100644 --- a/Modules/_asynciomodule.c +++ b/Modules/_asynciomodule.c @@ -2045,12 +2045,22 @@ static PyObject * swap_current_task(asyncio_state *state, PyObject *loop, PyObject *task) { PyObject *prev_task; + + if (task == Py_None) { + if (PyDict_Pop(state->current_tasks, loop, &prev_task) < 0) { + return NULL; + } + if (prev_task == NULL) { + Py_RETURN_NONE; + } + return prev_task; + } + Py_hash_t hash; hash = PyObject_Hash(loop); if (hash == -1) { return NULL; } - prev_task = _PyDict_GetItem_KnownHash(state->current_tasks, loop, hash); if (prev_task == NULL) { if (PyErr_Occurred()) { @@ -2059,22 +2069,12 @@ swap_current_task(asyncio_state *state, PyObject *loop, PyObject *task) prev_task = Py_None; } Py_INCREF(prev_task); - - if (task == Py_None) { - if (_PyDict_DelItem_KnownHash(state->current_tasks, loop, hash) == -1) { - goto error; - } - } else { - if (_PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash) == -1) { - goto error; - } + if (_PyDict_SetItem_KnownHash(state->current_tasks, loop, task, hash) == -1) { + Py_DECREF(prev_task); + return NULL; } return prev_task; - -error: - Py_DECREF(prev_task); - return NULL; } /* ----- Task */ diff --git a/Modules/_csv.c b/Modules/_csv.c index 660c5455af764e..277ea5c160ec97 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -1606,8 +1606,9 @@ _csv_unregister_dialect_impl(PyObject *module, PyObject *name) /*[clinic end generated code: output=0813ebca6c058df4 input=6b5c1557bf60c7e7]*/ { _csvstate *module_state = get_csv_state(module); - if (PyDict_DelItem(module_state->dialects, name) < 0) { - if (PyErr_ExceptionMatches(PyExc_KeyError)) { + int rc = PyDict_Pop(module_state->dialects, name, NULL); + if (rc <= 0) { + if (rc == 0) { PyErr_Format(module_state->error_obj, "unknown dialect"); } return NULL; diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index edd2f88a4881c3..aaa0cad76ae5c4 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -372,33 +372,27 @@ element_new(PyTypeObject *type, PyObject *args, PyObject *kwds) static PyObject* get_attrib_from_keywords(PyObject *kwds) { - PyObject *attrib_str = PyUnicode_FromString("attrib"); - if (attrib_str == NULL) { + PyObject *attrib; + if (PyDict_PopString(kwds, "attrib", &attrib) < 0) { return NULL; } - PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str); if (attrib) { /* If attrib was found in kwds, copy its value and remove it from * kwds */ if (!PyDict_Check(attrib)) { - Py_DECREF(attrib_str); PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s", Py_TYPE(attrib)->tp_name); + Py_DECREF(attrib); return NULL; } - attrib = PyDict_Copy(attrib); - if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) { - Py_SETREF(attrib, NULL); - } + Py_SETREF(attrib, PyDict_Copy(attrib)); } - else if (!PyErr_Occurred()) { + else { attrib = PyDict_New(); } - Py_DECREF(attrib_str); - if (attrib != NULL && PyDict_Update(attrib, kwds) < 0) { Py_DECREF(attrib); return NULL; diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 7587ac00eef60c..3da4a8c84548a8 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1263,11 +1263,7 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) remove the corresponding local dict */ if (self->dummies != NULL) { PyObject *ldict; - ldict = PyDict_GetItemWithError(self->dummies, dummyweakref); - if (ldict != NULL) { - PyDict_DelItem(self->dummies, dummyweakref); - } - if (PyErr_Occurred()) + if (PyDict_Pop(self->dummies, dummyweakref, &ldict) < 0) PyErr_WriteUnraisable((PyObject*)self); } Py_DECREF(self); diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 5e147dcfcd1e4e..3c23b64fa396b8 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -17556,11 +17556,11 @@ posixmodule_exec(PyObject *m) return -1; } - if (PyDict_DelItemString(dct, "pwritev") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "pwritev", NULL) == -1) { + return -1; } - if (PyDict_DelItemString(dct, "preadv") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "preadv", NULL) == -1) { + return -1; } } #endif diff --git a/Modules/timemodule.c b/Modules/timemodule.c index ed41ffd3662aa8..6ce7fbd00f6f18 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1928,20 +1928,20 @@ time_exec(PyObject *module) return -1; } - if (PyDict_DelItemString(dct, "clock_gettime") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "clock_gettime", NULL) == -1) { + return -1; } - if (PyDict_DelItemString(dct, "clock_gettime_ns") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "clock_gettime_ns", NULL) == -1) { + return -1; } - if (PyDict_DelItemString(dct, "clock_settime") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "clock_settime", NULL) == -1) { + return -1; } - if (PyDict_DelItemString(dct, "clock_settime_ns") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "clock_settime_ns", NULL) == -1) { + return -1; } - if (PyDict_DelItemString(dct, "clock_getres") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "clock_getres", NULL) == -1) { + return -1; } } #endif @@ -1951,11 +1951,11 @@ time_exec(PyObject *module) } else { PyObject* dct = PyModule_GetDict(module); - if (PyDict_DelItemString(dct, "thread_time") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "thread_time", NULL) == -1) { + return -1; } - if (PyDict_DelItemString(dct, "thread_time_ns") == -1) { - PyErr_Clear(); + if (PyDict_PopString(dct, "thread_time_ns", NULL) == -1) { + return -1; } } #endif diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 3a1c516658dce7..3083dfffa1681e 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -1004,10 +1004,11 @@ module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignor } else { /* delete */ - ret = PyDict_DelItem(dict, &_Py_ID(__annotations__)); - if (ret < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) { + ret = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL); + if (ret == 0) { PyErr_SetString(PyExc_AttributeError, "__annotations__"); } + ret = (ret > 0) ? 0 : -1; } exit: diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 181d0323284ebc..96c74c0e6fe715 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1236,20 +1236,21 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) } else { abstract = 0; - res = PyDict_DelItem(dict, &_Py_ID(__abstractmethods__)); - if (res && PyErr_ExceptionMatches(PyExc_KeyError)) { + res = PyDict_Pop(dict, &_Py_ID(__abstractmethods__), NULL); + if (res == 0) { PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__)); return -1; } } - if (res == 0) { - PyType_Modified(type); - if (abstract) - type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; - else - type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; + if (res < 0) { + return -1; } - return res; + PyType_Modified(type); + if (abstract) + type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; + else + type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT; + return 0; } static PyObject * @@ -1606,16 +1607,17 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context) result = PyDict_SetItem(dict, &_Py_ID(__annotations__), value); } else { /* delete */ - result = PyDict_DelItem(dict, &_Py_ID(__annotations__)); - if (result < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) { + result = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL); + if (result == 0) { PyErr_SetString(PyExc_AttributeError, "__annotations__"); + return -1; } } - - if (result == 0) { - PyType_Modified(type); + if (result < 0) { + return -1; } - return result; + PyType_Modified(type); + return 0; } static PyObject * diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 865fd76acf697d..59cc391881ab86 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -1077,20 +1077,20 @@ def visitModule(self, mod): if (!name) { goto cleanup; } - PyObject *value = PyDict_GetItemWithError(remaining_dict, name); + PyObject *value; + int rc = PyDict_Pop(remaining_dict, name, &value); + Py_DECREF(name); + if (rc < 0) { + goto cleanup; + } if (!value) { - if (PyErr_Occurred()) { - goto cleanup; - } break; } - if (PyList_Append(positional_args, value) < 0) { + rc = PyList_Append(positional_args, value); + Py_DECREF(value); + if (rc < 0) { goto cleanup; } - if (PyDict_DelItem(remaining_dict, name) < 0) { - goto cleanup; - } - Py_DECREF(name); } PyObject *args_tuple = PyList_AsTuple(positional_args); if (!args_tuple) { diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 46387493214829..7b591ddaa29869 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -5223,20 +5223,20 @@ ast_type_reduce(PyObject *self, PyObject *unused) if (!name) { goto cleanup; } - PyObject *value = PyDict_GetItemWithError(remaining_dict, name); + PyObject *value; + int rc = PyDict_Pop(remaining_dict, name, &value); + Py_DECREF(name); + if (rc < 0) { + goto cleanup; + } if (!value) { - if (PyErr_Occurred()) { - goto cleanup; - } break; } - if (PyList_Append(positional_args, value) < 0) { + rc = PyList_Append(positional_args, value); + Py_DECREF(value); + if (rc < 0) { goto cleanup; } - if (PyDict_DelItem(remaining_dict, name) < 0) { - goto cleanup; - } - Py_DECREF(name); } PyObject *args_tuple = PyList_AsTuple(positional_args); if (!args_tuple) { diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index b0074962b73799..f66a8c07c6f872 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -140,13 +140,10 @@ builtin___build_class__(PyObject *self, PyObject *const *args, Py_ssize_t nargs, goto error; } - if (PyDict_GetItemRef(mkw, &_Py_ID(metaclass), &meta) < 0) { + if (PyDict_Pop(mkw, &_Py_ID(metaclass), &meta) < 0) { goto error; } if (meta != NULL) { - if (PyDict_DelItem(mkw, &_Py_ID(metaclass)) < 0) { - goto error; - } /* metaclass is explicitly given, check if it's indeed a class */ isclass = PyType_Check(meta); } diff --git a/Python/bytecodes.c b/Python/bytecodes.c index bf0583d9c69014..0947a191e083a5 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1307,11 +1307,10 @@ dummy_func( inst(DELETE_GLOBAL, (--)) { PyObject *name = GETITEM(FRAME_CO_NAMES, oparg); - int err; - err = PyDict_DelItem(GLOBALS(), name); + int err = PyDict_Pop(GLOBALS(), name, NULL); // Can't use ERROR_IF here. - if (err != 0) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + if (err <= 0) { + if (err == 0) { _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, NAME_ERROR_MSG, name); } diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 4420c400e3a392..5d29dfdaa85872 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -1167,11 +1167,10 @@ case _DELETE_GLOBAL: { oparg = CURRENT_OPARG(); PyObject *name = GETITEM(FRAME_CO_NAMES, oparg); - int err; - err = PyDict_DelItem(GLOBALS(), name); + int err = PyDict_Pop(GLOBALS(), name, NULL); // Can't use ERROR_IF here. - if (err != 0) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + if (err <= 0) { + if (err == 0) { _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, NAME_ERROR_MSG, name); } diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index 6e8ca82b6db9dd..b3fce17aa9904e 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2368,11 +2368,10 @@ next_instr += 1; INSTRUCTION_STATS(DELETE_GLOBAL); PyObject *name = GETITEM(FRAME_CO_NAMES, oparg); - int err; - err = PyDict_DelItem(GLOBALS(), name); + int err = PyDict_Pop(GLOBALS(), name, NULL); // Can't use ERROR_IF here. - if (err != 0) { - if (_PyErr_ExceptionMatches(tstate, PyExc_KeyError)) { + if (err <= 0) { + if (err == 0) { _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, NAME_ERROR_MSG, name); } diff --git a/Python/pythonrun.c b/Python/pythonrun.c index f87c53fb28fbea..2970248da13705 100644 --- a/Python/pythonrun.c +++ b/Python/pythonrun.c @@ -452,7 +452,7 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit, v = run_pyc_file(pyc_fp, dict, dict, flags); } else { /* When running from stdin, leave __main__.__loader__ alone */ - if (PyUnicode_CompareWithASCIIString(filename, "") != 0 && + if ((!PyUnicode_Check(filename) || !PyUnicode_EqualToUTF8(filename, "")) && set_main_loader(dict, filename, "SourceFileLoader") < 0) { fprintf(stderr, "python: failed to set __main__.__loader__\n"); ret = -1; @@ -472,11 +472,11 @@ _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit, done: if (set_file_name) { - if (PyDict_DelItemString(dict, "__file__")) { - PyErr_Clear(); + if (PyDict_PopString(dict, "__file__", NULL) < 0) { + PyErr_Print(); } - if (PyDict_DelItemString(dict, "__cached__")) { - PyErr_Clear(); + if (PyDict_PopString(dict, "__cached__", NULL) < 0) { + PyErr_Print(); } } Py_XDECREF(main_module); From 989fd4f8880981f632c8e62b5db5b94fe2b92e4b Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Wed, 6 Mar 2024 23:50:31 +0200 Subject: [PATCH 2/3] Fix a leak in _localdummy_destroyed. --- Modules/_threadmodule.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index 3da4a8c84548a8..fc3669d29ea05e 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1262,8 +1262,7 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) /* If the thread-local object is still alive and not being cleared, remove the corresponding local dict */ if (self->dummies != NULL) { - PyObject *ldict; - if (PyDict_Pop(self->dummies, dummyweakref, &ldict) < 0) + if (PyDict_Pop(self->dummies, dummyweakref, NULL) < 0) PyErr_WriteUnraisable((PyObject*)self); } Py_DECREF(self); From 9109ccbcf015766096400094ca8f3ac86bbeba65 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 7 Mar 2024 10:01:56 +0200 Subject: [PATCH 3/3] Address review comments. --- Modules/_csv.c | 9 +++++---- Modules/_threadmodule.c | 3 ++- Modules/posixmodule.c | 4 ++-- Modules/timemodule.c | 14 +++++++------- Objects/moduleobject.c | 7 +++++-- Objects/typeobject.c | 2 ++ Python/bytecodes.c | 11 ++++++----- Python/executor_cases.c.h | 11 ++++++----- Python/generated_cases.c.h | 11 ++++++----- 9 files changed, 41 insertions(+), 31 deletions(-) diff --git a/Modules/_csv.c b/Modules/_csv.c index 277ea5c160ec97..ac948f417cebf5 100644 --- a/Modules/_csv.c +++ b/Modules/_csv.c @@ -1607,10 +1607,11 @@ _csv_unregister_dialect_impl(PyObject *module, PyObject *name) { _csvstate *module_state = get_csv_state(module); int rc = PyDict_Pop(module_state->dialects, name, NULL); - if (rc <= 0) { - if (rc == 0) { - PyErr_Format(module_state->error_obj, "unknown dialect"); - } + if (rc < 0) { + return NULL; + } + if (rc == 0) { + PyErr_Format(module_state->error_obj, "unknown dialect"); return NULL; } Py_RETURN_NONE; diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index fc3669d29ea05e..cc5396a035018f 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -1262,8 +1262,9 @@ _localdummy_destroyed(PyObject *localweakref, PyObject *dummyweakref) /* If the thread-local object is still alive and not being cleared, remove the corresponding local dict */ if (self->dummies != NULL) { - if (PyDict_Pop(self->dummies, dummyweakref, NULL) < 0) + if (PyDict_Pop(self->dummies, dummyweakref, NULL) < 0) { PyErr_WriteUnraisable((PyObject*)self); + } } Py_DECREF(self); Py_RETURN_NONE; diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 3c23b64fa396b8..2026e24648badb 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -17556,10 +17556,10 @@ posixmodule_exec(PyObject *m) return -1; } - if (PyDict_PopString(dct, "pwritev", NULL) == -1) { + if (PyDict_PopString(dct, "pwritev", NULL) < 0) { return -1; } - if (PyDict_PopString(dct, "preadv", NULL) == -1) { + if (PyDict_PopString(dct, "preadv", NULL) < 0) { return -1; } } diff --git a/Modules/timemodule.c b/Modules/timemodule.c index 6ce7fbd00f6f18..2ec5aff235c293 100644 --- a/Modules/timemodule.c +++ b/Modules/timemodule.c @@ -1928,19 +1928,19 @@ time_exec(PyObject *module) return -1; } - if (PyDict_PopString(dct, "clock_gettime", NULL) == -1) { + if (PyDict_PopString(dct, "clock_gettime", NULL) < 0) { return -1; } - if (PyDict_PopString(dct, "clock_gettime_ns", NULL) == -1) { + if (PyDict_PopString(dct, "clock_gettime_ns", NULL) < 0) { return -1; } - if (PyDict_PopString(dct, "clock_settime", NULL) == -1) { + if (PyDict_PopString(dct, "clock_settime", NULL) < 0) { return -1; } - if (PyDict_PopString(dct, "clock_settime_ns", NULL) == -1) { + if (PyDict_PopString(dct, "clock_settime_ns", NULL) < 0) { return -1; } - if (PyDict_PopString(dct, "clock_getres", NULL) == -1) { + if (PyDict_PopString(dct, "clock_getres", NULL) < 0) { return -1; } } @@ -1951,10 +1951,10 @@ time_exec(PyObject *module) } else { PyObject* dct = PyModule_GetDict(module); - if (PyDict_PopString(dct, "thread_time", NULL) == -1) { + if (PyDict_PopString(dct, "thread_time", NULL) < 0) { return -1; } - if (PyDict_PopString(dct, "thread_time_ns", NULL) == -1) { + if (PyDict_PopString(dct, "thread_time_ns", NULL) < 0) { return -1; } } diff --git a/Objects/moduleobject.c b/Objects/moduleobject.c index 3083dfffa1681e..9cd98fb4345fdd 100644 --- a/Objects/moduleobject.c +++ b/Objects/moduleobject.c @@ -1006,9 +1006,12 @@ module_set_annotations(PyModuleObject *m, PyObject *value, void *Py_UNUSED(ignor /* delete */ ret = PyDict_Pop(dict, &_Py_ID(__annotations__), NULL); if (ret == 0) { - PyErr_SetString(PyExc_AttributeError, "__annotations__"); + PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__annotations__)); + ret = -1; + } + else if (ret > 0) { + ret = 0; } - ret = (ret > 0) ? 0 : -1; } exit: diff --git a/Objects/typeobject.c b/Objects/typeobject.c index 96c74c0e6fe715..d8c3e920106bc3 100644 --- a/Objects/typeobject.c +++ b/Objects/typeobject.c @@ -1245,6 +1245,7 @@ type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context) if (res < 0) { return -1; } + PyType_Modified(type); if (abstract) type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT; @@ -1616,6 +1617,7 @@ type_set_annotations(PyTypeObject *type, PyObject *value, void *context) if (result < 0) { return -1; } + PyType_Modified(type); return 0; } diff --git a/Python/bytecodes.c b/Python/bytecodes.c index 0947a191e083a5..3276a4a9644b8f 100644 --- a/Python/bytecodes.c +++ b/Python/bytecodes.c @@ -1309,11 +1309,12 @@ dummy_func( PyObject *name = GETITEM(FRAME_CO_NAMES, oparg); int err = PyDict_Pop(GLOBALS(), name, NULL); // Can't use ERROR_IF here. - if (err <= 0) { - if (err == 0) { - _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, - NAME_ERROR_MSG, name); - } + if (err < 0) { + GOTO_ERROR(error); + } + if (err == 0) { + _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, + NAME_ERROR_MSG, name); GOTO_ERROR(error); } } diff --git a/Python/executor_cases.c.h b/Python/executor_cases.c.h index 5d29dfdaa85872..2e7b970b4ddb9e 100644 --- a/Python/executor_cases.c.h +++ b/Python/executor_cases.c.h @@ -1169,11 +1169,12 @@ PyObject *name = GETITEM(FRAME_CO_NAMES, oparg); int err = PyDict_Pop(GLOBALS(), name, NULL); // Can't use ERROR_IF here. - if (err <= 0) { - if (err == 0) { - _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, - NAME_ERROR_MSG, name); - } + if (err < 0) { + GOTO_ERROR(error); + } + if (err == 0) { + _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, + NAME_ERROR_MSG, name); GOTO_ERROR(error); } break; diff --git a/Python/generated_cases.c.h b/Python/generated_cases.c.h index b3fce17aa9904e..54c48617fd4df5 100644 --- a/Python/generated_cases.c.h +++ b/Python/generated_cases.c.h @@ -2370,11 +2370,12 @@ PyObject *name = GETITEM(FRAME_CO_NAMES, oparg); int err = PyDict_Pop(GLOBALS(), name, NULL); // Can't use ERROR_IF here. - if (err <= 0) { - if (err == 0) { - _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, - NAME_ERROR_MSG, name); - } + if (err < 0) { + GOTO_ERROR(error); + } + if (err == 0) { + _PyEval_FormatExcCheckArg(tstate, PyExc_NameError, + NAME_ERROR_MSG, name); GOTO_ERROR(error); } DISPATCH();