[New-bugs-announce] [issue10942] xml.etree.ElementTree.tostring returns type bytes, expected type str

J_Tom_Moon_79 report at bugs.python.org
Wed Jan 19 01:12:29 CET 2011

New submission from J_Tom_Moon_79 <jtm.moon.forum.user+python at gmail.com>:

method xml.etree.ElementTree.tostring from module returns type bytes.
The documentation reads
"""Returns an encoded string containing the XML data."""
(from http://docs.python.org/py3k/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring as of 2011-01-18)

Here is a test program:
# created for python 3.1

import sys
print(sys.version) # for help verifying version tested
from xml.etree import ElementTree

sampleinput = """<?xml version="1.0"?><Hello></Hello>"""
xmlobj = ElementTree.fromstring(sampleinput)
xmlstr = ElementTree.tostring(xmlobj,'utf-8')
print("xmlstr value is '", xmlstr, "'", sep="")
print("xmlstr type is '", type(xmlstr), "'", sep="")
test program output:
3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)]
xmlstr value is 'b'<Hello />''
xmlstr type is '<class 'bytes'>'

This cheap "fix" for this bug may be simply be a change in documentation.
However, a method called "tostring" really should return something nearer to the built-in str.

assignee: docs at python
components: Documentation, XML
messages: 126506
nosy: JTMoon79, docs at python
priority: normal
severity: normal
status: open
title: xml.etree.ElementTree.tostring returns type bytes, expected type str
type: behavior
versions: Python 3.1

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list