lxml.etree.parse method don't close the file

Bug #715737 reported by Rodrigo F. S.
6
This bug affects 1 person
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://codespeak.net/lxml/api/xml.etree.ElementTree-pysrc.html#)

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
            parser.feed(data)
    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
            parser.feed(data)
    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.close() after the parse..

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_VERSION))
lxml.etree : (2, 2, 4, 0)
In [9]: print("%-20s: %s" % ('libxml used', etree.LIBXML_VERSION))
libxml used : (2, 7, 6)
In [10]: print("%-20s: %s" % ('libxml compiled', etree.LIBXML_COMPILED_VERSION))
libxml compiled : (2, 7, 6)
In [11]: print("%-20s: %s" % ('libxslt used', etree.LIBXSLT_VERSION))
libxslt used : (1, 1, 26)
In [12]: print("%-20s: %s" % ('libxslt compiled', etree.LIBXSLT_COMPILED_VERSION))
libxslt compiled : (1, 1, 26)
In [13]: ## -----

Revision history for this message
scoder (scoder) wrote : Re: [Bug 715737] [NEW] lxml.etree.parse method don't close the file

Rodrigo F. S., 09.02.2011 13:39:
> in the method parse it open the file but don't close: (looking at link
> http://codespeak.net/lxml/api/xml.etree.ElementTree-pysrc.html#)

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,
> 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.close() after the parse..

> lxml.etree : (2, 2, 4, 0)

If you are referring to lxml.etree, this was likely fixed in lxml 2.3.
Please upgrade.

Stefan

scoder (scoder)
Changed in lxml:
assignee: nobody → Stefan Behnel (scoder)
importance: Undecided → Medium
milestone: none → 2.3
status: New → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.