diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-07-12 00:05:17 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-07-12 00:05:17 (GMT) |
commit | d917dcbe5e855ae30eca455d38bec2bf6354dbdf (patch) | |
tree | 3ded2efe18626a03d000ccc1f731531f9d21691d /Modules/_elementtree.c | |
parent | 81aac734e1003d93e6a21d2c8a4943f6398aa419 (diff) | |
download | cpython-d917dcbe5e855ae30eca455d38bec2bf6354dbdf.zip cpython-d917dcbe5e855ae30eca455d38bec2bf6354dbdf.tar.gz cpython-d917dcbe5e855ae30eca455d38bec2bf6354dbdf.tar.bz2 |
Issue #18408: Fix constructors of _elementtree.c
* Use Py_DECREF() instead of PyObject_GC_Del() to release correctly all
resources
* Raise MemoryError on memory allocation failure
Diffstat (limited to 'Modules/_elementtree.c')
-rw-r--r-- | Modules/_elementtree.c | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 7c01a60..0f63077 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -237,15 +237,16 @@ create_new_element(PyObject* tag, PyObject* attrib) self->weakreflist = NULL; + ALLOC(sizeof(ElementObject), "create element"); + PyObject_GC_Track(self); + if (attrib != Py_None && !is_empty_dict(attrib)) { if (create_extra(self, attrib) < 0) { - PyObject_GC_Del(self); + Py_DECREF(self); return NULL; } } - ALLOC(sizeof(ElementObject), "create element"); - PyObject_GC_Track(self); return (PyObject*) self; } @@ -2122,14 +2123,6 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext) it = PyObject_GC_New(ElementIterObject, &ElementIter_Type); if (!it) return NULL; - if (!(it->parent_stack = PyObject_Malloc(sizeof(ParentLocator)))) { - PyObject_GC_Del(it); - return NULL; - } - - it->parent_stack->parent = NULL; - it->parent_stack->child_index = 0; - it->parent_stack->next = NULL; if (PyUnicode_Check(tag)) star = PyUnicode_FromString("*"); @@ -2147,8 +2140,18 @@ create_elementiter(ElementObject *self, PyObject *tag, int gettext) Py_INCREF(self); it->root_element = self; - PyObject_GC_Track(it); + + it->parent_stack = PyObject_Malloc(sizeof(ParentLocator)); + if (it->parent_stack == NULL) { + Py_DECREF(it); + PyErr_NoMemory(); + return NULL; + } + it->parent_stack->parent = NULL; + it->parent_stack->child_index = 0; + it->parent_stack->next = NULL; + return (PyObject *)it; } |