X Tutup
The Wayback Machine - https://web.archive.org/web/20250609135937/https://github.com/python/cpython/issues/91767
Skip to content

ElementTree.tostring() changes text by not escaping carriage return during serialization #91767

Closed
@mforner

Description

@mforner

The function ET.tostring() does not escape carriage return (\r) when serializing a XML tree. This eventually causes the \r to disappear when re-parsing the output of ET.tostring().

>>> import xml.etree.ElementTree as ET
>>> xmlstr = "<elem>x&#13;&#10;</elem>" 
>>> ET.tostring(ET.fromstring(xmlstr))
b'<foo>x\r\n</foo>'
>>> ET.tostring(ET.fromstring(_))
b'<foo>x\n</foo>'

This happens with Python 2.7.12 and Python 3.8.1.
I also tried lxml and the C++ libraries TinyXml and Xerces whith this problem and all of them replaced \r with &#13; or '&#xD;, making the tostring() function reversible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-XMLtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup