[XML-SIG] Bug in minidom/DocumentFragment

Geert Jansen geert@boskant.nl
Mon, 3 Sep 2001 15:08:35 +0200


Hi!

[I already reported this bug in July. I'm reposting it here because it
hasn't been fixed yet neither in Python-2.2a2 nor in the PyXML CVS.]

When you're adding a DocumentFragment to a node with Node.appendNode(), this
is supposed to add all children of the DocumentFragment to the Node. I
noticed however that when the DocumentFragment has more than one node, its
_last_ node is skipped.

Looking through the sources, the problem seems to be caused in minidom.py,
lines 140-141:

   def appendChild(self, node):
        if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
            for c in node.childNodes:
                self.appendChild(c)
            ### The DOM does not clearly specify what to return in this case
            return node

The call "self.appendChild(c)" changes the list node.childNodes under our
feet, because it tries to remove the child from its parent. This apparently
works out in such a way that the iteration of node.childNodes skips the last
element.

A new patch is appended here, which also fixes Node.insertBefore.

--- minidom.py  Mon Sep  3 14:55:25 2001
+++ minidom-new.py      Mon Sep  3 14:57:21 2001
@@ -109,7 +109,8 @@

     def insertBefore(self, newChild, refChild):
         if newChild.nodeType == self.DOCUMENT_FRAGMENT_NODE:
-            for c in newChild.childNodes:
+            # Make a copy of childNodes as it is changed under our feet.
+            for c in newChild.childNodes[:]:
                 self.insertBefore(c, refChild)
             ### The DOM does not clearly specify what to return in this
case
             return newChild
@@ -137,7 +138,7 @@

     def appendChild(self, node):
         if node.nodeType == self.DOCUMENT_FRAGMENT_NODE:
-            for c in node.childNodes:
+            for c in node.childNodes[:]:
                 self.appendChild(c)
             ### The DOM does not clearly specify what to return in this
case
             return node

Can this please be fixed?

Greetings,
Geert Jansen
--
long long email=0X78DC806B91FCA334,i=0,j; main(){j=(email>>4*i)&15;
putchar(j+j/2+j/5-j/12+(j<13?97:-65+7*j));i/15?exit(0):++i,main();}