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.
This commit is contained in:
parent
f94471c140
commit
89f76d3f91
@ -2396,14 +2396,21 @@ class IOTest(unittest.TestCase):
|
|||||||
elem = ET.Element("tag")
|
elem = ET.Element("tag")
|
||||||
elem.text = "abc"
|
elem.text = "abc"
|
||||||
self.assertEqual(serialize(elem), '<tag>abc</tag>')
|
self.assertEqual(serialize(elem), '<tag>abc</tag>')
|
||||||
self.assertEqual(serialize(elem, encoding="utf-8"),
|
for enc in ("utf-8", "us-ascii"):
|
||||||
|
with self.subTest(enc):
|
||||||
|
self.assertEqual(serialize(elem, encoding=enc),
|
||||||
b'<tag>abc</tag>')
|
b'<tag>abc</tag>')
|
||||||
self.assertEqual(serialize(elem, encoding="us-ascii"),
|
self.assertEqual(serialize(elem, encoding=enc.upper()),
|
||||||
b'<tag>abc</tag>')
|
b'<tag>abc</tag>')
|
||||||
for enc in ("iso-8859-1", "utf-16", "utf-32"):
|
for enc in ("iso-8859-1", "utf-16", "utf-32"):
|
||||||
|
with self.subTest(enc):
|
||||||
self.assertEqual(serialize(elem, encoding=enc),
|
self.assertEqual(serialize(elem, encoding=enc),
|
||||||
("<?xml version='1.0' encoding='%s'?>\n"
|
("<?xml version='1.0' encoding='%s'?>\n"
|
||||||
"<tag>abc</tag>" % enc).encode(enc))
|
"<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 = ET.Element("tag")
|
||||||
elem.text = "<&\"\'>"
|
elem.text = "<&\"\'>"
|
||||||
|
@ -756,14 +756,13 @@ class ElementTree:
|
|||||||
encoding = "utf-8"
|
encoding = "utf-8"
|
||||||
else:
|
else:
|
||||||
encoding = "us-ascii"
|
encoding = "us-ascii"
|
||||||
else:
|
enc_lower = encoding.lower()
|
||||||
encoding = encoding.lower()
|
with _get_writer(file_or_filename, enc_lower) as write:
|
||||||
with _get_writer(file_or_filename, encoding) as write:
|
|
||||||
if method == "xml" and (xml_declaration or
|
if method == "xml" and (xml_declaration or
|
||||||
(xml_declaration is None and
|
(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
|
declared_encoding = encoding
|
||||||
if encoding == "unicode":
|
if enc_lower == "unicode":
|
||||||
# Retrieve the default encoding for the xml declaration
|
# Retrieve the default encoding for the xml declaration
|
||||||
import locale
|
import locale
|
||||||
declared_encoding = locale.getpreferredencoding()
|
declared_encoding = locale.getpreferredencoding()
|
||||||
|
@ -81,6 +81,10 @@ Core and Builtins
|
|||||||
Library
|
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
|
- Issue #19143: platform module now reads Windows version from kernel32.dll to
|
||||||
avoid compatibility shims.
|
avoid compatibility shims.
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user