This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author David.Buxton
Recipients David.Buxton
Date 2012-08-29.13:18:38
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1346246320.04.0.762260786774.issue15811@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2012-08-29 13:18:40David.Buxtonsetrecipients: + David.Buxton
2012-08-29 13:18:40David.Buxtonsetmessageid: <1346246320.04.0.762260786774.issue15811@psf.upfronthosting.co.za>
2012-08-29 13:18:39David.Buxtonlinkissue15811 messages
2012-08-29 13:18:38David.Buxtoncreate