Skip to content

Commit 80be9c3

Browse files
bpo-39903: Fix double decref in _elementtree.Element.__getstate__ (GH-18850)
(cherry picked from commit 88944a4) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 9d5ed83 commit 80be9c3

File tree

1 file changed

+17
-26
lines changed

1 file changed

+17
-26
lines changed

Modules/_elementtree.c

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,8 @@ static PyObject *
926926
_elementtree_Element___getstate___impl(ElementObject *self)
927927
/*[clinic end generated code: output=37279aeeb6bb5b04 input=f0d16d7ec2f7adc1]*/
928928
{
929-
Py_ssize_t i, noattrib;
930-
PyObject *instancedict = NULL, *children;
929+
Py_ssize_t i;
930+
PyObject *children, *attrib;
931931

932932
/* Build a list of children. */
933933
children = PyList_New(self->extra ? self->extra->length : 0);
@@ -939,33 +939,24 @@ _elementtree_Element___getstate___impl(ElementObject *self)
939939
PyList_SET_ITEM(children, i, child);
940940
}
941941

942-
/* Construct the state object. */
943-
noattrib = (self->extra == NULL || self->extra->attrib == Py_None);
944-
if (noattrib)
945-
instancedict = Py_BuildValue("{sOsOs{}sOsO}",
946-
PICKLED_TAG, self->tag,
947-
PICKLED_CHILDREN, children,
948-
PICKLED_ATTRIB,
949-
PICKLED_TEXT, JOIN_OBJ(self->text),
950-
PICKLED_TAIL, JOIN_OBJ(self->tail));
951-
else
952-
instancedict = Py_BuildValue("{sOsOsOsOsO}",
953-
PICKLED_TAG, self->tag,
954-
PICKLED_CHILDREN, children,
955-
PICKLED_ATTRIB, self->extra->attrib,
956-
PICKLED_TEXT, JOIN_OBJ(self->text),
957-
PICKLED_TAIL, JOIN_OBJ(self->tail));
958-
if (instancedict) {
959-
Py_DECREF(children);
960-
return instancedict;
942+
if (self->extra && self->extra->attrib != Py_None) {
943+
attrib = self->extra->attrib;
944+
Py_INCREF(attrib);
961945
}
962946
else {
963-
for (i = 0; i < PyList_GET_SIZE(children); i++)
964-
Py_DECREF(PyList_GET_ITEM(children, i));
965-
Py_DECREF(children);
966-
967-
return NULL;
947+
attrib = PyDict_New();
948+
if (!attrib) {
949+
Py_DECREF(children);
950+
return NULL;
951+
}
968952
}
953+
954+
return Py_BuildValue("{sOsNsNsOsO}",
955+
PICKLED_TAG, self->tag,
956+
PICKLED_CHILDREN, children,
957+
PICKLED_ATTRIB, attrib,
958+
PICKLED_TEXT, JOIN_OBJ(self->text),
959+
PICKLED_TAIL, JOIN_OBJ(self->tail));
969960
}
970961

971962
static PyObject *

0 commit comments

Comments
 (0)