Skip to content

Commit 047157b

Browse files
authored
Use PyDict_GetItemRef() in _elementtree.c (#158809)
Replace PyDict_GetItemWithError() with PyDict_GetItemRef(). expat_default_handler() now holds a strong reference to value.
1 parent eab2d16 commit 047157b

1 file changed

Lines changed: 55 additions & 47 deletions

File tree

‎Modules/_elementtree.c‎

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,11 +1497,13 @@ _elementtree_Element_get_impl(ElementObject *self, PyObject *key,
14971497
{
14981498
if (self->extra && self->extra->attrib) {
14991499
PyObject *attrib = Py_NewRef(self->extra->attrib);
1500-
PyObject *value = Py_XNewRef(PyDict_GetItemWithError(attrib, key));
1501-
Py_DECREF(attrib);
1502-
if (value != NULL || PyErr_Occurred()) {
1500+
PyObject *value;
1501+
if (PyDict_GetItemRef(attrib, key, &value) != 0) {
1502+
Py_DECREF(attrib);
1503+
// The key exists or an error occurred
15031504
return value;
15041505
}
1506+
Py_DECREF(attrib);
15051507
}
15061508

15071509
return Py_NewRef(default_value);
@@ -3177,56 +3179,58 @@ makeuniversal(XMLParserObject* self, const char* string)
31773179
if (!key)
31783180
return NULL;
31793181

3180-
value = Py_XNewRef(PyDict_GetItemWithError(self->names, key));
3181-
3182-
if (value == NULL && !PyErr_Occurred()) {
3183-
/* new name. convert to universal name, and decode as
3184-
necessary */
3185-
3186-
PyObject* tag;
3187-
Py_ssize_t i;
3182+
if (PyDict_GetItemRef(self->names, key, &value) != 0) {
3183+
// The key exists or an error occurred
3184+
Py_DECREF(key);
3185+
return value;
3186+
}
31883187

3189-
/* look for namespace separator */
3190-
for (i = 0; i < size; i++)
3191-
if (string[i] == '}')
3192-
break;
3193-
if (i != size) {
3194-
/* convert to universal name */
3195-
PyBytesWriter *writer = PyBytesWriter_Create(1 + size);
3196-
if (writer == NULL) {
3197-
Py_DECREF(key);
3198-
return NULL;
3199-
}
3200-
char *p = PyBytesWriter_GetData(writer);
3201-
p[0] = '{';
3202-
memcpy(p+1, string, size);
3203-
size++;
3188+
/* new name. convert to universal name, and decode as
3189+
necessary */
32043190

3205-
tag = PyBytesWriter_Finish(writer);
3206-
if (tag == NULL) {
3207-
Py_DECREF(key);
3208-
return NULL;
3209-
}
3210-
} else {
3211-
/* plain name; use key as tag */
3212-
tag = Py_NewRef(key);
3213-
}
3191+
PyObject* tag;
3192+
Py_ssize_t i;
32143193

3215-
/* decode universal name */
3216-
const char *p = PyBytes_AS_STRING(tag);
3217-
value = PyUnicode_DecodeUTF8(p, size, "strict");
3218-
Py_DECREF(tag);
3219-
if (!value) {
3194+
/* look for namespace separator */
3195+
for (i = 0; i < size; i++)
3196+
if (string[i] == '}')
3197+
break;
3198+
if (i != size) {
3199+
/* convert to universal name */
3200+
PyBytesWriter *writer = PyBytesWriter_Create(1 + size);
3201+
if (writer == NULL) {
32203202
Py_DECREF(key);
32213203
return NULL;
32223204
}
3205+
char *p = PyBytesWriter_GetData(writer);
3206+
p[0] = '{';
3207+
memcpy(p+1, string, size);
3208+
size++;
32233209

3224-
/* add to names dictionary */
3225-
if (PyDict_SetItem(self->names, key, value) < 0) {
3210+
tag = PyBytesWriter_Finish(writer);
3211+
if (tag == NULL) {
32263212
Py_DECREF(key);
3227-
Py_DECREF(value);
32283213
return NULL;
32293214
}
3215+
} else {
3216+
/* plain name; use key as tag */
3217+
tag = Py_NewRef(key);
3218+
}
3219+
3220+
/* decode universal name */
3221+
const char *p = PyBytes_AS_STRING(tag);
3222+
value = PyUnicode_DecodeUTF8(p, size, "strict");
3223+
Py_DECREF(tag);
3224+
if (!value) {
3225+
Py_DECREF(key);
3226+
return NULL;
3227+
}
3228+
3229+
/* add to names dictionary */
3230+
if (PyDict_SetItem(self->names, key, value) < 0) {
3231+
Py_DECREF(key);
3232+
Py_DECREF(value);
3233+
return NULL;
32303234
}
32313235

32323236
Py_DECREF(key);
@@ -3304,7 +3308,11 @@ expat_default_handler(void *op, const XML_Char *data_in, int data_len)
33043308
if (!key)
33053309
return;
33063310

3307-
value = PyDict_GetItemWithError(self->entity, key);
3311+
if (PyDict_GetItemRef(self->entity, key, &value) < 0) {
3312+
Py_DECREF(key);
3313+
return;
3314+
}
3315+
Py_DECREF(key);
33083316

33093317
elementtreestate *st = self->state;
33103318
if (value) {
@@ -3316,8 +3324,10 @@ expat_default_handler(void *op, const XML_Char *data_in, int data_len)
33163324
res = PyObject_CallOneArg(self->handle_data, value);
33173325
else
33183326
res = NULL;
3327+
Py_DECREF(value);
33193328
Py_XDECREF(res);
3320-
} else if (!PyErr_Occurred()) {
3329+
}
3330+
else {
33213331
/* Report the first error, not the last */
33223332
char message[128] = "undefined entity ";
33233333
strncat(message, data_in, data_len < 100?data_len:100);
@@ -3329,8 +3339,6 @@ expat_default_handler(void *op, const XML_Char *data_in, int data_len)
33293339
message
33303340
);
33313341
}
3332-
3333-
Py_DECREF(key);
33343342
}
33353343

33363344
/* Append (action, node) to the list of events of the pull parser. */

0 commit comments

Comments
 (0)