Skip to content

xml.etree.ElementTree.ElementTree.write(): encoding handling problems #53703

Closed

Description

@kune
mannequin
BPO 9458
Nosy @vstinner, @merwok, @florentx, @dabrahams, @serhiy-storchaka
Files
  • bugs.tar.gz: Tar container with bug examples
  • ElementTree.patch: Patch for xml.etree.ElementTree.py
  • 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 2012-07-15.03:20:58.401>
    created_at = <Date 2010-08-02.21:02:04.633>
    labels = ['expert-XML', 'type-bug', 'docs']
    title = 'xml.etree.ElementTree.ElementTree.write(): encoding handling problems'
    updated_at = <Date 2012-07-15.03:20:58.398>
    user = 'https://bugs.python.org/kune'

    bugs.python.org fields:

    activity = <Date 2012-07-15.03:20:58.398>
    actor = 'eli.bendersky'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2012-07-15.03:20:58.401>
    closer = 'eli.bendersky'
    components = ['Documentation', 'XML']
    creation = <Date 2010-08-02.21:02:04.633>
    creator = 'kune'
    dependencies = []
    files = ['18349', '18350']
    hgrepos = []
    issue_num = 9458
    keywords = ['patch']
    message_count = 12.0
    messages = ['112550', '112568', '112653', '112658', '148280', '158903', '158905', '164714', '164745', '165378', '165493', '165494']
    nosy_count = 10.0
    nosy_names = ['effbot', 'vstinner', 'eric.araujo', 'kune', 'eli.bendersky', 'flox', 'docs@python', 'dabrahams', 'python-dev', 'serhiy.storchaka']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'test needed'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue9458'
    versions = ['Python 2.7', 'Python 3.2', 'Python 3.3']

    Activity

    1. kune commented on Aug 2, 2010

      kunemannequin
      MannequinAuthor

      If one wants to use the encoding parameter of ElementTree.write() the file must be opened with "wb". Without encoding parameter normal files can be used, but the should be opened with the encoding "UTF-8", because otherwise this may create an error.

      Probably comparable problems exist with the parser side of things.

    2. merwok commented on Aug 3, 2010

      @merwok
      Member

      Is this a behavior bug or a doc bug?

    3. kune commented on Aug 3, 2010

      kunemannequin
      MannequinAuthor

      I believe handling of TextIOWrapper streams is broken in xml.etree.ElementTree.ElementTree.write().

      First example:

      import sys
      from xml.etree import ElementTree
      
      element = ElementTree.fromstring("""<foo><bar>foobar</bar></foo>""")
      element_tree = ElementTree.ElementTree(element)

      assert sys.stdout.encoding == "UTF-8"
      element_tree.write(sys.stdout, encoding="UTF-8")
      print()

      I don't think that write a tree into a stream with the correct encoding should generate any problem at all.

      The output looks like this:

      Traceback (most recent call last):
        File "/home/kunitz/test/lib/python3.2/xml/etree/ElementTree.py", line 825, in write
          "xmlcharrefreplace"))
      TypeError: must be str, not bytes
      
      During handling of the above exception, another exception occurred:
      
      Traceback (most recent call last):
        File "bug1.py", line 9, in <module>
          element_tree.write(sys.stdout, encoding="UTF-8")
        File "/home/kunitz/test/lib/python3.2/xml/etree/ElementTree.py", line 843, in write
          write("<?xml version='1.0' encoding='%s'?>\n" % encoding_)
        File "/home/kunitz/test/lib/python3.2/xml/etree/ElementTree.py", line 827, in write
          _raise_serialization_error(text)
        File "/home/kunitz/test/lib/python3.2/xml/etree/ElementTree.py", line 1077, in _raise_serialization_error
          "cannot serialize %r (type %s)" % (text, type(text).__name__)
      TypeError: cannot serialize "<?xml version='1.0' encoding='UTF-8'?>\n" (type str)

      Example 2:
      import sys
      from xml.etree import ElementTree

      element = ElementTree.fromstring("""<foo><bar>fööbar</bar></foo>""")
      element_tree = ElementTree.ElementTree(element)
      
      with open("bug2.xml", "w", encoding="US-ASCII") as f:
          element_tree.write(f)

      The first ö umlaut generates an UnicodeEncodeError here, while the method could use XML character references. One could argue this, but the method could take care of the problem.

      Third example:
      import sys
      from xml.etree import ElementTree

      element = ElementTree.fromstring("""<foo><bar>fööbar</bar></foo>""")
      element_tree = ElementTree.ElementTree(element)

      with open("bug3.xml", "w", encoding="ISO-8859-1",
      errors="xmlcharrefreplace") as f:
      element_tree.write(f, xml_declaration=True)

      This creates finally an ISO-8859-1 encoded XML file, but without XML declaration. Didn't we request one?

      Example 4: Try to do the right thing.
      import sys
      from xml.etree import ElementTree

      element = ElementTree.fromstring("""<foo><bar>fööbar</bar></foo>""")
      element_tree = ElementTree.ElementTree(element)

      with open("bug4.xml", "w", encoding="ISO-8859-1",
      errors="xmlcharrefreplace") as f:
      element_tree.write(f, encoding="ISO-8859-1", xml_declaration=True)

      Here we get the same exception as example 1 of course.

      All the files can be found in the tar container below.

    4. changed the title [-]xml.etree.ElementTree.write(): encoding handling problems[/-] [+]xml.etree.ElementTree.ElementTree.write(): encoding handling problems[/+] on Aug 3, 2010
    5. added
      type-bugAn unexpected behavior, bug, or error
      on Aug 3, 2010
    6. kune commented on Aug 3, 2010

      kunemannequin
      MannequinAuthor

      Here is a patch that handles all 4 examples in the last comment correctly and survives the Python test suite on Linux (Ubuntu 9.04 x86-64).

    7. merwok commented on Nov 24, 2011

      @merwok
      Member

      Thanks for the patch. The examples in your message need to be converted to a patch that applies to 3.2 or 2.7, so that we can reproduce the bug before fixing it.

    8. dabrahams commented on Apr 21, 2012

      dabrahamsmannequin
      Mannequin

      These bugs are annoying. How does one convert a set of examples into a patch? Do you mean you want these to become test cases?

    9. merwok commented on Apr 21, 2012

      @merwok
      Member

      Yes. See the devguide if you need more info.

    10. elibendersky commented on Jul 6, 2012

      elibenderskymannequin
      Mannequin

      Please make sure that the patch(es) apply cleanly to 3.3, since this is the version I'll be focusing on.

    11. dabrahams commented on Jul 6, 2012

      dabrahamsmannequin
      Mannequin

      I won't get to this, FYI.

    12. serhiy-storchaka commented on Jul 13, 2012

      @serhiy-storchaka
      Member

      ElementTree write works with two kinds of output -- binary and text. The difference between them is only determined by encoding argument. If encoding is "unicode", then output is text, else it is binary. There is no other way for filename or general file-like object to determine kind of output. If these are not explained in the documentation, then the documentation should be improved.

      The patch can cause data corruption because direct writing to underlying file by fileno conflicts with TextIOBase/BufferedIOBase internal buffering. And not every file-like object have fileno. With patch the behavior becomes less obvious and will lead to confusion.

      I don't see a behavior bug which should be fixed.

      Only one thing can be enhanced -- error diagnostic in some corner cases. When we can determines that file object is instance of RawIOBase or TextIOBase and it is conflicts with encoding argument value, it will be helpful for novices to raise a descriptive exception. This is of course not eliminate all causes for confusing.

    13. python-dev commented on Jul 15, 2012

      python-devmannequin
      Mannequin

      New changeset 51b5ee7cfa3b by Eli Bendersky in branch 'default':
      Issue bpo-9458: clarify the documentation of ElementTree.write with regards to the type of the stream expected for a given encoding
      http://hg.python.org/cpython/rev/51b5ee7cfa3b

    14. elibendersky commented on Jul 15, 2012

      elibenderskymannequin
      Mannequin

      I agree with Serhiy that this is more of a documentation/understanding issue than a real bug. I've clarified the doc of ElementTree.write a bit to make it explicit what stream is expected for 'write'.

    15. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      docsDocumentation in the Doc dirtopic-XMLtype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions