diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2022-05-11 17:36:52 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-11 17:36:52 (GMT) |
commit | 20fec2c265f7f47304041abcafdffe7de9937682 (patch) | |
tree | e3887769d057cd710dddc29e666ac245b2c899c8 /Lib/test | |
parent | 5ea8a93e1a35bf08153b5d02e9a332cd9b531241 (diff) | |
download | cpython-20fec2c265f7f47304041abcafdffe7de9937682.zip cpython-20fec2c265f7f47304041abcafdffe7de9937682.tar.gz cpython-20fec2c265f7f47304041abcafdffe7de9937682.tar.bz2 |
[3.11] gh-91810: ElementTree: Use text file's encoding by default in XML declaration (GH-91903) (GH-92663)
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.
(cherry picked from commit 707839b0fe02ba2c891a40f40e7a869d84c2c9c5)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Automerge-Triggered-By: GH:serhiy-storchaka
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_xml_etree.py | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index db25eab..aea77b1 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -10,7 +10,6 @@ import functools import html import io import itertools -import locale import operator import os import pickle @@ -978,15 +977,13 @@ class ElementTreeTest(unittest.TestCase): def test_tostring_xml_declaration_unicode_encoding(self): elem = ET.XML('<body><tag/></body>') - preferredencoding = locale.getpreferredencoding() self.assertEqual( - f"<?xml version='1.0' encoding='{preferredencoding}'?>\n<body><tag /></body>", - ET.tostring(elem, encoding='unicode', xml_declaration=True) + ET.tostring(elem, encoding='unicode', xml_declaration=True), + "<?xml version='1.0' encoding='utf-8'?>\n<body><tag /></body>" ) def test_tostring_xml_declaration_cases(self): elem = ET.XML('<body><tag>ø</tag></body>') - preferredencoding = locale.getpreferredencoding() TESTCASES = [ # (expected_retval, encoding, xml_declaration) # ... xml_declaration = None @@ -1013,7 +1010,7 @@ class ElementTreeTest(unittest.TestCase): b"<body><tag>ø</tag></body>", 'US-ASCII', True), (b"<?xml version='1.0' encoding='ISO-8859-1'?>\n" b"<body><tag>\xf8</tag></body>", 'ISO-8859-1', True), - (f"<?xml version='1.0' encoding='{preferredencoding}'?>\n" + ("<?xml version='1.0' encoding='utf-8'?>\n" "<body><tag>ø</tag></body>", 'unicode', True), ] @@ -1051,11 +1048,10 @@ class ElementTreeTest(unittest.TestCase): b"<?xml version='1.0' encoding='us-ascii'?>\n<body><tag /></body>" ) - preferredencoding = locale.getpreferredencoding() stringlist = ET.tostringlist(elem, encoding='unicode', xml_declaration=True) self.assertEqual( ''.join(stringlist), - f"<?xml version='1.0' encoding='{preferredencoding}'?>\n<body><tag /></body>" + "<?xml version='1.0' encoding='utf-8'?>\n<body><tag /></body>" ) self.assertRegex(stringlist[0], r"^<\?xml version='1.0' encoding='.+'?>") self.assertEqual(['<body', '>', '<tag', ' />', '</body>'], stringlist[1:]) @@ -3740,17 +3736,16 @@ class IOTest(unittest.TestCase): encoding = f.encoding os_helper.unlink(TESTFN) - try: - '\xf8'.encode(encoding) - except UnicodeEncodeError: - self.skipTest(f'default file encoding {encoding} not supported') - tree = ET.ElementTree(ET.XML('''<site>\xf8</site>''')) tree.write(TESTFN, encoding='unicode') with open(TESTFN, 'rb') as f: data = f.read() expected = "<site>\xf8</site>".encode(encoding, 'xmlcharrefreplace') - self.assertEqual(data, expected) + if encoding.lower() in ('utf-8', 'ascii'): + self.assertEqual(data, expected) + else: + self.assertIn(b"<?xml version='1.0' encoding=", data) + self.assertIn(expected, data) def test_write_to_text_file(self): self.addCleanup(os_helper.unlink, TESTFN) @@ -3765,13 +3760,17 @@ class IOTest(unittest.TestCase): tree.write(f, encoding='unicode') self.assertFalse(f.closed) with open(TESTFN, 'rb') as f: - self.assertEqual(f.read(), b'''<site>ø</site>''') + self.assertEqual(f.read(), convlinesep( + b'''<?xml version='1.0' encoding='ascii'?>\n''' + b'''<site>ø</site>''')) with open(TESTFN, 'w', encoding='ISO-8859-1') as f: tree.write(f, encoding='unicode') self.assertFalse(f.closed) with open(TESTFN, 'rb') as f: - self.assertEqual(f.read(), b'''<site>\xf8</site>''') + self.assertEqual(f.read(), convlinesep( + b'''<?xml version='1.0' encoding='ISO-8859-1'?>\n''' + b'''<site>\xf8</site>''')) def test_write_to_binary_file(self): self.addCleanup(os_helper.unlink, TESTFN) |