# HG changeset patch # Parent 8f94e695f56b79492a23f19105fd6da8e0146e1f 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. diff -r 8f94e695f56b Lib/test/test_xml_etree.py --- a/Lib/test/test_xml_etree.py Tue Sep 15 19:59:03 2015 +0300 +++ b/Lib/test/test_xml_etree.py Fri Sep 18 02:37:40 2015 +0000 @@ -2396,14 +2396,21 @@ 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 -r 8f94e695f56b Lib/xml/etree/ElementTree.py --- a/Lib/xml/etree/ElementTree.py Tue Sep 15 19:59:03 2015 +0300 +++ b/Lib/xml/etree/ElementTree.py Fri Sep 18 02:37:40 2015 +0000 @@ -756,14 +756,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 -r 8f94e695f56b Misc/NEWS --- a/Misc/NEWS Tue Sep 15 19:59:03 2015 +0300 +++ b/Misc/NEWS Fri Sep 18 02:37:40 2015 +0000 @@ -81,6 +81,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 #24684: socket.socket.getaddrinfo() now calls PyUnicode_AsEncodedString() instead of calling the encode() method of the host, to handle correctly custom string with an encode() method which doesn't