Skip to content
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

Closed
txomon mannequin opened this issue Jan 8, 2013 · 5 comments
Closed

minidom error #61094

txomon mannequin opened this issue Jan 8, 2013 · 5 comments
Labels
topic-XML type-bug An unexpected behavior, bug, or error

Comments

@txomon
Copy link
Mannequin

txomon mannequin commented Jan 8, 2013

BPO 16890
Nosy @ezio-melotti

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:

assignee = None
closed_at = <Date 2013-01-08.03:24:29.332>
created_at = <Date 2013-01-08.02:58:47.283>
labels = ['expert-XML', 'type-bug', 'invalid']
title = 'minidom error'
updated_at = <Date 2013-01-08.11:07:20.382>
user = 'https://bugs.python.org/txomon'

bugs.python.org fields:

activity = <Date 2013-01-08.11:07:20.382>
actor = 'txomon'
assignee = 'none'
closed = True
closed_date = <Date 2013-01-08.03:24:29.332>
closer = 'ezio.melotti'
components = ['XML']
creation = <Date 2013-01-08.02:58:47.283>
creator = 'txomon'
dependencies = []
files = []
hgrepos = []
issue_num = 16890
keywords = []
message_count = 5.0
messages = ['179309', '179310', '179327', '179329', '179331']
nosy_count = 2.0
nosy_names = ['ezio.melotti', 'txomon']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue16890'
versions = ['Python 3.2']

@txomon
Copy link
Mannequin Author

txomon mannequin commented Jan 8, 2013

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

@txomon txomon mannequin added the topic-XML label Jan 8, 2013
@ezio-melotti
Copy link
Member

The problem is that you are mutating parent.childNodes while you are iterating on it. Iterating over a copy seems to solve the problem.

@ezio-melotti ezio-melotti added invalid type-bug An unexpected behavior, bug, or error labels Jan 8, 2013
@txomon
Copy link
Mannequin Author

txomon mannequin commented Jan 8, 2013

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\>


@ezio-melotti
Copy link
Member

It happens with lists too:
>>> l = list(range(10))
>>> for x in l:
...     l.remove(x)
... 
>>> l
[1, 3, 5, 7, 9]

@txomon
Copy link
Mannequin Author

txomon mannequin commented Jan 8, 2013

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\>


@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-XML type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant