diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index b574c96d3f96255..1a088f11ff0a892 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -375,9 +375,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
*/
@@ -385,16 +387,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);
@@ -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);
@@ -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 */
diff --git a/Python/compile.c b/Python/compile.c
index 2a6291ccb51b0c9..beafcb06e654804 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -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) {
@@ -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;
}