Skip to content

Commit 1b0d656

Browse files
pythongh-111789: Use PyDict_GetItemRef() in Modules/_elementtree.c
1 parent 0ff6368 commit 1b0d656

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

Modules/_elementtree.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -372,26 +372,26 @@ get_attrib_from_keywords(PyObject *kwds)
372372
if (attrib_str == NULL) {
373373
return NULL;
374374
}
375-
PyObject *attrib = PyDict_GetItemWithError(kwds, attrib_str);
376-
377-
if (attrib) {
375+
PyObject *attrib;
376+
if (PyDict_GetItemRef(kwds, attrib_str, &attrib) == 0) {
377+
attrib = PyDict_New();
378+
}
379+
else if (attrib) {
378380
/* If attrib was found in kwds, copy its value and remove it from
379381
* kwds
380382
*/
381383
if (!PyDict_Check(attrib)) {
382384
Py_DECREF(attrib_str);
383385
PyErr_Format(PyExc_TypeError, "attrib must be dict, not %.100s",
384386
Py_TYPE(attrib)->tp_name);
387+
Py_DECREF(attrib);
385388
return NULL;
386389
}
387-
attrib = PyDict_Copy(attrib);
390+
Py_SETREF(attrib, PyDict_Copy(attrib));
388391
if (attrib && PyDict_DelItem(kwds, attrib_str) < 0) {
389392
Py_SETREF(attrib, NULL);
390393
}
391394
}
392-
else if (!PyErr_Occurred()) {
393-
attrib = PyDict_New();
394-
}
395395

396396
Py_DECREF(attrib_str);
397397

@@ -1421,11 +1421,14 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
14211421
{
14221422
if (self->extra && self->extra->attrib) {
14231423
PyObject *attrib = Py_NewRef(self->extra->attrib);
1424-
PyObject *value = Py_XNewRef(PyDict_GetItemWithError(attrib, key));
1425-
Py_DECREF(attrib);
1426-
if (value != NULL || PyErr_Occurred()) {
1424+
PyObject *value;
1425+
if (PyDict_GetItemRef(attrib, key, &value) != 0) {
1426+
// found or error
1427+
Py_DECREF(attrib);
14271428
return value;
14281429
}
1430+
// not found
1431+
Py_DECREF(attrib);
14291432
}
14301433

14311434
return Py_NewRef(default_value);
@@ -3085,9 +3088,7 @@ makeuniversal(XMLParserObject* self, const char* string)
30853088
if (!key)
30863089
return NULL;
30873090

3088-
value = Py_XNewRef(PyDict_GetItemWithError(self->names, key));
3089-
3090-
if (value == NULL && !PyErr_Occurred()) {
3091+
if (PyDict_GetItemRef(self->names, key, &value) == 0) {
30913092
/* new name. convert to universal name, and decode as
30923093
necessary */
30933094

0 commit comments

Comments
 (0)