Skip to content

Commit 982a08f

Browse files
committed
Issue #25047: Merge Element Tree encoding from 3.4 into 3.5
2 parents 5f62112 + 89f76d3 commit 982a08f

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

Lib/test/test_xml_etree.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,14 +2396,21 @@ def test_encoding(self):
23962396
elem = ET.Element("tag")
23972397
elem.text = "abc"
23982398
self.assertEqual(serialize(elem), '<tag>abc</tag>')
2399-
self.assertEqual(serialize(elem, encoding="utf-8"),
2400-
b'<tag>abc</tag>')
2401-
self.assertEqual(serialize(elem, encoding="us-ascii"),
2402-
b'<tag>abc</tag>')
2399+
for enc in ("utf-8", "us-ascii"):
2400+
with self.subTest(enc):
2401+
self.assertEqual(serialize(elem, encoding=enc),
2402+
b'<tag>abc</tag>')
2403+
self.assertEqual(serialize(elem, encoding=enc.upper()),
2404+
b'<tag>abc</tag>')
24032405
for enc in ("iso-8859-1", "utf-16", "utf-32"):
2404-
self.assertEqual(serialize(elem, encoding=enc),
2405-
("<?xml version='1.0' encoding='%s'?>\n"
2406-
"<tag>abc</tag>" % enc).encode(enc))
2406+
with self.subTest(enc):
2407+
self.assertEqual(serialize(elem, encoding=enc),
2408+
("<?xml version='1.0' encoding='%s'?>\n"
2409+
"<tag>abc</tag>" % enc).encode(enc))
2410+
upper = enc.upper()
2411+
self.assertEqual(serialize(elem, encoding=upper),
2412+
("<?xml version='1.0' encoding='%s'?>\n"
2413+
"<tag>abc</tag>" % upper).encode(enc))
24072414

24082415
elem = ET.Element("tag")
24092416
elem.text = "<&\"\'>"

Lib/xml/etree/ElementTree.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -752,14 +752,13 @@ def write(self, file_or_filename,
752752
encoding = "utf-8"
753753
else:
754754
encoding = "us-ascii"
755-
else:
756-
encoding = encoding.lower()
757-
with _get_writer(file_or_filename, encoding) as write:
755+
enc_lower = encoding.lower()
756+
with _get_writer(file_or_filename, enc_lower) as write:
758757
if method == "xml" and (xml_declaration or
759758
(xml_declaration is None and
760-
encoding not in ("utf-8", "us-ascii", "unicode"))):
759+
enc_lower not in ("utf-8", "us-ascii", "unicode"))):
761760
declared_encoding = encoding
762-
if encoding == "unicode":
761+
if enc_lower == "unicode":
763762
# Retrieve the default encoding for the xml declaration
764763
import locale
765764
declared_encoding = locale.getpreferredencoding()

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #25047: The XML encoding declaration written by Element Tree now
22+
respects the letter case given by the user. This restores the ability to
23+
write encoding names in uppercase like "UTF-8", which worked in Python 2.
24+
2125
- Issue #19143: platform module now reads Windows version from kernel32.dll to
2226
avoid compatibility shims.
2327

0 commit comments

Comments
 (0)