[lxml-dev] Bug with attribute mangling when adding child elements

Hello, I noticed today that certain child attribute values are mangled when you try to insert/append child elements onto parent elements. I have written a test script for this as usual. I'm probably missing something... again. :) Thanks, Noah -- "Creativity can be a social contribution, but only in so far as society is free to use the results." - R. Stallman

Hello, I have just noticed that if you set the attributes after you have added to the tree the weird bug does not happen. Attached is a modified version of the file. Hope this helps someone.... Noah On 29/05/06, Noah Slater <nslater@gmail.com> wrote:
Hello,
I noticed today that certain child attribute values are mangled when you try to insert/append child elements onto parent elements.
I have written a test script for this as usual.
I'm probably missing something... again. :)
Thanks, Noah
-- "Creativity can be a social contribution, but only in so far as society is free to use the results." - R. Stallman
-- "Creativity can be a social contribution, but only in so far as society is free to use the results." - R. Stallman

Hi Noah, Noah Slater wrote:
On 29/05/06, Noah Slater <nslater@gmail.com> wrote:
I noticed today that certain child attribute values are mangled when you try to insert/append child elements onto parent elements.
I have written a test script for this as usual.
I have just noticed that if you set the attributes after you have added to the tree the weird bug does not happen.
Thanks for reporting this. It looks like a bug in libxml2, as it only happens for "xml:id", not for other attributes. I guess the bug is in xmlReconciliateNs, but that's really just guessing. A simpler way to reproduce this is: ------------------------------- root = etree.Element('element') subelement = etree.Element('subelement') subelement.set("{http://www.w3.org/XML/1998/namespace}id", "foo") root.append(subelement) print etree.tostring(root) ------------------------------- What surprises me most is that ElementTree (1.2.6) has a similar bug. It returns this for subelement.attrib after appending: {'{}id': 'foo'} I'll file a bug report on libxml2 anyway. Stefan

Stefan Behnel wrote:
What surprises me most is that ElementTree (1.2.6) has a similar bug. It returns this for subelement.attrib after appending:
{'{}id': 'foo'}
that's somewhat surprising, given that ET doesn't do any mangling by itself; that's entirely up to the serialization code. here's what I get on my machine:
e = ET.Element("subelement") e.set("{http://www.w3.org/XML/1998/namespace}id", "foo") e <Element subelement at b42d28> ET.tostring(e) '<subelement xml:id="foo" />' e.attrib {'{http://www.w3.org/XML/1998/namespace}id': 'foo'}
</F>

Varified using python-elementtree v.1.2.6-8 on Debian PPC (Sid):
import elementtree.ElementTree as ET
element = ET.Element('element') subelement = ET.Element('subelement') subelement.set('{http://www.w3.org/XML/1998/namespace}id', 'foo') element.append(subelement) ET.dump(element) <element><subelement xml:id="foo" /></element> subelement.attrib {'{http://www.w3.org/XML/1998/namespace}id': 'foo'}
On 29/05/06, Fredrik Lundh <fredrik@pythonware.com> wrote:
Stefan Behnel wrote:
What surprises me most is that ElementTree (1.2.6) has a similar bug. It returns this for subelement.attrib after appending:
{'{}id': 'foo'}
that's somewhat surprising, given that ET doesn't do any mangling by itself; that's entirely up to the serialization code.
here's what I get on my machine:
e = ET.Element("subelement") e.set("{http://www.w3.org/XML/1998/namespace}id", "foo") e <Element subelement at b42d28> ET.tostring(e) '<subelement xml:id="foo" />' e.attrib {'{http://www.w3.org/XML/1998/namespace}id': 'foo'}
</F>
_______________________________________________ lxml-dev mailing list lxml-dev@codespeak.net http://codespeak.net/mailman/listinfo/lxml-dev
-- "Creativity can be a social contribution, but only in so far as society is free to use the results." - R. Stallman

Hi Fredrik, sorry for the confusion. Fredrik Lundh wrote:
Stefan Behnel wrote:
What surprises me most is that ElementTree (1.2.6) has a similar bug. It returns this for subelement.attrib after appending:
{'{}id': 'foo'}
that's somewhat surprising, given that ET doesn't do any mangling by itself; that's entirely up to the serialization code.
here's what I get on my machine:
e = ET.Element("subelement") e.set("{http://www.w3.org/XML/1998/namespace}id", "foo") e <Element subelement at b42d28> ET.tostring(e) '<subelement xml:id="foo" />' e.attrib {'{http://www.w3.org/XML/1998/namespace}id': 'foo'}
I get that, too. I had forgotten a tiny 'self.' in the test case, which let it use etree instead of ElementTree. Oh well, imports ... Sorry, Stefan

Hi, Stefan Behnel wrote:
Noah Slater wrote:
On 29/05/06, Noah Slater <nslater@gmail.com> wrote:
I noticed today that certain child attribute values are mangled when you try to insert/append child elements onto parent elements.
I have written a test script for this as usual. I have just noticed that if you set the attributes after you have added to the tree the weird bug does not happen.
Thanks for reporting this. It looks like a bug in libxml2, as it only happens for "xml:id", not for other attributes. I guess the bug is in xmlReconciliateNs, but that's really just guessing.
Well, it was /related/ to xmlReconciliateNs, but not a bug in libxml2 and only by chance it showed fro xml:id. lxml was running xmlReconciliateNs /after/ cleaning up the document references in the Python elements. Which means it freed the document if all references were gone, but before trying to access its namespaces. Great. That bug was in there for ages, since SVN revision 12568! Fixed in the trunk. Stefan
participants (3)
-
Fredrik Lundh
-
Noah Slater
-
Stefan Behnel