changeset: 103694:0a5596315cf0 branch: 3.5 parent: 103692:74b84014bc27 user: Raymond Hettinger date: Sun Sep 11 23:18:03 2016 -0700 files: Lib/test/test_xml_etree.py Lib/xml/etree/ElementTree.py Misc/NEWS description: Issue #17582: xml.etree.ElementTree nows preserves whitespaces in attributes (Patch by Duane Griffin. Reviewed and approved by Stefan Behnel.) diff -r 74b84014bc27 -r 0a5596315cf0 Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Mon Sep 12 01:50:03 2016 -0400 +++ b/Lib/test/test_xml_etree.py Sun Sep 11 23:18:03 2016 -0700 @@ -405,6 +405,14 @@ self.assertEqual(ET.tostring(elem), b'aa') + elem = ET.Element('test') + elem.set('a', '\r') + elem.set('b', '\r\n') + elem.set('c', '\t\n\r ') + elem.set('d', '\n\n') + self.assertEqual(ET.tostring(elem), + b'') + def test_makeelement(self): # Test makeelement handling. diff -r 74b84014bc27 -r 0a5596315cf0 Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py Mon Sep 12 01:50:03 2016 -0400 +++ b/Lib/xml/etree/ElementTree.py Sun Sep 11 23:18:03 2016 -0700 @@ -1083,8 +1083,19 @@ text = text.replace(">", ">") if "\"" in text: text = text.replace("\"", """) + # The following business with carriage returns is to satisfy + # Section 2.11 of the XML specification, stating that + # CR or CR LN should be replaced with just LN + # http://www.w3.org/TR/REC-xml/#sec-line-ends + if "\r\n" in text: + text = text.replace("\r\n", "\n") + if "\r" in text: + text = text.replace("\r", "\n") + #The following four lines are issue 17582 if "\n" in text: text = text.replace("\n", " ") + if "\t" in text: + text = text.replace("\t", " ") return text except (TypeError, AttributeError): _raise_serialization_error(text) diff -r 74b84014bc27 -r 0a5596315cf0 Misc/NEWS --- a/Misc/NEWS Mon Sep 12 01:50:03 2016 -0400 +++ b/Misc/NEWS Sun Sep 11 23:18:03 2016 -0700 @@ -83,6 +83,9 @@ - Issue #24594: Validates persist parameter when opening MSI database +- Issue #17582: xml.etree.ElementTree nows preserves whitespaces in attributes + (Patch by Duane Griffin. Reviewed and approved by Stefan Behnel.) + - Issue #28047: Fixed calculation of line length used for the base64 CTE in the new email policies.