Closes #2892: preserve iterparse events in case of SyntaxError.
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 3061d8e..7887721 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -3000,6 +3000,7 @@
" self._file = file\n"
" self._events = []\n"
" self._index = 0\n"
+ " self._error = None\n"
" self.root = self._root = None\n"
" b = cElementTree.TreeBuilder()\n"
" self._parser = cElementTree.XMLParser(b)\n"
@@ -3008,24 +3009,31 @@
" while 1:\n"
" try:\n"
" item = self._events[self._index]\n"
- " except IndexError:\n"
- " if self._parser is None:\n"
- " self.root = self._root\n"
- " if self._close_file:\n"
- " self._file.close()\n"
- " raise StopIteration\n"
- " # load event buffer\n"
- " del self._events[:]\n"
- " self._index = 0\n"
- " data = self._file.read(16384)\n"
- " if data:\n"
- " self._parser.feed(data)\n"
- " else:\n"
- " self._root = self._parser.close()\n"
- " self._parser = None\n"
- " else:\n"
- " self._index = self._index + 1\n"
+ " self._index += 1\n"
" return item\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"
+ " self.root = self._root\n"
+ " if self._close_file:\n"
+ " self._file.close()\n"
+ " raise StopIteration\n"
+ " # load event buffer\n"
+ " del self._events[:]\n"
+ " self._index = 0\n"
+ " data = self._file.read(16384)\n"
+ " if data:\n"
+ " try:\n"
+ " self._parser.feed(data)\n"
+ " except SyntaxError as exc:\n"
+ " self._error = exc\n"
+ " else:\n"
+ " self._root = self._parser.close()\n"
+ " self._parser = None\n"
" def __iter__(self):\n"
" return self\n"
"cElementTree.iterparse = iterparse\n"