Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xml.etree.ElementTree.tostring returns type bytes, expected type str #55151

Closed
JTMoon79 mannequin opened this issue Jan 19, 2011 · 5 comments
Closed

xml.etree.ElementTree.tostring returns type bytes, expected type str #55151

JTMoon79 mannequin opened this issue Jan 19, 2011 · 5 comments
Labels
docs Documentation in the Doc dir topic-XML type-bug An unexpected behavior, bug, or error

Comments

@JTMoon79
Copy link
Mannequin

JTMoon79 mannequin commented Jan 19, 2011

BPO 10942
Nosy @bitdancer, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2015-11-26.17:17:05.889>
created_at = <Date 2011-01-19.00:12:25.785>
labels = ['expert-XML', 'type-bug', 'docs']
title = 'xml.etree.ElementTree.tostring returns type bytes, expected type str'
updated_at = <Date 2015-11-26.17:17:05.889>
user = 'https://bugs.python.org/JTMoon79'

bugs.python.org fields:

activity = <Date 2015-11-26.17:17:05.889>
actor = 'serhiy.storchaka'
assignee = 'docs@python'
closed = True
closed_date = <Date 2015-11-26.17:17:05.889>
closer = 'serhiy.storchaka'
components = ['Documentation', 'XML']
creation = <Date 2011-01-19.00:12:25.785>
creator = 'JTMoon79'
dependencies = []
files = []
hgrepos = []
issue_num = 10942
keywords = []
message_count = 5.0
messages = ['126506', '126507', '126517', '159021', '255058']
nosy_count = 5.0
nosy_names = ['r.david.murray', 'docs@python', 'JTMoon79', 'serhiy.storchaka', 'Gunnar.Eikman']
pr_nums = []
priority = 'normal'
resolution = 'out of date'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue10942'
versions = ['Python 3.1']

@JTMoon79
Copy link
Mannequin Author

JTMoon79 mannequin commented Jan 19, 2011

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:
-------------------------------------------------------
#!/usr/bin/python
# 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)
type(xmlobj)
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.

@JTMoon79 JTMoon79 mannequin assigned docspython Jan 19, 2011
@JTMoon79 JTMoon79 mannequin added docs Documentation in the Doc dir topic-XML type-bug An unexpected behavior, bug, or error labels Jan 19, 2011
@JTMoon79
Copy link
Mannequin Author

JTMoon79 mannequin commented Jan 19, 2011

Some other bugs affecting the tostring method (for consideration by the reviewer):
http://bugs.python.org/issue6233#msg89718
http://bugs.python.org/msg101037
http://bugs.python.org/issue9692

@bitdancer
Copy link
Member

This is indeed a doc problem, although there was some discussion of working toward a method rename. See bpo-8047 (but be prepared to read a novel to understand why tostring returns bytes...) The doc for 3.2 is slightly clearer, but both 3.1 and 3.2 could be made clearer by referring to an 'encoded byte string' rather than just an 'encoded string'. (An encoded string has to be a byte string, but that isn't obvious unless you've dealt with encode/decode a bunch.)

Technically this could be closed as a duplicate of bpo-8047, since that issue proposes that the API fix (which would include the doc change) be backported to 3.1. But no one has proposed a patch there, so at a minimum the 3.1 docs should be clarified.

@GunnarEikman
Copy link
Mannequin

GunnarEikman mannequin commented Apr 23, 2012

I moved a working script from Ubuntu (Python 3.1.2) to Windows (Python 3.2.3) today.

Had to revise script. The tostring method returns a string on Linux (contradicts this issue), but bytes on Windows (as described in this issue)...

I used tostring with a single argument "tostring(theXml)"

Is there an explanation for this? I am not an advanced Python hacker...

Be careful when moving from one environment to another!

@serhiy-storchaka
Copy link
Member

For now the documentation explains the resulting type of tostring().

https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring
"""
Generates a string representation of an XML element, including all subelements. element is an Element instance. encoding [1] is the output encoding (default is US-ASCII). Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring is generated). method is either "xml", "html" or "text" (default is "xml").
"""

Looks as this issue can be closed.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir topic-XML type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants