Using xmlfile nsmap with both None and other keys raises TypeError
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Fix Released
|
Medium
|
scoder |
Bug Description
Using etree.xmlfile elements with an nsmap which has both a key None and other keys, thus specifying both the default namespace and other namespace prefixes, results in a TypeError exception, as demonstrated with the attached script.
Summary of the attached script:
buf = io.BytesIO()
with etree.xmlfile(buf) as xf:
with xf.element(
pass
Running the attached script produces the following output:
Python : sys.version_
lxml.etree : (3, 4, 2, 0)
libxml used : (2, 9, 1)
libxml compiled : (2, 9, 1)
libxslt used : (1, 1, 28)
libxslt compiled : (1, 1, 28)
Traceback (most recent call last):
File "test.py", line 16, in <module>
"foo": "ns:foo"}):
File "serializer.pxi", line 967, in lxml.etree.
File "serializer.pxi", line 811, in lxml.etree.
File "serializer.pxi", line 831, in lxml.etree.
TypeError: unorderable types: NoneType() < bytes()
I came up with a very simple (and probably slow) patch. It could be improved, but I don’t really know how to implement this with a good performance, as my experience with how well cython translates certain python constructs is about zero. So I did not even try.
The idea is that the ordering of the namespaces is now based on the prefix name only, and due to the structure of the new_namespaces array, the prefixless namespace is sorted as if its prefix was "xmlns".