Permalink
Browse files

fix TypeError when passing nsmap with both default and prefixed names…

…pace into xmlfile.element()
  • Loading branch information...
scoder committed Mar 1, 2015
1 parent 122b037 commit 6abb42d39bc243b4656bf68966df8a4ad7fb7e81
Showing with 16 additions and 2 deletions.
  1. +3 −0 CHANGES.txt
  2. +3 −2 src/lxml/serializer.pxi
  3. +10 −0 src/lxml/tests/test_incremental_xmlfile.py
View
@@ -8,6 +8,9 @@ lxml changelog
Bugs fixed
----------
* LP#1426868: Passing a default namespace and a prefixed namespace mapping
as nsmap into ``xmlfile.element()`` raised a ``TypeError``.
* LP#1421927: DOCTYPE system URLs were incorrectly quoted when containing
double quotes. Patch by Olli Pottonen.
View
@@ -801,7 +801,7 @@ cdef class _IncrementalFileWriter:
return _FileWriterElement(self, (ns, name, attributes, reversed_nsmap))
cdef _write_qname(self, bytes name, bytes prefix):
if prefix is not None:
if prefix: # empty bytes for no prefix (not None to allow sorting)
tree.xmlOutputBufferWrite(self._c_out, len(prefix), _cstr(prefix))
tree.xmlOutputBufferWrite(self._c_out, 1, ':')
tree.xmlOutputBufferWrite(self._c_out, len(name), _cstr(name))
@@ -887,7 +887,8 @@ cdef class _IncrementalFileWriter:
for ns, prefix in nsmap.iteritems():
flat_namespaces_map[ns] = prefix
if prefix is None:
new_namespaces.append((None, b'xmlns', ns))
# use empty bytes rather than None to allow sorting
new_namespaces.append((b'', b'xmlns', ns))
else:
new_namespaces.append((b'xmlns', prefix, ns))
# merge in flat namespace map of parent
@@ -109,6 +109,16 @@ def test_nested_default_namespace(self):
pass
self.assertXml('<test xmlns="nsURI"><toast></toast></test>')
def test_nested_default_namespace_and_other(self):
with etree.xmlfile(self._file) as xf:
with xf.element('{nsURI}test', nsmap={None: 'nsURI', 'p': 'ns2'}):
with xf.element('{nsURI}toast'):
pass
with xf.element('{ns2}toast'):
pass
self.assertXml(
'<test xmlns="nsURI" xmlns:p="ns2"><toast></toast><p:toast></p:toast></test>')
def test_pi(self):
with etree.xmlfile(self._file) as xf:
xf.write(etree.ProcessingInstruction('pypi'))

0 comments on commit 6abb42d

Please sign in to comment.