This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: minidom error
Type: behavior Stage: resolved
Components: XML Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, txomon
Priority: normal Keywords:

Created on 2013-01-08 02:58 by txomon, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg179309 - (view) Author: Javier Domingo (txomon) Date: 2013-01-08 02:58
Hi I found something like a bug, I can't get this working:

import xml.dom.minidom as minidom
document="""<a>
    <b>
        <c />
        <c />
    </b>
</a>""" 
dom = minidom.parseString(document)

dom.childNodes
dom.childNodes[0].childNodes
dom.childNodes[0].childNodes[1].childNodes

def delete_recursive(parent):
    for child in parent.childNodes:
        if child.hasChildNodes():
            delete_recursive(child)
        else:
            parent.removeChild(child)

delete_recursive(dom)

dom.childNodes
dom.childNodes[0].childNodes
dom.childNodes[0].childNodes[0].childNodes

Executes as:
>>> import xml.dom.minidom as minidom
>>> document="""<a>
...     <b>
...         <c />
...         <c />
...     </b>
... </a>""" 
>>> dom = minidom.parseString(document)
>>> 
>>> dom.childNodes
[<DOM Element: a at 0x7f5408e717d0>]
>>> dom.childNodes[0].childNodes
[<DOM Text node "'\n    '">, <DOM Element: b at 0x7f5408e71f50>, <DOM Text node "'\n'">]
>>> dom.childNodes[0].childNodes[1].childNodes
[<DOM Text node "'\n        '">, <DOM Element: c at 0x7f5408e93290>, <DOM Text node "'\n        '">, <DOM Element: c at 0x7f5408e93310>, <DOM Text node "'\n    '">]
>>> 
>>> def delete_recursive(parent):
...     for child in parent.childNodes:
...         if child.hasChildNodes():
...             delete_recursive(child)
...         else:
...             parent.removeChild(child)
... 
>>> delete_recursive(dom)
>>> 
>>> dom.childNodes
[<DOM Element: a at 0x7f5408e717d0>]
>>> dom.childNodes[0].childNodes
[<DOM Element: b at 0x7f5408e71f50>]
>>> dom.childNodes[0].childNodes[0].childNodes
[<DOM Text node "'\n        '">, <DOM Element: c at 0x7f5408e93290>, <DOM Text node "'\n        '">, <DOM Element: c at 0x7f5408e93310>, <DOM Text node "'\n    '">]

The problem here is this last line, those text nodes shouldn't be here! I have traced the problem, and seem that the for loop is not correctly executed
msg179310 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-08 03:24
The problem is that you are mutating parent.childNodes while you are iterating on it.  Iterating over a copy seems to solve the problem.
msg179327 - (view) Author: Javier Domingo (txomon) Date: 2013-01-08 10:32
I know that is the problem, but that shouldn't happen! if you remove a item
from a list, that doesn't happen. That is why I tagged the error as
minidom's
El 08/01/2013 04:24, "Ezio Melotti" <report@bugs.python.org> escribió:

>
> Ezio Melotti added the comment:
>
> The problem is that you are mutating parent.childNodes while you are
> iterating on it.  Iterating over a copy seems to solve the problem.
>
> ----------
> nosy: +ezio.melotti
> resolution:  -> invalid
> stage:  -> committed/rejected
> status: open -> closed
> type:  -> behavior
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16890>
> _______________________________________
>
msg179329 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-01-08 10:59
It happens with lists too:
>>> l = list(range(10))
>>> for x in l:
...     l.remove(x)
... 
>>> l
[1, 3, 5, 7, 9]
msg179331 - (view) Author: Javier Domingo (txomon) Date: 2013-01-08 11:07
Ok, sorry then.

Javier Domingo

2013/1/8 Ezio Melotti <report@bugs.python.org>

>
> Ezio Melotti added the comment:
>
> It happens with lists too:
> >>> l = list(range(10))
> >>> for x in l:
> ...     l.remove(x)
> ...
> >>> l
> [1, 3, 5, 7, 9]
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue16890>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:57:40adminsetgithub: 61094
2013-01-08 11:07:20txomonsetmessages: + msg179331
2013-01-08 10:59:21ezio.melottisetmessages: + msg179329
2013-01-08 10:32:17txomonsetmessages: + msg179327
2013-01-08 03:24:29ezio.melottisetstatus: open -> closed

type: behavior

nosy: + ezio.melotti
messages: + msg179310
resolution: not a bug
stage: resolved
2013-01-08 02:58:47txomoncreate