[lxml-dev] segfault when using etree.CustomElementClassLookup

Martijn Faassen faassen at startifact.com
Wed Jun 4 22:01:24 CEST 2008


Hey Stefan,

On Wed, Jun 4, 2008 at 2:38 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> Martijn Faassen wrote:
>> I just ran into a segfault with lxml (2.0.6). The problem is as follows:
>>
>> from lxml import etree
>>
>> class Lookup(etree.CustomElementClassLookup):
>>      def __init__(self):
>>          pass
>
> Yep, you didn't call the __init__() method of the super class here, so the
> internal lookup function call isn't set up.

Hm, I thought you were wrong, but you are right. I actually did have a
super call before I whittled it away to a minimal (too minimal!) test
case:

class Lookup(etree.CustomElementClassLookup):
  def __init__(self):
      super(etree.CustomElementClassLookup, self).__init__()

But I just realized that call was wrong, and should've been:

class Lookup(etree.CustomElementClassLookup):
    def __init__(self):
        super(Lookup, self).__init__()

that *does* work. :)

> I replaced that with a __cinit__()
> now that always sets it to the default lookup scheme, so that it won't
> segfault anymore even if people forget the obvious. ;)

> A patch is attached and it's generally easy to work around this by writing
> correct code, so there won't be a 2.0.7 right away.

Yes, that's fine, I could work around it anyway, and you're right it's
also a mistake for me. You don't expect a segfault even if you do it
wrong of course, but it's a corner case. My apologies for the mistaken
bug report!

> BTW, this:
>
>> parser.setElementClassLookup(lookup)
>
> is correctly spelled
>
>> parser.set_element_class_lookup(lookup)
>
> since lxml 2.0, following PEP 8 naming conventions. However, I didn't dare to
> remove the original method, since I figured that it would break tons of code
> for no major reason. At least the examples should reflect the new name
> everywhere now, so maybe I can remove it in lxml 3.0. ;)

The documentation on the website still has the camelCases when I read
it yesterday.

Regards,

Martijn


More information about the lxml-dev mailing list