Skip to content

Commit 2ed04f0

Browse files
committed
Co-authored-by: Copilot <copilot@github.com>
1 parent 95559d2 commit 2ed04f0

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

Lib/test/test_xml_etree.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,9 +4312,9 @@ def test_write_to_filename_with_encoding(self):
43124312

43134313
tree.write(TESTFN, encoding='ISO-8859-1')
43144314
with open(TESTFN, 'rb') as f:
4315-
self.assertEqual(f.read(), convlinesep(
4315+
self.assertEqual(f.read(),
43164316
b'''<?xml version='1.0' encoding='ISO-8859-1'?>\n'''
4317-
b'''<site>\xf8</site>'''))
4317+
b'''<site>\xf8</site>''')
43184318

43194319
def test_write_to_filename_as_unicode(self):
43204320
self.addCleanup(os_helper.unlink, TESTFN)
@@ -4357,6 +4357,25 @@ def test_write_to_binary_file(self):
43574357
with open(TESTFN, 'rb') as f:
43584358
self.assertEqual(f.read(), b'''<site>&#248;</site>''')
43594359

4360+
def test_write_with_and_without_context_manager_same_output(self):
4361+
self.addCleanup(os_helper.unlink, TESTFN)
4362+
testfn2 = TESTFN + '2'
4363+
self.addCleanup(os_helper.unlink, testfn2)
4364+
4365+
tree = ET.ElementTree(ET.XML('''<site>\x0a</site>'''))
4366+
tree.write(TESTFN, encoding='utf-8')
4367+
with open(TESTFN, 'rb') as f:
4368+
output_without_context_manager = f.read()
4369+
4370+
with open(testfn2, 'wb') as f:
4371+
tree.write(f, encoding='utf-8')
4372+
self.assertFalse(f.closed)
4373+
with open(testfn2, 'rb') as f:
4374+
output_with_context_manager = f.read()
4375+
4376+
self.assertEqual(output_without_context_manager,
4377+
output_with_context_manager)
4378+
43604379
def test_write_to_binary_file_with_encoding(self):
43614380
self.addCleanup(os_helper.unlink, TESTFN)
43624381
tree = ET.ElementTree(ET.XML('''<site>\xf8</site>'''))

Lib/xml/etree/ElementTree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ def _get_writer(file_or_filename, encoding):
756756
if encoding.lower() == "unicode":
757757
encoding="utf-8"
758758
with open(file_or_filename, "w", encoding=encoding,
759-
errors="xmlcharrefreplace") as file:
759+
errors="xmlcharrefreplace", newline="\n") as file:
760760
yield file.write, encoding
761761
else:
762762
# file_or_filename is a file-like object

0 commit comments

Comments
 (0)