diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py
index 0f45fc71ce7736..22f14a2b4d6330 100644
--- a/Lib/test/test_xml_etree.py
+++ b/Lib/test/test_xml_etree.py
@@ -112,6 +112,9 @@ def newtest(*args, **kwargs):
return newtest
return decorator
+def convlinesep(data):
+ return data.replace(b'\n', os.linesep.encode())
+
class ModuleTest(unittest.TestCase):
def test_sanity(self):
@@ -3654,32 +3657,92 @@ def test_encoding(self):
def test_write_to_filename(self):
self.addCleanup(support.unlink, TESTFN)
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
tree.write(TESTFN)
with open(TESTFN, 'rb') as f:
- self.assertEqual(f.read(), b'''''')
+ self.assertEqual(f.read(), b'''ø''')
+
+ def test_write_to_filename_with_encoding(self):
+ self.addCleanup(support.unlink, TESTFN)
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
+ tree.write(TESTFN, encoding='utf-8')
+ with open(TESTFN, 'rb') as f:
+ self.assertEqual(f.read(), b'''\xc3\xb8''')
+
+ tree.write(TESTFN, encoding='ISO-8859-1')
+ with open(TESTFN, 'rb') as f:
+ self.assertEqual(f.read(), convlinesep(
+ b'''\n'''
+ b'''\xf8'''))
+
+ def test_write_to_filename_as_unicode(self):
+ self.addCleanup(support.unlink, TESTFN)
+ with open(TESTFN, 'w') as f:
+ encoding = f.encoding
+ support.unlink(TESTFN)
+
+ try:
+ '\xf8'.encode(encoding)
+ except UnicodeEncodeError:
+ self.skipTest(f'default file encoding {encoding} not supported')
+
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
+ tree.write(TESTFN, encoding='unicode')
+ with open(TESTFN, 'rb') as f:
+ data = f.read()
+ expected = "\xf8".encode(encoding, 'xmlcharrefreplace')
+ self.assertEqual(data, expected)
def test_write_to_text_file(self):
self.addCleanup(support.unlink, TESTFN)
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
with open(TESTFN, 'w', encoding='utf-8') as f:
tree.write(f, encoding='unicode')
self.assertFalse(f.closed)
with open(TESTFN, 'rb') as f:
- self.assertEqual(f.read(), b'''''')
+ self.assertEqual(f.read(), b'''\xc3\xb8''')
+
+ with open(TESTFN, 'w', encoding='ascii', errors='xmlcharrefreplace') as f:
+ tree.write(f, encoding='unicode')
+ self.assertFalse(f.closed)
+ with open(TESTFN, 'rb') as f:
+ self.assertEqual(f.read(), b'''ø''')
+
+ 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'''\xf8''')
def test_write_to_binary_file(self):
self.addCleanup(support.unlink, TESTFN)
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
with open(TESTFN, 'wb') as f:
tree.write(f)
self.assertFalse(f.closed)
with open(TESTFN, 'rb') as f:
- self.assertEqual(f.read(), b'''''')
+ self.assertEqual(f.read(), b'''ø''')
+
+ def test_write_to_binary_file_with_encoding(self):
+ self.addCleanup(support.unlink, TESTFN)
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
+ with open(TESTFN, 'wb') as f:
+ tree.write(f, encoding='utf-8')
+ self.assertFalse(f.closed)
+ with open(TESTFN, 'rb') as f:
+ self.assertEqual(f.read(), b'''\xc3\xb8''')
+
+ with open(TESTFN, 'wb') as f:
+ tree.write(f, encoding='ISO-8859-1')
+ self.assertFalse(f.closed)
+ with open(TESTFN, 'rb') as f:
+ self.assertEqual(f.read(),
+ b'''\n'''
+ b'''\xf8''')
def test_write_to_binary_file_with_bom(self):
self.addCleanup(support.unlink, TESTFN)
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
# test BOM writing to buffered file
with open(TESTFN, 'wb') as f:
tree.write(f, encoding='utf-16')
@@ -3687,7 +3750,7 @@ def test_write_to_binary_file_with_bom(self):
with open(TESTFN, 'rb') as f:
self.assertEqual(f.read(),
'''\n'''
- ''''''.encode("utf-16"))
+ '''\xf8'''.encode("utf-16"))
# test BOM writing to non-buffered file
with open(TESTFN, 'wb', buffering=0) as f:
tree.write(f, encoding='utf-16')
@@ -3695,7 +3758,7 @@ def test_write_to_binary_file_with_bom(self):
with open(TESTFN, 'rb') as f:
self.assertEqual(f.read(),
'''\n'''
- ''''''.encode("utf-16"))
+ '''\xf8'''.encode("utf-16"))
def test_read_from_stringio(self):
tree = ET.ElementTree()
@@ -3704,10 +3767,10 @@ def test_read_from_stringio(self):
self.assertEqual(tree.getroot().tag, 'site')
def test_write_to_stringio(self):
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
stream = io.StringIO()
tree.write(stream, encoding='unicode')
- self.assertEqual(stream.getvalue(), '''''')
+ self.assertEqual(stream.getvalue(), '''\xf8''')
def test_read_from_bytesio(self):
tree = ET.ElementTree()
@@ -3716,10 +3779,10 @@ def test_read_from_bytesio(self):
self.assertEqual(tree.getroot().tag, 'site')
def test_write_to_bytesio(self):
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
raw = io.BytesIO()
tree.write(raw)
- self.assertEqual(raw.getvalue(), b'''''')
+ self.assertEqual(raw.getvalue(), b'''ø''')
class dummy:
pass
@@ -3733,12 +3796,12 @@ def test_read_from_user_text_reader(self):
self.assertEqual(tree.getroot().tag, 'site')
def test_write_to_user_text_writer(self):
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
stream = io.StringIO()
writer = self.dummy()
writer.write = stream.write
tree.write(writer, encoding='unicode')
- self.assertEqual(stream.getvalue(), '''''')
+ self.assertEqual(stream.getvalue(), '''\xf8''')
def test_read_from_user_binary_reader(self):
raw = io.BytesIO(b'''''')
@@ -3750,12 +3813,12 @@ def test_read_from_user_binary_reader(self):
tree = ET.ElementTree()
def test_write_to_user_binary_writer(self):
- tree = ET.ElementTree(ET.XML(''''''))
+ tree = ET.ElementTree(ET.XML('''\xf8'''))
raw = io.BytesIO()
writer = self.dummy()
writer.write = raw.write
tree.write(writer)
- self.assertEqual(raw.getvalue(), b'''''')
+ self.assertEqual(raw.getvalue(), b'''ø''')
def test_write_to_user_binary_writer_with_bom(self):
tree = ET.ElementTree(ET.XML(''''''))