Skip to content

bpo-33761: Fix a file leak in test_iterparse in test_xml_etree. #7358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Lib/test/test_xml_etree.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ def test_iterparse(self):
self.assertEqual(str(cm.exception),
'junk after document element: line 1, column 12')

self.addCleanup(support.unlink, TESTFN)
with open(TESTFN, "wb") as f:
f.write(b"<document />junk")
it = iterparse(TESTFN)
Expand Down Expand Up @@ -2849,9 +2850,6 @@ def test_setslice_negative_steps(self):


class IOTest(unittest.TestCase):
def tearDown(self):
support.unlink(TESTFN)

def test_encoding(self):
# Test encoding issues.
elem = ET.Element("tag")
Expand Down Expand Up @@ -2922,12 +2920,14 @@ def test_encoding(self):
"<tag key=\"åöö&lt;&gt;\" />" % enc).encode(enc))

def test_write_to_filename(self):
self.addCleanup(support.unlink, TESTFN)
tree = ET.ElementTree(ET.XML('''<site />'''))
tree.write(TESTFN)
with open(TESTFN, 'rb') as f:
self.assertEqual(f.read(), b'''<site />''')

def test_write_to_text_file(self):
self.addCleanup(support.unlink, TESTFN)
tree = ET.ElementTree(ET.XML('''<site />'''))
with open(TESTFN, 'w', encoding='utf-8') as f:
tree.write(f, encoding='unicode')
Expand All @@ -2936,6 +2936,7 @@ def test_write_to_text_file(self):
self.assertEqual(f.read(), b'''<site />''')

def test_write_to_binary_file(self):
self.addCleanup(support.unlink, TESTFN)
tree = ET.ElementTree(ET.XML('''<site />'''))
with open(TESTFN, 'wb') as f:
tree.write(f)
Expand All @@ -2944,6 +2945,7 @@ def test_write_to_binary_file(self):
self.assertEqual(f.read(), b'''<site />''')

def test_write_to_binary_file_with_bom(self):
self.addCleanup(support.unlink, TESTFN)
tree = ET.ElementTree(ET.XML('''<site />'''))
# test BOM writing to buffered file
with open(TESTFN, 'wb') as f:
Expand Down