From 1b0d6565e701bcdbca21fe5b8621259e6e3d9bf7 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Tue, 14 Nov 2023 21:11:05 +0200 Subject: [PATCH] gh-111789: Use PyDict_GetItemRef() in Modules/_elementtree.c --- Modules/_elementtree.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index f9d5793f9b6497..5d02946d23ef5f 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -372,9 +372,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 */ @@ -382,16 +384,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); @@ -1421,11 +1421,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); @@ -3085,9 +3088,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 */