From 0e1887352018d3d77ec1f97500f248fc00d5e35d Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Jan 2010 20:06:42 +0100 Subject: [PATCH] issue 3299: replace PyObject_DEL() by Py_DECREF() in cElementTree module --- Modules/_elementtree.c | 92 ++++++++++++++++++++++++++++------------------- 1 files changed, 55 insertions(+), 37 deletions(-) diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c index 684081c..e0b8c5f 100644 --- a/Modules/_elementtree.c +++ b/Modules/_elementtree.c @@ -278,8 +278,10 @@ LOCAL(int) element_new_extra(ElementObject* self, PyObject* attrib) { self->extra = PyObject_Malloc(sizeof(ElementObjectExtra)); - if (!self->extra) + if (!self->extra) { + PyErr_NoMemory(); return -1; + } if (!attrib) attrib = Py_None; @@ -319,6 +321,15 @@ element_new(PyObject* tag, PyObject* attrib) if (self == NULL) return NULL; + Py_INCREF(tag); + self->tag = tag; + + Py_INCREF(Py_None); + self->text = Py_None; + + Py_INCREF(Py_None); + self->tail = Py_None; + /* use None for empty dictionaries */ if (PyDict_CheckExact(attrib) && !PyDict_Size(attrib)) attrib = Py_None; @@ -328,9 +339,9 @@ element_new(PyObject* tag, PyObject* attrib) if (attrib != Py_None) { if (element_new_extra(self, attrib) < 0) { - PyObject_Del(self); + Py_DECREF(self); return NULL; - } + } self->extra->length = 0; self->extra->allocated = STATIC_CHILDREN; @@ -338,15 +349,6 @@ element_new(PyObject* tag, PyObject* attrib) } - Py_INCREF(tag); - self->tag = tag; - - Py_INCREF(Py_None); - self->text = Py_None; - - Py_INCREF(Py_None); - self->tail = Py_None; - ALLOC(sizeof(ElementObject), "create element"); return (PyObject*) self; @@ -361,8 +363,10 @@ element_resize(ElementObject* self, int extra) /* make sure self->children can hold the given number of extra elements. set an exception and return -1 if allocation failed */ - if (!self->extra) - element_new_extra(self, NULL); + if (!self->extra) { + if (element_new_extra(self, NULL) < 0) + return -1; + } size = self->extra->length + extra; @@ -1003,8 +1007,10 @@ element_insert(ElementObject* self, PyObject* args) &Element_Type, &element)) return NULL; - if (!self->extra) - element_new_extra(self, NULL); + if (!self->extra) { + if (element_new_extra(self, NULL) < 0) + return NULL; + } if (index < 0) index = 0; @@ -1174,8 +1180,10 @@ element_set(ElementObject* self, PyObject* args) if (!PyArg_ParseTuple(args, "OO:set", &key, &value)) return NULL; - if (!self->extra) - element_new_extra(self, NULL); + if (!self->extra) { + if (element_new_extra(self, NULL) < 0) + return NULL; + } attrib = element_get_attrib(self); if (!attrib) @@ -1194,8 +1202,10 @@ element_setslice(PyObject* self_, Py_ssize_t start, Py_ssize_t end, PyObject* it Py_ssize_t i, new, old; PyObject* recycle = NULL; - if (!self->extra) - element_new_extra(self, NULL); + if (!self->extra) { + if (element_new_extra(self, NULL) < 0) + return -1; + } /* standard clamping */ if (start < 0) @@ -1347,8 +1357,10 @@ element_getattr(ElementObject* self, char* name) else if (strcmp(name, "tail") == 0) { res = element_get_tail(self); } else if (strcmp(name, "attrib") == 0) { - if (!self->extra) - element_new_extra(self, NULL); + if (!self->extra) { + if (element_new_extra(self, NULL) < 0) + return NULL; + } res = element_get_attrib(self); } else { PyErr_SetString(PyExc_AttributeError, name); @@ -1386,8 +1398,10 @@ element_setattr(ElementObject* self, const char* name, PyObject* value) self->tail = value; Py_INCREF(self->tail); } else if (strcmp(name, "attrib") == 0) { - if (!self->extra) - element_new_extra(self, NULL); + if (!self->extra) { + if (element_new_extra(self, NULL) < 0) + return -1; + } Py_DECREF(self->extra->attrib); self->extra->attrib = value; Py_INCREF(self->extra->attrib); @@ -2241,17 +2255,26 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw) self = PyObject_New(XMLParserObject, &XMLParser_Type); if (self == NULL) return NULL; + self->parser = NULL; + self->target = NULL; + self->entity = NULL; + self->names = NULL; + self->handle_pi = NULL; + self->handle_comment = NULL; + self->handle_end = NULL; + self->handle_data = NULL; + self->handle_start = NULL; + self->handle_xml = NULL; self->entity = PyDict_New(); if (!self->entity) { - PyObject_Del(self); + Py_DECREF(self); return NULL; } self->names = PyDict_New(); if (!self->names) { - PyObject_Del(self->entity); - PyObject_Del(self); + Py_DECREF(self); return NULL; } @@ -2261,9 +2284,7 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw) self->parser = EXPAT(ParserCreate_MM)(encoding, &memory_handler, "}"); if (!self->parser) { - PyObject_Del(self->names); - PyObject_Del(self->entity); - PyObject_Del(self); + Py_DECREF(self); PyErr_NoMemory(); return NULL; } @@ -2272,10 +2293,7 @@ xmlparser(PyObject* self_, PyObject* args, PyObject* kw) if (!target) { target = treebuilder_new(); if (!target) { - EXPAT(ParserFree)(self->parser); - PyObject_Del(self->names); - PyObject_Del(self->entity); - PyObject_Del(self); + Py_DECREF(self); return NULL; } } else @@ -2340,9 +2358,9 @@ xmlparser_dealloc(XMLParserObject* self) Py_XDECREF(self->handle_start); Py_XDECREF(self->handle_xml); - Py_DECREF(self->target); - Py_DECREF(self->entity); - Py_DECREF(self->names); + Py_XDECREF(self->target); + Py_XDECREF(self->entity); + Py_XDECREF(self->names); RELEASE(sizeof(XMLParserObject), "destroy expatparser"); -- 1.6.6