[Python-checkins] cpython (merge 3.5 -> default): merge

raymond.hettinger python-checkins at python.org
Mon Sep 12 02:23:30 EDT 2016


https://hg.python.org/cpython/rev/934336599bc6
changeset:   103695:934336599bc6
parent:      103693:63f6ac38d18d
parent:      103694:0a5596315cf0
user:        Raymond Hettinger <python at rcn.com>
date:        Sun Sep 11 23:23:24 2016 -0700
summary:
  merge

files:
  Lib/test/test_xml_etree.py   |   8 ++++++++
  Lib/xml/etree/ElementTree.py |  11 +++++++++++
  Misc/NEWS                    |   3 +++
  3 files changed, 22 insertions(+), 0 deletions(-)


diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -409,6 +409,14 @@
         self.assertEqual(ET.tostring(elem),
                 b'<test testa="testval" testb="test1" testc="test2">aa</test>')
 
+        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'<test a="
" b="
" c="	

 " d="

" />')
+
     def test_makeelement(self):
         # Test makeelement handling.
 
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -1084,8 +1084,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 --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -209,6 +209,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.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list