[lxml-dev] resolvers

Steven Vereecken steven.vereecken at gmail.com
Tue Sep 16 17:41:46 CEST 2008


I've added some simple examples to illustrate what I mean. Hope this helps...

2008/9/15 Steven Vereecken <steven.vereecken at gmail.com>:
> Hi,
>
> I 've got a couple of strange things with custom resolvers:
>
> 1) I can't get the resolve_file method to work, not with actual file
> objects, and not with StringIO either. I always get the following
> message:
> Exception exceptions.TypeError: 'function takes exactly 4 arguments (3
> given)' in 'lxml.etree._local_resolver'  ignored
> As it says, this is ignored, but I get lxml.etree.XMLSyntaxError: None later on.
> This is with lxml 2.1.1 (on Windows XP) . I've also tried an older
> version (1.3.6) that was installed on my Ubuntu box, and that worked,
> however, after building 2.1.2 there, I get the same problem.
> I'm not sure whether this is an actual bug, or whether something has
> changed that is not clear from the docs.
>

Example:

from cStringIO import StringIO
from lxml import etree

test_file = StringIO('''<?xml version="1.0" encoding="utf-8"?>
<doc>h\xc3\xa9llo world</doc>''')

class TestResolver(etree.Resolver):
    def resolve(self, url, id, context):
        return self.resolve_file(test_file, context)

test_parser = etree.XMLParser()
test_parser.resolvers.add(TestResolver())

# this works
etree.parse(test_file)te
# this doesn't
etree.parse('dummy', parser=test_parser)

> 2) resolve_string seems only to work for unicode strings and
> ASCII-only bytestrings. I would expect behaviour more consistent with
> etree.fromstring, so I'd assume this is a bug too?
>
> If you can confirm these are bugs, I'll file them in the tracker.
>
> greetings,
>
> Steven
>

Example:

from lxml import etree

test_string = '''<?xml version="1.0" encoding="utf-8"?>
<doc>h\xc3\xa9ello world</doc>'''

class TestResolver(etree.Resolver):
    def resolve(self, url, id, context):
        return self.resolve_string(test_string, context)

test_parser = etree.XMLParser()
test_parser.resolvers.add(TestResolver())

# this works
etree.fromstring(test_string)
# this doesn't
etree.parse('dummy', parser=test_parser)


More information about the lxml-dev mailing list