[Pyrex] __Pyx_GetStarArgs can cause segfault when called by __new__

Paul Prescod paul at prescod.net
Sat Jan 17 06:00:56 CET 2004


Jiba wrote:
> Here is a small example that DOES segfault, with an artificial call to
> gc.collect() :
> (Notice that if you define the "obj" attribute in class C instead of D,
> it does not segfault. It seems that when C.__new__ is executed, C
> attributes are initialized to None, but D ones are not initialized yet)

There are no C attributes in the example you showed.

But anyhow, I think the right solution is a slightly smarter 
tp_traverse. Each type that "participates" in garbage collection does so 
by defining tp_traverse. tp_traverse tells the garbage collector what 
its struct members are. If it skips one, the garbage collector won't 
know about it. That's fine in the case where a member is NULL (not None, 
NULL). I believe Python initializes all properties to NULL when it 
creates objects...so Pyrex doesn't even need to do it.

Here is the fix:

     def generate_traverse_function(self, scope, code):

...

         for entry in scope.var_entries:
             if entry.type.is_pyobject:
                 var_code = "p->%s" % entry.cname
+               code.putln("if(%s){"% var_code)
                 if entry.type.is_extension_type:
                     var_code = "((PyObject*)%s)" % var_code
                 code.putln(
                         "e = (*v)(%s, a); if (e) return e;"
                          % var_code)
+               code.putln("}")

If I'm right, it turned out to be quite easy.

  Paul Prescod






More information about the Pyrex mailing list