Commit 91d5193b authored by Florent Xicluna's avatar Florent Xicluna

Closes #2892: preserve iterparse events in case of SyntaxError.

parent c1e73c30
...@@ -754,6 +754,7 @@ def iterparse(): ...@@ -754,6 +754,7 @@ def iterparse():
... print(action, elem.tag) ... print(action, elem.tag)
... except ET.ParseError as v: ... except ET.ParseError as v:
... print(v) ... print(v)
end document
junk after document element: line 1, column 12 junk after document element: line 1, column 12
""" """
......
...@@ -1250,6 +1250,7 @@ class _IterParseIterator: ...@@ -1250,6 +1250,7 @@ class _IterParseIterator:
self._close_file = close_source self._close_file = close_source
self._events = [] self._events = []
self._index = 0 self._index = 0
self._error = None
self.root = self._root = None self.root = self._root = None
self._parser = parser self._parser = parser
# wire up the parser for event reporting # wire up the parser for event reporting
...@@ -1291,7 +1292,14 @@ class _IterParseIterator: ...@@ -1291,7 +1292,14 @@ class _IterParseIterator:
while 1: while 1:
try: try:
item = self._events[self._index] item = self._events[self._index]
self._index += 1
return item
except IndexError: except IndexError:
pass
if self._error:
e = self._error
self._error = None
raise e
if self._parser is None: if self._parser is None:
self.root = self._root self.root = self._root
if self._close_file: if self._close_file:
...@@ -1302,13 +1310,13 @@ class _IterParseIterator: ...@@ -1302,13 +1310,13 @@ class _IterParseIterator:
self._index = 0 self._index = 0
data = self._file.read(16384) data = self._file.read(16384)
if data: if data:
try:
self._parser.feed(data) self._parser.feed(data)
except SyntaxError as exc:
self._error = exc
else: else:
self._root = self._parser.close() self._root = self._parser.close()
self._parser = None self._parser = None
else:
self._index = self._index + 1
return item
def __iter__(self): def __iter__(self):
return self return self
......
...@@ -66,6 +66,8 @@ Core and Builtins ...@@ -66,6 +66,8 @@ Core and Builtins
Library Library
------- -------
- Issue #2892: preserve iterparse events in case of SyntaxError.
- Issue #670664: Fix HTMLParser to correctly handle the content of - Issue #670664: Fix HTMLParser to correctly handle the content of
``<script>...</script>`` and ``<style>...</style>``. ``<script>...</script>`` and ``<style>...</style>``.
......
...@@ -3000,6 +3000,7 @@ PyInit__elementtree(void) ...@@ -3000,6 +3000,7 @@ PyInit__elementtree(void)
" self._file = file\n" " self._file = file\n"
" self._events = []\n" " self._events = []\n"
" self._index = 0\n" " self._index = 0\n"
" self._error = None\n"
" self.root = self._root = None\n" " self.root = self._root = None\n"
" b = cElementTree.TreeBuilder()\n" " b = cElementTree.TreeBuilder()\n"
" self._parser = cElementTree.XMLParser(b)\n" " self._parser = cElementTree.XMLParser(b)\n"
...@@ -3008,7 +3009,14 @@ PyInit__elementtree(void) ...@@ -3008,7 +3009,14 @@ PyInit__elementtree(void)
" while 1:\n" " while 1:\n"
" try:\n" " try:\n"
" item = self._events[self._index]\n" " item = self._events[self._index]\n"
" self._index += 1\n"
" return item\n"
" except IndexError:\n" " except IndexError:\n"
" pass\n"
" if self._error:\n"
" e = self._error\n"
" self._error = None\n"
" raise e\n"
" if self._parser is None:\n" " if self._parser is None:\n"
" self.root = self._root\n" " self.root = self._root\n"
" if self._close_file:\n" " if self._close_file:\n"
...@@ -3019,13 +3027,13 @@ PyInit__elementtree(void) ...@@ -3019,13 +3027,13 @@ PyInit__elementtree(void)
" self._index = 0\n" " self._index = 0\n"
" data = self._file.read(16384)\n" " data = self._file.read(16384)\n"
" if data:\n" " if data:\n"
" try:\n"
" self._parser.feed(data)\n" " self._parser.feed(data)\n"
" except SyntaxError as exc:\n"
" self._error = exc\n"
" else:\n" " else:\n"
" self._root = self._parser.close()\n" " self._root = self._parser.close()\n"
" self._parser = None\n" " self._parser = None\n"
" else:\n"
" self._index = self._index + 1\n"
" return item\n"
" def __iter__(self):\n" " def __iter__(self):\n"
" return self\n" " return self\n"
"cElementTree.iterparse = iterparse\n" "cElementTree.iterparse = iterparse\n"
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment