Skip to content

gh-111789: Use PyDict_GetItemRef() #111790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
27 changes: 14 additions & 13 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,26 +375,26 @@ 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
*/
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);
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);

Expand Down Expand Up @@ -1424,11 +1424,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);
Expand Down Expand Up @@ -3089,9 +3092,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 */

Expand Down
9 changes: 4 additions & 5 deletions Python/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -921,11 +921,10 @@ dict_add_o(PyObject *dict, PyObject *o)
PyObject *v;
Py_ssize_t arg;

v = PyDict_GetItemWithError(dict, o);
if (PyDict_GetItemRef(dict, o, &v) < 0) {
return ERROR;
}
if (!v) {
if (PyErr_Occurred()) {
return ERROR;
}
arg = PyDict_GET_SIZE(dict);
v = PyLong_FromSsize_t(arg);
if (!v) {
Expand All @@ -935,10 +934,10 @@ dict_add_o(PyObject *dict, PyObject *o)
Py_DECREF(v);
return ERROR;
}
Py_DECREF(v);
}
else
arg = PyLong_AsLong(v);
Py_DECREF(v);
return arg;
}

Expand Down