[New-bugs-announce] [issue39011] ElementTree attributes replace "\r" with "\n"

mefistotelis report at bugs.python.org
Mon Dec 9 18:40:50 EST 2019

New submission from mefistotelis <mefistotelis at gmail.com>:

If I place "\r" in an Element attribute, it is handled and idiomized to "
" in the XML file. But wait - \r is not really code 10, right?

Real description:

If I create ElementTree and read it just after creation, I'm getting what I put there - "\r". But if I save and re-load, it transforms into "\n". The character is incorrectly converted before being idiomized, and saved XML file has invalid value stored.

Quick repro:

# python3 -i
Python 3.8.0 (default, Oct 25 2019, 06:23:40)  [GCC 9.2.0 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import xml.etree.ElementTree as ET
>>> elem = ET.Element('TEST')
>>> elem.set("Attt", "a\x0db")
>>> tree = ET.ElementTree(elem)
>>> with open("_test1.xml", "wb") as xml_fh:
...     tree.write(xml_fh, encoding='utf-8', xml_declaration=True)
>>> tree.getroot().get("Attt")
>>> tree = ET.parse("_test1.xml")
>>> tree.getroot().get("Attt")

Related issue: https://bugs.python.org/issue5752
(keeping this one separate as it seem to be a simple bug, easy to fix outside of the discussion there)

If there's a good workaround - please let me know.

Tested on Windows, v3.8 and v3.6

components: XML
messages: 358154
nosy: mefistotelis
priority: normal
severity: normal
status: open
title: ElementTree attributes replace "\r" with "\n"
type: behavior
versions: Python 3.6, Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list