From 89f76d3f913e0527fbcc0d15cb3c17fbf6ca8618 Mon Sep 17 00:00:00 2001 From: Martin Panter Date: Wed, 23 Sep 2015 01:14:35 +0000 Subject: [PATCH] Issue #25047: Respect case writing XML encoding declarations This restores the ability to write encoding names in uppercase like "UTF-8", which worked in Python 2. --- 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 index 1c6a939c29a..b87b09832d1 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -2396,14 +2396,21 @@ class IOTest(unittest.TestCase): elem = ET.Element("tag") elem.text = "abc" self.assertEqual(serialize(elem), 'abc') - self.assertEqual(serialize(elem, encoding="utf-8"), - b'abc') - self.assertEqual(serialize(elem, encoding="us-ascii"), - b'abc') + for enc in ("utf-8", "us-ascii"): + with self.subTest(enc): + self.assertEqual(serialize(elem, encoding=enc), + b'abc') + self.assertEqual(serialize(elem, encoding=enc.upper()), + b'abc') for enc in ("iso-8859-1", "utf-16", "utf-32"): - self.assertEqual(serialize(elem, encoding=enc), - ("\n" - "abc" % enc).encode(enc)) + with self.subTest(enc): + self.assertEqual(serialize(elem, encoding=enc), + ("\n" + "abc" % enc).encode(enc)) + upper = enc.upper() + self.assertEqual(serialize(elem, encoding=upper), + ("\n" + "abc" % upper).encode(enc)) elem = ET.Element("tag") elem.text = "<&\"\'>" diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index a8585b6e2f4..97eba8bfcd6 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -756,14 +756,13 @@ class ElementTree: 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 index 90d9e1f7283..347dd6fa81e 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -81,6 +81,10 @@ Core and Builtins 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.