Skip to content

Commit 707839b

Browse files
gh-91810: ElementTree: Use text file's encoding by default in XML declaration (GH-91903)
ElementTree method write() and function tostring() now use the text file's encoding ("UTF-8" if not available) instead of locale encoding in XML declaration when encoding="unicode" is specified.
1 parent 75e4634 commit 707839b

File tree

3 files changed

+29
-30
lines changed

3 files changed

+29
-30
lines changed

Lib/test/test_xml_etree.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import html
1111
import io
1212
import itertools
13-
import locale
1413
import operator
1514
import os
1615
import pickle
@@ -978,15 +977,13 @@ def test_tostring_xml_declaration(self):
978977

979978
def test_tostring_xml_declaration_unicode_encoding(self):
980979
elem = ET.XML('<body><tag/></body>')
981-
preferredencoding = locale.getpreferredencoding()
982980
self.assertEqual(
983-
f"<?xml version='1.0' encoding='{preferredencoding}'?>\n<body><tag /></body>",
984-
ET.tostring(elem, encoding='unicode', xml_declaration=True)
981+
ET.tostring(elem, encoding='unicode', xml_declaration=True),
982+
"<?xml version='1.0' encoding='utf-8'?>\n<body><tag /></body>"
985983
)
986984

987985
def test_tostring_xml_declaration_cases(self):
988986
elem = ET.XML('<body><tag>ø</tag></body>')
989-
preferredencoding = locale.getpreferredencoding()
990987
TESTCASES = [
991988
# (expected_retval, encoding, xml_declaration)
992989
# ... xml_declaration = None
@@ -1013,7 +1010,7 @@ def test_tostring_xml_declaration_cases(self):
10131010
b"<body><tag>&#248;</tag></body>", 'US-ASCII', True),
10141011
(b"<?xml version='1.0' encoding='ISO-8859-1'?>\n"
10151012
b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', True),
1016-
(f"<?xml version='1.0' encoding='{preferredencoding}'?>\n"
1013+
("<?xml version='1.0' encoding='utf-8'?>\n"
10171014
"<body><tag>ø</tag></body>", 'unicode', True),
10181015

10191016
]
@@ -1051,11 +1048,10 @@ def test_tostringlist_xml_declaration(self):
10511048
b"<?xml version='1.0' encoding='us-ascii'?>\n<body><tag /></body>"
10521049
)
10531050

1054-
preferredencoding = locale.getpreferredencoding()
10551051
stringlist = ET.tostringlist(elem, encoding='unicode', xml_declaration=True)
10561052
self.assertEqual(
10571053
''.join(stringlist),
1058-
f"<?xml version='1.0' encoding='{preferredencoding}'?>\n<body><tag /></body>"
1054+
"<?xml version='1.0' encoding='utf-8'?>\n<body><tag /></body>"
10591055
)
10601056
self.assertRegex(stringlist[0], r"^<\?xml version='1.0' encoding='.+'?>")
10611057
self.assertEqual(['<body', '>', '<tag', ' />', '</body>'], stringlist[1:])
@@ -3740,17 +3736,16 @@ def test_write_to_filename_as_unicode(self):
37403736
encoding = f.encoding
37413737
os_helper.unlink(TESTFN)
37423738

3743-
try:
3744-
'\xf8'.encode(encoding)
3745-
except UnicodeEncodeError:
3746-
self.skipTest(f'default file encoding {encoding} not supported')
3747-
37483739
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>'''))
37493740
tree.write(TESTFN, encoding='unicode')
37503741
with open(TESTFN, 'rb') as f:
37513742
data = f.read()
37523743
expected = "<site>\xf8</site>".encode(encoding, 'xmlcharrefreplace')
3753-
self.assertEqual(data, expected)
3744+
if encoding.lower() in ('utf-8', 'ascii'):
3745+
self.assertEqual(data, expected)
3746+
else:
3747+
self.assertIn(b"<?xml version='1.0' encoding=", data)
3748+
self.assertIn(expected, data)
37543749

37553750
def test_write_to_text_file(self):
37563751
self.addCleanup(os_helper.unlink, TESTFN)
@@ -3765,13 +3760,17 @@ def test_write_to_text_file(self):
37653760
tree.write(f, encoding='unicode')
37663761
self.assertFalse(f.closed)
37673762
with open(TESTFN, 'rb') as f:
3768-
self.assertEqual(f.read(), b'''<site>&#248;</site>''')
3763+
self.assertEqual(f.read(), convlinesep(
3764+
b'''<?xml version='1.0' encoding='ascii'?>\n'''
3765+
b'''<site>&#248;</site>'''))
37693766

37703767
with open(TESTFN, 'w', encoding='ISO-8859-1') as f:
37713768
tree.write(f, encoding='unicode')
37723769
self.assertFalse(f.closed)
37733770
with open(TESTFN, 'rb') as f:
3774-
self.assertEqual(f.read(), b'''<site>\xf8</site>''')
3771+
self.assertEqual(f.read(), convlinesep(
3772+
b'''<?xml version='1.0' encoding='ISO-8859-1'?>\n'''
3773+
b'''<site>\xf8</site>'''))
37753774

37763775
def test_write_to_binary_file(self):
37773776
self.addCleanup(os_helper.unlink, TESTFN)

Lib/xml/etree/ElementTree.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -728,16 +728,10 @@ def write(self, file_or_filename,
728728
encoding = "utf-8"
729729
else:
730730
encoding = "us-ascii"
731-
enc_lower = encoding.lower()
732-
with _get_writer(file_or_filename, enc_lower) as write:
731+
with _get_writer(file_or_filename, encoding) as (write, declared_encoding):
733732
if method == "xml" and (xml_declaration or
734733
(xml_declaration is None and
735-
enc_lower not in ("utf-8", "us-ascii", "unicode"))):
736-
declared_encoding = encoding
737-
if enc_lower == "unicode":
738-
# Retrieve the default encoding for the xml declaration
739-
import locale
740-
declared_encoding = locale.getpreferredencoding()
734+
declared_encoding.lower() not in ("utf-8", "us-ascii"))):
741735
write("<?xml version='1.0' encoding='%s'?>\n" % (
742736
declared_encoding,))
743737
if method == "text":
@@ -762,19 +756,20 @@ def _get_writer(file_or_filename, encoding):
762756
write = file_or_filename.write
763757
except AttributeError:
764758
# file_or_filename is a file name
765-
if encoding == "unicode":
766-
file = open(file_or_filename, "w")
759+
if encoding.lower() == "unicode":
760+
file = open(file_or_filename, "w",
761+
errors="xmlcharrefreplace")
767762
else:
768763
file = open(file_or_filename, "w", encoding=encoding,
769764
errors="xmlcharrefreplace")
770765
with file:
771-
yield file.write
766+
yield file.write, file.encoding
772767
else:
773768
# file_or_filename is a file-like object
774769
# encoding determines if it is a text or binary writer
775-
if encoding == "unicode":
770+
if encoding.lower() == "unicode":
776771
# use a text writer as is
777-
yield write
772+
yield write, getattr(file_or_filename, "encoding", None) or "utf-8"
778773
else:
779774
# wrap a binary writer with TextIOWrapper
780775
with contextlib.ExitStack() as stack:
@@ -805,7 +800,7 @@ def _get_writer(file_or_filename, encoding):
805800
# Keep the original file open when the TextIOWrapper is
806801
# destroyed
807802
stack.callback(file.detach)
808-
yield file.write
803+
yield file.write, encoding
809804

810805
def _namespaces(elem, default_namespace=None):
811806
# identify namespaces used in this tree
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
:class:`~xml.etree.ElementTree.ElementTree` method
2+
:meth:`~xml.etree.ElementTree.ElementTree.write` and function
3+
:func:`~xml.etree.ElementTree.tostring` now use the text file's encoding
4+
("UTF-8" if not available) instead of locale encoding in XML declaration
5+
when ``encoding="unicode"`` is specified.

0 commit comments

Comments
 (0)