Message169373
The problem is an inconsistency between the ElementTree.write() method on Python 2 and 3 when xml_declaration is True. For Python 2.7 the encoding argument MUST NOT be a unicode string. For Python 3.2 the encoding argument MUST be a unicode string.
On Python 2.7.3 (ElementTree 1.3.0) you can only use byte strings as the encoding argument when including the xml declaration. If you use a unicode object you get TypeError thrown:
>>> from xml.etree import ElementTree as ET
>>> from io import BytesIO
>>>
>>> tree = ET.ElementTree(ET.Element(u'example'))
>>> tree.write(BytesIO(), xml_declaration=True, encoding='utf-8')
>>> tree.write(BytesIO(), xml_declaration=True, encoding=u'utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xml/etree/ElementTree.py", line 813, in write
write("<?xml version='1.0' encoding='%s'?>\n" % encoding)
TypeError: 'unicode' does not have the buffer interface
So the encoding argument must be a byte string.
However on Python 3.2.3 (ElementTree 1.3.0) the same argument must be a unicode string. If you pass a byte string in it raises TypeError.
This only happens when you pass in an encoding and xml_declaration=True. This is a (small) problem when writing Py 2/3 compatible code since the version of ElementTree is supposed to be the same. |
|
Date |
User |
Action |
Args |
2012-08-29 13:18:40 | David.Buxton | set | recipients:
+ David.Buxton |
2012-08-29 13:18:40 | David.Buxton | set | messageid: <1346246320.04.0.762260786774.issue15811@psf.upfronthosting.co.za> |
2012-08-29 13:18:39 | David.Buxton | link | issue15811 messages |
2012-08-29 13:18:38 | David.Buxton | create | |
|