Closes #7334: close source files on ElementTree.parse and iterparse (partial backport of issue #10093 from 3.2).

This commit is contained in:
Florent Xicluna 2011-10-29 03:38:56 +02:00
parent e96102b45b
commit 67d5d0ed44
3 changed files with 45 additions and 22 deletions

View File

@ -642,17 +642,23 @@ class ElementTree(object):
# @exception ParseError If the parser fails to parse the document. # @exception ParseError If the parser fails to parse the document.
def parse(self, source, parser=None): def parse(self, source, parser=None):
close_source = False
if not hasattr(source, "read"): if not hasattr(source, "read"):
source = open(source, "rb") source = open(source, "rb")
if not parser: close_source = True
parser = XMLParser(target=TreeBuilder()) try:
while 1: if not parser:
data = source.read(65536) parser = XMLParser(target=TreeBuilder())
if not data: while 1:
break data = source.read(65536)
parser.feed(data) if not data:
self._root = parser.close() break
return self._root 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 # Creates a tree iterator for the root element. The iterator loops
@ -1189,16 +1195,19 @@ def parse(source, parser=None):
# @return A (event, elem) iterator. # @return A (event, elem) iterator.
def iterparse(source, events=None, parser=None): def iterparse(source, events=None, parser=None):
close_source = False
if not hasattr(source, "read"): if not hasattr(source, "read"):
source = open(source, "rb") source = open(source, "rb")
close_source = True
if not parser: if not parser:
parser = XMLParser(target=TreeBuilder()) parser = XMLParser(target=TreeBuilder())
return _IterParseIterator(source, events, parser) return _IterParseIterator(source, events, parser, close_source)
class _IterParseIterator(object): class _IterParseIterator(object):
def __init__(self, source, events, parser): def __init__(self, source, events, parser, close_source=False):
self._file = source self._file = source
self._close_file = close_source
self._events = [] self._events = []
self._index = 0 self._index = 0
self.root = self._root = None self.root = self._root = None
@ -1249,6 +1258,8 @@ class _IterParseIterator(object):
except IndexError: except IndexError:
if self._parser is None: if self._parser is None:
self.root = self._root self.root = self._root
if self._close_file:
self._file.close()
raise StopIteration raise StopIteration
# load event buffer # load event buffer
del self._events[:] del self._events[:]

View File

@ -71,6 +71,8 @@ Core and Builtins
Library Library
------- -------
- Issue #7334: close source files on ElementTree.parse and iterparse.
- Issue #13232: logging: Improved logging of exceptions in the presence of - Issue #13232: logging: Improved logging of exceptions in the presence of
multiple encodings. multiple encodings.

View File

@ -2915,19 +2915,25 @@ init_elementtree(void)
"class ElementTree(ET.ElementTree):\n" /* public */ "class ElementTree(ET.ElementTree):\n" /* public */
" def parse(self, source, parser=None):\n" " def parse(self, source, parser=None):\n"
" close_source = False\n"
" if not hasattr(source, 'read'):\n" " if not hasattr(source, 'read'):\n"
" source = open(source, 'rb')\n" " source = open(source, 'rb')\n"
" if parser is not None:\n" " close_source = False\n"
" while 1:\n" " try:\n"
" data = source.read(65536)\n" " if parser is not None:\n"
" if not data:\n" " while 1:\n"
" break\n" " data = source.read(65536)\n"
" parser.feed(data)\n" " if not data:\n"
" self._root = parser.close()\n" " break\n"
" else:\n" " parser.feed(data)\n"
" parser = cElementTree.XMLParser()\n" " self._root = parser.close()\n"
" self._root = parser._parse(source)\n" " else:\n"
" return self._root\n" " parser = cElementTree.XMLParser()\n"
" self._root = parser._parse(source)\n"
" return self._root\n"
" finally:\n"
" if close_source:\n"
" source.close()\n"
"cElementTree.ElementTree = ElementTree\n" "cElementTree.ElementTree = ElementTree\n"
"def iter(node, tag=None):\n" /* helper */ "def iter(node, tag=None):\n" /* helper */
@ -2957,8 +2963,10 @@ init_elementtree(void)
"class iterparse(object):\n" "class iterparse(object):\n"
" root = None\n" " root = None\n"
" def __init__(self, file, events=None):\n" " def __init__(self, file, events=None):\n"
" self._close_file = False\n"
" if not hasattr(file, 'read'):\n" " if not hasattr(file, 'read'):\n"
" file = open(file, 'rb')\n" " file = open(file, 'rb')\n"
" self._close_file = True\n"
" self._file = file\n" " self._file = file\n"
" self._events = []\n" " self._events = []\n"
" self._index = 0\n" " self._index = 0\n"
@ -2973,6 +2981,8 @@ init_elementtree(void)
" except IndexError:\n" " except IndexError:\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"
" self._file.close()\n"
" raise StopIteration\n" " raise StopIteration\n"
" # load event buffer\n" " # load event buffer\n"
" del self._events[:]\n" " del self._events[:]\n"