[lxml-dev] Resolvers and open files

Sidnei da Silva sidnei at enfoldsystems.com
Tue Oct 24 22:02:16 CEST 2006


I have a long-running process that uses a custom resolver to resolve a
simple filename to a file relative to a pre-configured directory.

It looks like this:


      class RelativeUrlResolver(etree.Resolver):
        def __init__(self, prefix):
            self.prefix = prefix

        def resolve(self, url, id, context):
            print "Resolving URL '%s'" % url
            if not url.startswith('http'):
                url = self.prefix + urllib.quote_plus(url)
            ssf = urllib.urlopen(url)
            if ssf is None:
                raise ValueError, 'could not resolve url: %r' % url
            return self.resolve_file(ssf, context)

I'm creating the parser like this:

        parser = etree.XMLParser()
        parser.resolvers.add(RelativeUrlResolver(BASE))

(Where BASE = 'file:///path/to/some/dir')

Now, the issue that's biting me is that it looks like the file is kept
open after the processing has finished.

The parser is re-created every time ATM, and goes 'out of scope' right
after doing the transformation, so I would expect it all to be garbage
collected, and the file to be closed.

Do I need to do anything special to get this file to be closed?

Thanks.

-- 
Sidnei da Silva
Enfold Systems                http://enfoldsystems.com
Fax +1 832 201 8856     Office +1 713 942 2377 Ext 214


More information about the lxml-dev mailing list