New submission from David Buxton:
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.
--
components: XML
messages: 169373
nosy: David.Buxton
priority: normal
severity: normal
status: open
title: ElementTree.write() raises TypeError when xml_declaration = True and
encoding is a unicode string
type: behavior
versions: Python 2.7
___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue15811
___
___
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com