lxml.etree.parse method don't close the file
Affects | Status | Importance | Assigned to | Milestone | |
---|---|---|---|---|---|
lxml |
Fix Released
|
Medium
|
scoder |
Bug Description
in the method parse it open the file but don't close: (looking at link http://
def parse(self, source, parser=None):
if not hasattr(source, "read"):
source = open(source, "rb")
if not parser:
parser = XMLTreeBuilder()
while 1:
data = source.read(32768)
if not data:
break
self._root = parser.close()
return self._root
and it should be:
def parse(self, source, parser=None):
if not hasattr(source, "read"):
source = open(source, "rb")
if not parser:
parser = XMLTreeBuilder()
while 1:
data = source.read(32768)
if not data:
break
source.close() # add this line
self._root = parser.close()
return self._root
I got this bug because my program is reading the xml file into a timer, and after some minutes it got the 'too many files open' error into the console
of course, I did a working arround opening the file out of lxml and passing the file handler to the parse method, and doing a file_handler.
In [3]: ## -- PLEASE PROVIDE THE FOLLOWING INFORMATION: --
In [4]: import sys
In [5]: from lxml import etree
In [6]:
In [7]: print("%-20s: %s" % ('Python', sys.version_info))
Python : (2, 6, 5, 'final', 0)
In [8]: print("%-20s: %s" % ('lxml.etree', etree.LXML_
lxml.etree : (2, 2, 4, 0)
In [9]: print("%-20s: %s" % ('libxml used', etree.LIBXML_
libxml used : (2, 7, 6)
In [10]: print("%-20s: %s" % ('libxml compiled', etree.LIBXML_
libxml compiled : (2, 7, 6)
In [11]: print("%-20s: %s" % ('libxslt used', etree.LIBXSLT_
libxslt used : (1, 1, 26)
In [12]: print("%-20s: %s" % ('libxslt compiled', etree.LIBXSLT_
libxslt compiled : (1, 1, 26)
In [13]: ## -----
Changed in lxml: | |
assignee: | nobody → Stefan Behnel (scoder) |
importance: | Undecided → Medium |
milestone: | none → 2.3 |
status: | New → Fix Released |
Rodrigo F. S., 09.02.2011 13:39: codespeak. net/lxml/ api/xml. etree.ElementTr ee-pysrc. html#)
> in the method parse it open the file but don't close: (looking at link
> http://
Note that this source file is part of ElementTree, not lxml.
> I got this bug because my program is reading the xml file into a timer, close() after the parse..
> and after some minutes it got the 'too many files open' error into the
> console
>
> of course, I did a working arround opening the file out of lxml and
> passing the file handler to the parse method, and doing a
> file_handler.
> lxml.etree : (2, 2, 4, 0)
If you are referring to lxml.etree, this was likely fixed in lxml 2.3.
Please upgrade.
Stefan