[Python-checkins] cpython (merge 3.5 -> default): Issue #25047: Merge Element Tree encoding from 3.5

martin.panter python-checkins at python.org
Wed Sep 23 04:08:28 CEST 2015


https://hg.python.org/cpython/rev/409bab2181d3
changeset:   98203:409bab2181d3
parent:      98200:1438ac605924
parent:      98202:9c248233754c
user:        Martin Panter <vadmium+py at gmail.com>
date:        Wed Sep 23 01:49:24 2015 +0000
summary:
  Issue #25047: Merge Element Tree encoding from 3.5

files:
  Lib/test/test_xml_etree.py   |  21 ++++++++++++++-------
  Lib/xml/etree/ElementTree.py |   9 ++++-----
  Misc/NEWS                    |   4 ++++
  3 files changed, 22 insertions(+), 12 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
@@ -2396,14 +2396,21 @@
         elem = ET.Element("tag")
         elem.text = "abc"
         self.assertEqual(serialize(elem), '<tag>abc</tag>')
-        self.assertEqual(serialize(elem, encoding="utf-8"),
-                b'<tag>abc</tag>')
-        self.assertEqual(serialize(elem, encoding="us-ascii"),
-                b'<tag>abc</tag>')
+        for enc in ("utf-8", "us-ascii"):
+            with self.subTest(enc):
+                self.assertEqual(serialize(elem, encoding=enc),
+                        b'<tag>abc</tag>')
+                self.assertEqual(serialize(elem, encoding=enc.upper()),
+                        b'<tag>abc</tag>')
         for enc in ("iso-8859-1", "utf-16", "utf-32"):
-            self.assertEqual(serialize(elem, encoding=enc),
-                    ("<?xml version='1.0' encoding='%s'?>\n"
-                     "<tag>abc</tag>" % enc).encode(enc))
+            with self.subTest(enc):
+                self.assertEqual(serialize(elem, encoding=enc),
+                        ("<?xml version='1.0' encoding='%s'?>\n"
+                         "<tag>abc</tag>" % enc).encode(enc))
+                upper = enc.upper()
+                self.assertEqual(serialize(elem, encoding=upper),
+                        ("<?xml version='1.0' encoding='%s'?>\n"
+                         "<tag>abc</tag>" % upper).encode(enc))
 
         elem = ET.Element("tag")
         elem.text = "<&\"\'>"
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
@@ -752,14 +752,13 @@
                 encoding = "utf-8"
             else:
                 encoding = "us-ascii"
-        else:
-            encoding = encoding.lower()
-        with _get_writer(file_or_filename, encoding) as write:
+        enc_lower = encoding.lower()
+        with _get_writer(file_or_filename, enc_lower) as write:
             if method == "xml" and (xml_declaration or
                     (xml_declaration is None and
-                     encoding not in ("utf-8", "us-ascii", "unicode"))):
+                     enc_lower not in ("utf-8", "us-ascii", "unicode"))):
                 declared_encoding = encoding
-                if encoding == "unicode":
+                if enc_lower == "unicode":
                     # Retrieve the default encoding for the xml declaration
                     import locale
                     declared_encoding = locale.getpreferredencoding()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -138,6 +138,10 @@
 Library
 -------
 
+- Issue #25047: The XML encoding declaration written by Element Tree now
+  respects the letter case given by the user. This restores the ability to
+  write encoding names in uppercase like "UTF-8", which worked in Python 2.
+
 - Issue #19143: platform module now reads Windows version from kernel32.dll to
   avoid compatibility shims.
 

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


More information about the Python-checkins mailing list