diff options
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/test/test_xml_etree.py | 1 | ||||
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 38 |
2 files changed, 24 insertions, 15 deletions
diff --git a/Lib/test/test_xml_etree.py b/Lib/test/test_xml_etree.py index 22fafa9..b7a996c 100644 --- a/Lib/test/test_xml_etree.py +++ b/Lib/test/test_xml_etree.py @@ -754,6 +754,7 @@ def iterparse(): ... print(action, elem.tag) ... except ET.ParseError as v: ... print(v) + end document junk after document element: line 1, column 12 """ diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index ba33879..f94c48c 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -1250,6 +1250,7 @@ class _IterParseIterator: self._close_file = close_source self._events = [] self._index = 0 + self._error = None self.root = self._root = None self._parser = parser # wire up the parser for event reporting @@ -1291,24 +1292,31 @@ class _IterParseIterator: while 1: try: item = self._events[self._index] + self._index += 1 + return item except IndexError: - if self._parser is None: - self.root = self._root - if self._close_file: - self._file.close() - raise StopIteration - # load event buffer - del self._events[:] - self._index = 0 - data = self._file.read(16384) - if data: + pass + if self._error: + e = self._error + self._error = None + raise e + if self._parser is None: + self.root = self._root + if self._close_file: + self._file.close() + raise StopIteration + # load event buffer + del self._events[:] + self._index = 0 + data = self._file.read(16384) + if data: + try: self._parser.feed(data) - else: - self._root = self._parser.close() - self._parser = None + except SyntaxError as exc: + self._error = exc else: - self._index = self._index + 1 - return item + self._root = self._parser.close() + self._parser = None def __iter__(self): return self |