summaryrefslogtreecommitdiffstats
path: root/Modules/_elementtree.c
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2010-10-29 10:38:18 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2010-10-29 10:38:18 (GMT)
commite033e06db077d5abcb4bc3729d03f8a4a09b2486 (patch)
tree04445ffa669d4d0df240d680249c7d7a7f661bd4 /Modules/_elementtree.c
parent9cbdd75ec5deda8f55edd7caab42dff65d009da2 (diff)
downloadcpython-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 'Modules/_elementtree.c')
-rw-r--r--Modules/_elementtree.c32
1 files changed, 21 insertions, 11 deletions
diff --git a/Modules/_elementtree.c b/Modules/_elementtree.c
index 876ab3a..851eed2 100644
--- a/Modules/_elementtree.c
+++ b/Modules/_elementtree.c
@@ -2946,19 +2946,25 @@ PyInit__elementtree(void)
"class ElementTree(ET.ElementTree):\n" /* public */
" def parse(self, source, parser=None):\n"
+ " close_source = False\n"
" if not hasattr(source, 'read'):\n"
" source = open(source, 'rb')\n"
- " if parser is not None:\n"
- " while 1:\n"
- " data = source.read(65536)\n"
- " if not data:\n"
- " break\n"
- " parser.feed(data)\n"
- " self._root = parser.close()\n"
- " else:\n"
- " parser = cElementTree.XMLParser()\n"
- " self._root = parser._parse(source)\n"
- " return self._root\n"
+ " close_source = True\n"
+ " try:\n"
+ " if parser is not None:\n"
+ " while 1:\n"
+ " data = source.read(65536)\n"
+ " if not data:\n"
+ " break\n"
+ " parser.feed(data)\n"
+ " self._root = parser.close()\n"
+ " else:\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"
"def iter(node, tag=None):\n" /* helper */
@@ -2988,8 +2994,10 @@ PyInit__elementtree(void)
"class iterparse:\n"
" root = None\n"
" def __init__(self, file, events=None):\n"
+ " self._close_file = False\n"
" if not hasattr(file, 'read'):\n"
" file = open(file, 'rb')\n"
+ " self._close_file = True\n"
" self._file = file\n"
" self._events = []\n"
" self._index = 0\n"
@@ -3004,6 +3012,8 @@ PyInit__elementtree(void)
" 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"