[issue15811] ElementTree.write() raises TypeError when xml_declaration = True and encoding is a unicode string

2012-08-29 Thread David Buxton

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



[issue15811] ElementTree.write() raises TypeError when xml_declaration = True and encoding is a unicode string

2012-08-29 Thread David Buxton

David Buxton added the comment:

A patch against the current default branch to add tests for the xml_declaration 
keyword argument. This passes when applied to the 2.7 branch too.

This does NOT test whether one can use both bytes/unicode for the encoding 
argument. It just uses the native string type for each interpreter. I don't 
know whether you would actually consider that part of things worth testing yet.

--
keywords: +patch
Added file: http://bugs.python.org/file27046/xml_declaration_test.patch

___
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



[issue15811] ElementTree.write() raises TypeError when xml_declaration = True and encoding is a unicode string

2012-08-29 Thread David Buxton

David Buxton added the comment:

Only a problem because I am using unicode_literals and it didn't occur to me to 
use `str('utf-8')` to get a native string on both 2+3. Much the best solution, 
thank you.

But that is still a little smelly - I think what I want ideally is for 
ElementTree to accept str or unicode on 2.7. Either way I appreciate this is 
very low priority and indeed debatable as to whether it is wrong.

--

___
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