summaryrefslogtreecommitdiffstats
path: root/Lib/xml/etree
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-11-23 13:45:12 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-11-23 13:45:12 (GMT)
commit6f988b5990406022f00d9c32a465291ba359ecd9 (patch)
tree10e42c8eccd4ea85b8c51380ff14201840b6903c /Lib/xml/etree
parent4409c6cfae2faa9217902ea720c1cf2d6f2b4fd2 (diff)
parente3d4ec4766629c3cbcc1afb4e5c020894d5e2258 (diff)
downloadcpython-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/xml/etree')
-rw-r--r--Lib/xml/etree/ElementTree.py41
1 files changed, 26 insertions, 15 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py
index bb32a8f..62b5d3a 100644
--- a/Lib/xml/etree/ElementTree.py
+++ b/Lib/xml/etree/ElementTree.py
@@ -1202,7 +1202,12 @@ def iterparse(source, events=None, parser=None):
if not hasattr(source, "read"):
source = open(source, "rb")
close_source = True
- return _IterParseIterator(source, events, parser, close_source)
+ try:
+ return _IterParseIterator(source, events, parser, close_source)
+ except:
+ if close_source:
+ source.close()
+ raise
class XMLPullParser:
@@ -1285,20 +1290,26 @@ class _IterParseIterator:
self.root = self._root = None
def __next__(self):
- while 1:
- for event in self._parser.read_events():
- return event
- if self._parser._parser is None:
- self.root = self._root
- if self._close_file:
- self._file.close()
- raise StopIteration
- # load event buffer
- data = self._file.read(16 * 1024)
- if data:
- self._parser.feed(data)
- else:
- self._root = self._parser._close_and_return_root()
+ try:
+ while 1:
+ for event in self._parser.read_events():
+ return event
+ if self._parser._parser is None:
+ break
+ # load event buffer
+ data = self._file.read(16 * 1024)
+ if data:
+ self._parser.feed(data)
+ else:
+ self._root = self._parser._close_and_return_root()
+ self.root = self._root
+ except:
+ if self._close_file:
+ self._file.close()
+ raise
+ if self._close_file:
+ self._file.close()
+ raise StopIteration
def __iter__(self):
return self