| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -561,11 +561,21 @@ def test_iterparse(self): | |||
| 561 | 561 | self.assertEqual(res, ['start-ns', 'end-ns']) | |
| 562 | 562 | ||
| 563 | 563 | events = ("start", "end", "bogus") | |
| 564 | - with self.assertRaises(ValueError) as cm: | ||
| 565 | - with open(SIMPLE_XMLFILE, "rb") as f: | ||
| 564 | + with open(SIMPLE_XMLFILE, "rb") as f: | ||
| 565 | + with self.assertRaises(ValueError) as cm: | ||
| 566 | 566 | iterparse(f, events) | |
| 567 | + self.assertFalse(f.closed) | ||
| 567 | 568 | self.assertEqual(str(cm.exception), "unknown event 'bogus'") | |
| 568 | 569 | ||
| 570 | + with warnings.catch_warnings(record=True) as w: | ||
| 571 | + warnings.filterwarnings("always", category=ResourceWarning) | ||
| 572 | + with self.assertRaises(ValueError) as cm: | ||
| 573 | + iterparse(SIMPLE_XMLFILE, events) | ||
| 574 | + self.assertEqual(str(cm.exception), "unknown event 'bogus'") | ||
| 575 | + del cm | ||
| 576 | + support.gc_collect() | ||
| 577 | + self.assertEqual(w, []) | ||
| 578 | + | ||
| 569 | 579 | source = io.BytesIO( | |
| 570 | 580 | b"<?xml version='1.0' encoding='iso-8859-1'?>\n" | |
| 571 | 581 | b"<body xmlns='http://éffbot.org/ns'\n" | |
@@ -586,6 +596,21 @@ def test_iterparse(self): | |||
| 586 | 596 | self.assertEqual(str(cm.exception), | |
| 587 | 597 | 'junk after document element: line 1, column 12') | |
| 588 | 598 | ||
| 599 | + with open(TESTFN, "wb") as f: | ||
| 600 | + f.write(b"<document />junk") | ||
| 601 | + it = iterparse(TESTFN) | ||
| 602 | + action, elem = next(it) | ||
| 603 | + self.assertEqual((action, elem.tag), ('end', 'document')) | ||
| 604 | + with warnings.catch_warnings(record=True) as w: | ||
| 605 | + warnings.filterwarnings("always", category=ResourceWarning) | ||
| 606 | + with self.assertRaises(ET.ParseError) as cm: | ||
| 607 | + next(it) | ||
| 608 | + self.assertEqual(str(cm.exception), | ||
| 609 | + 'junk after document element: line 1, column 12') | ||
| 610 | + del cm, it | ||
| 611 | + support.gc_collect() | ||
| 612 | + self.assertEqual(w, []) | ||
| 613 | + | ||
| 589 | 614 | def test_writefile(self): | |
| 590 | 615 | elem = ET.Element("tag") | |
| 591 | 616 | elem.text = "text" | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1202,7 +1202,12 @@ def iterparse(source, events=None, parser=None): | |||
| 1202 | 1202 | if not hasattr(source, "read"): | |
| 1203 | 1203 | source = open(source, "rb") | |
| 1204 | 1204 | close_source = True | |
| 1205 | - return _IterParseIterator(source, events, parser, close_source) | ||
| 1205 | + try: | ||
| 1206 | + return _IterParseIterator(source, events, parser, close_source) | ||
| 1207 | + except: | ||
| 1208 | + if close_source: | ||
| 1209 | + source.close() | ||
| 1210 | + raise | ||
| 1206 | 1211 | ||
| 1207 | 1212 | ||
| 1208 | 1213 | class XMLPullParser: | |
@@ -1285,20 +1290,26 @@ def __init__(self, source, events, parser, close_source=False): | |||
| 1285 | 1290 | self.root = self._root = None | |
| 1286 | 1291 | ||
| 1287 | 1292 | def __next__(self): | |
| 1288 | - while 1: | ||
| 1289 | - for event in self._parser.read_events(): | ||
| 1290 | - return event | ||
| 1291 | - if self._parser._parser is None: | ||
| 1292 | - self.root = self._root | ||
| 1293 | - if self._close_file: | ||
| 1294 | - self._file.close() | ||
| 1295 | - raise StopIteration | ||
| 1296 | - # load event buffer | ||
| 1297 | - data = self._file.read(16 * 1024) | ||
| 1298 | - if data: | ||
| 1299 | - self._parser.feed(data) | ||
| 1300 | - else: | ||
| 1301 | - self._root = self._parser._close_and_return_root() | ||
| 1293 | + try: | ||
| 1294 | + while 1: | ||
| 1295 | + for event in self._parser.read_events(): | ||
| 1296 | + return event | ||
| 1297 | + if self._parser._parser is None: | ||
| 1298 | + break | ||
| 1299 | + # load event buffer | ||
| 1300 | + data = self._file.read(16 * 1024) | ||
| 1301 | + if data: | ||
| 1302 | + self._parser.feed(data) | ||
| 1303 | + else: | ||
| 1304 | + self._root = self._parser._close_and_return_root() | ||
| 1305 | + self.root = self._root | ||
| 1306 | + except: | ||
| 1307 | + if self._close_file: | ||
| 1308 | + self._file.close() | ||
| 1309 | + raise | ||
| 1310 | + if self._close_file: | ||
| 1311 | + self._file.close() | ||
| 1312 | + raise StopIteration | ||
| 1302 | 1313 | ||
| 1303 | 1314 | def __iter__(self): | |
| 1304 | 1315 | return self | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -13,6 +13,8 @@ Core and Builtins | |||
| 13 | 13 | Library | |
| 14 | 14 | ------- | |
| 15 | 15 | ||
| 16 | + - Issue #25688: Fixed file leak in ElementTree.iterparse() raising an error. | ||
| 17 | + | ||
| 16 | 18 | - Issue #23914: Fixed SystemError raised by unpickler on broken pickle data. | |
| 17 | 19 | ||
| 18 | 20 | - Issue #25691: Fixed crash on deleting ElementTree.Element attributes. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments