Skip to content
This repository was archived by the owner on Feb 13, 2025. It is now read-only.

Commit 076366c

Browse files
committed
Issue python#17582: xml.etree.ElementTree nows preserves whitespaces in attributes
(Patch by Duane Griffin. Reviewed and approved by Stefan Behnel.)
1 parent 4b73676 commit 076366c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Lib/test/test_xml_etree.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,14 @@ def test_attrib(self):
405405
self.assertEqual(ET.tostring(elem),
406406
b'<test testa="testval" testb="test1" testc="test2">aa</test>')
407407

408+
elem = ET.Element('test')
409+
elem.set('a', '\r')
410+
elem.set('b', '\r\n')
411+
elem.set('c', '\t\n\r ')
412+
elem.set('d', '\n\n')
413+
self.assertEqual(ET.tostring(elem),
414+
b'<test a="&#10;" b="&#10;" c="&#09;&#10;&#10; " d="&#10;&#10;" />')
415+
408416
def test_makeelement(self):
409417
# Test makeelement handling.
410418

Lib/xml/etree/ElementTree.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,8 +1083,19 @@ def _escape_attrib(text):
10831083
text = text.replace(">", "&gt;")
10841084
if "\"" in text:
10851085
text = text.replace("\"", "&quot;")
1086+
# The following business with carriage returns is to satisfy
1087+
# Section 2.11 of the XML specification, stating that
1088+
# CR or CR LN should be replaced with just LN
1089+
# http://www.w3.org/TR/REC-xml/#sec-line-ends
1090+
if "\r\n" in text:
1091+
text = text.replace("\r\n", "\n")
1092+
if "\r" in text:
1093+
text = text.replace("\r", "\n")
1094+
#The following four lines are issue 17582
10861095
if "\n" in text:
10871096
text = text.replace("\n", "&#10;")
1097+
if "\t" in text:
1098+
text = text.replace("\t", "&#09;")
10881099
return text
10891100
except (TypeError, AttributeError):
10901101
_raise_serialization_error(text)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ Library
8383

8484
- Issue #24594: Validates persist parameter when opening MSI database
8585

86+
- Issue #17582: xml.etree.ElementTree nows preserves whitespaces in attributes
87+
(Patch by Duane Griffin. Reviewed and approved by Stefan Behnel.)
88+
8689
- Issue #28047: Fixed calculation of line length used for the base64 CTE
8790
in the new email policies.
8891

0 commit comments

Comments
 (0)