diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 54451081211654..4f707b577220d0 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -376,9 +376,11 @@ get_attrib_from_keywords(PyObject *kwds) if (attrib_str == NULL) { return NULL; } - PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str); - - if (attrib) { + PyObject *attrib; + if (PyDict_GetItemRef(kwds, attrib_str, &attrib) == 0) { + attrib = PyDict_New(); + } + else if (attrib) { /* If attrib was found in kwds, copy its value and remove it from * kwds */ @@ -386,16 +388,14 @@ get_attrib_from_keywords(PyObject *kwds) 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); + Py_SETREF(attrib, PyDict_Copy(attrib)); if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) { Py_SETREF(attrib, NULL); } } - else if (!PyErr_Occurred()) { - attrib = PyDict_New(); - } Py_DECREF(attrib_str); @@ -1427,11 +1427,14 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key, { if (self->extra && self->extra->attrib) { PyObject *attrib = Py_NewRef(self->extra->attrib); - PyObject *value = Py_XNewRef(PyDict_GetItemWithError(attrib, key)); - Py_DECREF(attrib); - if (value != NULL || PyErr_Occurred()) { + PyObject *value; + if (PyDict_GetItemRef(attrib, key, &value) != 0) { + // found or error + Py_DECREF(attrib); return value; } + // not found + Py_DECREF(attrib); } return Py_NewRef(default_value); @@ -3092,9 +3095,7 @@ makeuniversal(XMLParserObject* self, const char* string) if (!key) return NULL; - value = Py_XNewRef(PyDict_GetItemWithError(self->names, key)); - - if (value == NULL && !PyErr_Occurred()) { + if (PyDict_GetItemRef(self->names, key, &value) == 0) { /* new name. convert to universal name, and decode as necessary */