diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-29 10:38:18 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2010-10-29 10:38:18 (GMT) |
commit | e033e06db077d5abcb4bc3729d03f8a4a09b2486 (patch) | |
tree | 04445ffa669d4d0df240d680249c7d7a7f661bd4 /Lib/xml/etree | |
parent | 9cbdd75ec5deda8f55edd7caab42dff65d009da2 (diff) | |
download | cpython-e033e06db077d5abcb4bc3729d03f8a4a09b2486.zip cpython-e033e06db077d5abcb4bc3729d03f8a4a09b2486.tar.gz cpython-e033e06db077d5abcb4bc3729d03f8a4a09b2486.tar.bz2 |
Issue #10093: ResourceWarnings are now issued when files and sockets are
deallocated without explicit closing. These warnings are silenced by
default, except in pydebug mode.
Diffstat (limited to 'Lib/xml/etree')
-rw-r--r-- | Lib/xml/etree/ElementTree.py | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/Lib/xml/etree/ElementTree.py b/Lib/xml/etree/ElementTree.py index ecc8ea7..9f5717e 100644 --- a/Lib/xml/etree/ElementTree.py +++ b/Lib/xml/etree/ElementTree.py @@ -662,17 +662,23 @@ class ElementTree: # @exception ParseError If the parser fails to parse the document. def parse(self, source, parser=None): + close_source = False if not hasattr(source, "read"): source = open(source, "rb") - if not parser: - parser = XMLParser(target=TreeBuilder()) - while 1: - data = source.read(65536) - if not data: - break - parser.feed(data) - self._root = parser.close() - return self._root + close_source = True + try: + if not parser: + parser = XMLParser(target=TreeBuilder()) + while 1: + data = source.read(65536) + if not data: + break + parser.feed(data) + self._root = parser.close() + return self._root + finally: + if close_source: + source.close() ## # Creates a tree iterator for the root element. The iterator loops @@ -1226,16 +1232,19 @@ def parse(source, parser=None): # @return A (event, elem) iterator. def iterparse(source, events=None, parser=None): + close_source = False if not hasattr(source, "read"): source = open(source, "rb") + close_source = True if not parser: parser = XMLParser(target=TreeBuilder()) - return _IterParseIterator(source, events, parser) + return _IterParseIterator(source, events, parser, close_source) class _IterParseIterator: - def __init__(self, source, events, parser): + def __init__(self, source, events, parser, close_source=False): self._file = source + self._close_file = close_source self._events = [] self._index = 0 self.root = self._root = None @@ -1282,6 +1291,8 @@ class _IterParseIterator: 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[:] |