summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2020-03-09 12:37:08 (GMT)
committerGitHub <noreply@github.com>2020-03-09 12:37:08 (GMT)
commit88944a44aa84b0f3674939019b1befbc7a9dc874 (patch)
treef459c6dd1458692a590def50dfa5babe97428df7 /Modules
parentfc72ab6913f2b5337ae7fda711f2de846d38f479 (diff)
downloadcpython-88944a44aa84b0f3674939019b1befbc7a9dc874.zip
cpython-88944a44aa84b0f3674939019b1befbc7a9dc874.tar.gz
cpython-88944a44aa84b0f3674939019b1befbc7a9dc874.tar.bz2
bpo-39903: Fix double decref in _elementtree.Element.__getstate__ (GH-18850)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_elementtree.c43
1 files changed, 17 insertions, 26 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index cc06824..0d3112a 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -950,8 +950,8 @@ static PyObject *
_elementtree_Element___getstate___impl(ElementObject *self)
/*[clinic end generated code: output=37279aeeb6bb5b04 input=f0d16d7ec2f7adc1]*/
{
- Py_ssize_t i, noattrib;
- PyObject *instancedict = NULL, *children;
+ Py_ssize_t i;
+ PyObject *children, *attrib;
/* Build a list of children. */
children = PyList_New(self->extra ? self->extra->length : 0);
@@ -963,33 +963,24 @@ _elementtree_Element___getstate___impl(ElementObject *self)
PyList_SET_ITEM(children, i, child);
}
- /* Construct the state object. */
- noattrib = (self->extra == NULL || self->extra->attrib == Py_None);
- if (noattrib)
- instancedict = Py_BuildValue("{sOsOs{}sOsO}",
- PICKLED_TAG, self->tag,
- PICKLED_CHILDREN, children,
- PICKLED_ATTRIB,
- PICKLED_TEXT, JOIN_OBJ(self->text),
- PICKLED_TAIL, JOIN_OBJ(self->tail));
- else
- instancedict = Py_BuildValue("{sOsOsOsOsO}",
- PICKLED_TAG, self->tag,
- PICKLED_CHILDREN, children,
- PICKLED_ATTRIB, self->extra->attrib,
- PICKLED_TEXT, JOIN_OBJ(self->text),
- PICKLED_TAIL, JOIN_OBJ(self->tail));
- if (instancedict) {
- Py_DECREF(children);
- return instancedict;
+ if (self->extra && self->extra->attrib != Py_None) {
+ attrib = self->extra->attrib;
+ Py_INCREF(attrib);
}
else {
- for (i = 0; i < PyList_GET_SIZE(children); i++)
- Py_DECREF(PyList_GET_ITEM(children, i));
- Py_DECREF(children);
-
- return NULL;
+ attrib = PyDict_New();
+ if (!attrib) {
+ Py_DECREF(children);
+ return NULL;
+ }
}
+
+ return Py_BuildValue("{sOsNsNsOsO}",
+ PICKLED_TAG, self->tag,
+ PICKLED_CHILDREN, children,
+ PICKLED_ATTRIB, attrib,
+ PICKLED_TEXT, JOIN_OBJ(self->text),
+ PICKLED_TAIL, JOIN_OBJ(self->tail));
}
static PyObject *