[lxml-dev] Behaviour of Element.iter() changes when removing a node depending on whether the node has children or not
Hi (and sorry for the stupidly long subject, but otherwise it doesn't actually cover the subject), I'm not sure whether this is a bug or not (if not, there probably ought to be some note in the docs), but, for example:
from lxml import etree foo = etree.fromstring("<root><a><b/></a><a/></root>") for element in foo.iter(etree.Element): ... print element.tag ... if element.tag == "a": ... element.getparent().remove(element) ... root a b foo = etree.fromstring("<root><a/><a/></root>") for element in foo.iter(etree.Element): ... print element.tag ... if element.tag == "a": ... element.getparent().remove(element) ... root a a
Only in the latter case does the final a element actually appear in the iteration, whereas in the former case it just vanishes. What changes the behaviour is the fact that in former case the first a element has a child: an empty b element. -- Geoffrey Sneddon <http://gsnedders.com/>
Hi, Geoffrey Sneddon wrote:
Behaviour of Element.iter() changes when removing a node depending on whether the node has children or not
Admittedly, the docs are not very explicit here, but it's common that container modification during iteration results in undefined behaviour. This also applies to lxml's tree iterator. Stefan
participants (2)
-
Geoffrey Sneddon
-
Stefan Behnel