Using xmlfile nsmap with both None and other keys raises TypeError

Bug #1426868 reported by jwi
6
This bug affects 1 person
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("{ns:foo}el", nsmap={None: "ns:bar",
                                         "foo": "ns:foo"}):
        pass

Running the attached script produces the following output:

Python : sys.version_info(major=3, minor=4, micro=1, releaselevel='final', serial=0)
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._FileWriterElement.__enter__ (src/lxml/lxml.etree.c:129055)
  File "serializer.pxi", line 811, in lxml.etree._IncrementalFileWriter._write_start_element (src/lxml/lxml.etree.c:126688)
  File "serializer.pxi", line 831, in lxml.etree._IncrementalFileWriter._write_attributes_and_namespaces (src/lxml/lxml.etree.c:126997)
TypeError: unorderable types: NoneType() < bytes()

Revision history for this message
jwi (p-uone) wrote :
Revision history for this message
jwi (p-uone) wrote :

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".

Revision history for this message
scoder (scoder) wrote :
Changed in lxml:
assignee: nobody → scoder (scoder)
importance: Undecided → Medium
milestone: none → 3.4
status: New → Fix Committed
Revision history for this message
jwi (p-uone) wrote :

Right, that looks pretty smart :). Thanks for fixing!

Revision history for this message
scoder (scoder) wrote :

Fixed in lxml 3.4.3.

Changed in lxml:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Bug attachments

Remote bug watches

Bug watches keep track of this bug in other bug trackers.