diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-23 13:45:12 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-11-23 13:45:12 (GMT) |
commit | 6f988b5990406022f00d9c32a465291ba359ecd9 (patch) | |
tree | 10e42c8eccd4ea85b8c51380ff14201840b6903c /Lib/test/test_xml_etree.py | |
parent | 4409c6cfae2faa9217902ea720c1cf2d6f2b4fd2 (diff) | |
parent | e3d4ec4766629c3cbcc1afb4e5c020894d5e2258 (diff) | |
download | cpython-6f988b5990406022f00d9c32a465291ba359ecd9.zip cpython-6f988b5990406022f00d9c32a465291ba359ecd9.tar.gz cpython-6f988b5990406022f00d9c32a465291ba359ecd9.tar.bz2 |
Issue #25688: Fixed file leak in ElementTree.iterparse() raising an error.
Diffstat (limited to 'Lib/test/test_xml_etree.py')
-rw-r--r-- | Lib/test/test_xml_etree.py | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 0a2c7bc..57d8e4d 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -561,11 +561,21 @@ class ElementTreeTest(unittest.TestCase): self.assertEqual(res, ['start-ns', 'end-ns']) events = ("start", "end", "bogus") - with self.assertRaises(ValueError) as cm: - with open(SIMPLE_XMLFILE, "rb") as f: + with open(SIMPLE_XMLFILE, "rb") as f: + with self.assertRaises(ValueError) as cm: iterparse(f, events) + self.assertFalse(f.closed) self.assertEqual(str(cm.exception), "unknown event 'bogus'") + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings("always", category=ResourceWarning) + with self.assertRaises(ValueError) as cm: + iterparse(SIMPLE_XMLFILE, events) + self.assertEqual(str(cm.exception), "unknown event 'bogus'") + del cm + support.gc_collect() + self.assertEqual(w, []) + source = io.BytesIO( b"<?xml version='1.0' encoding='iso-8859-1'?>\n" b"<body xmlns='http://éffbot.org/ns'\n" @@ -586,6 +596,21 @@ class ElementTreeTest(unittest.TestCase): self.assertEqual(str(cm.exception), 'junk after document element: line 1, column 12') + with open(TESTFN, "wb") as f: + f.write(b"<document />junk") + it = iterparse(TESTFN) + action, elem = next(it) + self.assertEqual((action, elem.tag), ('end', 'document')) + with warnings.catch_warnings(record=True) as w: + warnings.filterwarnings("always", category=ResourceWarning) + with self.assertRaises(ET.ParseError) as cm: + next(it) + self.assertEqual(str(cm.exception), + 'junk after document element: line 1, column 12') + del cm, it + support.gc_collect() + self.assertEqual(w, []) + def test_writefile(self): elem = ET.Element("tag") elem.text = "text" |