Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 11fa3ff

Browse files
committed
merge
2 parents 3dad1a5 + 076366c commit 11fa3ff

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

Lib/test/test_xml_etree.py

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

412+
elem = ET.Element('test')
413+
elem.set('a', '\r')
414+
elem.set('b', '\r\n')
415+
elem.set('c', '\t\n\r ')
416+
elem.set('d', '\n\n')
417+
self.assertEqual(ET.tostring(elem),
418+
b'<test a="&#10;" b="&#10;" c="&#09;&#10;&#10; " d="&#10;&#10;" />')
419+
412420
def test_makeelement(self):
413421
# Test makeelement handling.
414422

Lib/xml/etree/ElementTree.py

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

Misc/NEWS

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

210210
- Issue #24594: Validates persist parameter when opening MSI database
211211

212+
- Issue #17582: xml.etree.ElementTree nows preserves whitespaces in attributes
213+
(Patch by Duane Griffin. Reviewed and approved by Stefan Behnel.)
214+
212215
- Issue #28047: Fixed calculation of line length used for the base64 CTE
213216
in the new email policies.
214217

0 commit comments

Comments
 (0)