-
-
Notifications
You must be signed in to change notification settings - Fork 29.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
minidom error #61094
Comments
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 def delete_recursive(parent):
for child in parent.childNodes:
if child.hasChildNodes():
delete_recursive(child)
else:
parent.removeChild(child)
delete_recursive(dom) dom.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 |
The problem is that you are mutating parent.childNodes while you are iterating on it. Iterating over a copy seems to solve the problem. |
I know that is the problem, but that shouldn't happen! if you remove a item
|
It happens with lists too:
>>> l = list(range(10))
>>> for x in l:
... l.remove(x)
...
>>> l
[1, 3, 5, 7, 9] |
Ok, sorry then. Javier Domingo 2013/1/8 Ezio Melotti <report@bugs.python.org>
|
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: