[Lxml-checkins] r47317 - lxml/trunk/src/lxml

scoder at codespeak.net scoder at codespeak.net
Mon Oct 8 21:57:08 CEST 2007


Author: scoder
Date: Mon Oct  8 21:57:08 2007
New Revision: 47317

Modified:
   lxml/trunk/src/lxml/apihelpers.pxi
Log:
handle exceptions in subelement creation

Modified: lxml/trunk/src/lxml/apihelpers.pxi
==============================================================================
--- lxml/trunk/src/lxml/apihelpers.pxi	(original)
+++ lxml/trunk/src/lxml/apihelpers.pxi	Mon Oct  8 21:57:08 2007
@@ -171,15 +171,24 @@
         python.PyErr_NoMemory()
     tree.xmlAddChild(parent._c_node, c_node)
 
-    if text is not None:
-        _setNodeText(c_node, text)
-    if tail is not None:
-        _setTailText(c_node, tail)
-
-    # add namespaces to node if necessary
-    doc._setNodeNamespaces(c_node, ns_utf, nsmap)
-    _initNodeAttributes(c_node, doc, attrib, extra_attrs)
-    return _elementFactory(doc, c_node)
+    try:
+        if text is not None:
+            _setNodeText(c_node, text)
+        if tail is not None:
+            _setTailText(c_node, tail)
+
+        # add namespaces to node if necessary
+        doc._setNodeNamespaces(c_node, ns_utf, nsmap)
+        _initNodeAttributes(c_node, doc, attrib, extra_attrs)
+        return _elementFactory(doc, c_node)
+    except:
+        # free allocated c_node/c_doc unless Python does it for us
+        if c_node.doc is not c_doc:
+            # node not yet in document => will not be freed by document
+            if tail is not None:
+                _removeText(c_node.next) # tail
+            tree.xmlFreeNode(c_node)
+        raise
 
 cdef _initNodeAttributes(xmlNode* c_node, _Document doc, attrib, extra):
     """Initialise the attributes of an element node.


More information about the lxml-checkins mailing list