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
Comments
method xml.etree.ElementTree.tostring from module returns type bytes. ======================================================= 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: This cheap "fix" for this bug may be simply be a change in documentation. |
Some other bugs affecting the tostring method (for consideration by the reviewer): |
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. |
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! |
For now the documentation explains the resulting type of tostring(). https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring Looks as this issue can be closed. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: