[Cython] [cython-users] which __new__ to use?

Stefan Behnel stefan_ml at behnel.de
Fri Jan 10 17:42:15 CET 2014


Stefan Behnel, 10.01.2014 17:30:
> Nils Bruin, 22.11.2013 00:10:
>> With the following classes:
>>
>> cdef class A(object):
>>     cdef int a
>>     pass
>> cdef class B(object):
>>     pass
>>
>> and the following multiply inherited classes:
>>
>> The problem arises 
>>
>> class C(object):
>>     pass
>> class D(C,B):
>>     pass
>> class F(B,C):
>>     pass
>> class E(C,A):
>>     pass
>> class G(A,C):
>>     pass
>>
>> I observe:
>>
>>>>> D.__new__(D)
>>
>> B.__new__(D) is not safe, use object.__new__()
>>
>>>>> E.__new__(E)
>>
>>  <__main__.E object at 0x6c56488>
>>
>>>>> F.__new__(F)
>>
>> <__main__.F object at 0x6c4fdd0>
>>>>> G.__new__(G)
>> <__main__.G object at 0x6c4fdd0>
>>
>>
>> As you can see, a problem only arises for D, in which case there is 
>> actually NOT a layout problem: when the class A is involved, where it does 
>> matter which __new__ is used, that right one (I assume) is chosen in all 
>> cases. A little googling suggests that this error might be coming from 
>> tp_new_wrapper in Objects/typeobject.c, which tries to do some sanity 
>> checks before calling type->tp_new. It is not clear to me, however, why 
>> those sanity checks fail for D.__new__(D) and succeed for E.__new__(E)
> 
> I came up with this fix:
> 
> https://github.com/cython/cython/commit/ec37469dc61e0e24db448e9455b2483797afe936
> 
> It passes your test case and doesn't seem to break others so far.
> 
> However, this is a bit of a deep change, so I'd like to see some serious
> user side testing before I'll happily push for its inclusion in Cython 0.20.

And, not entirely unexpected, it breaks Sage pretty much completely:

https://sage.math.washington.edu:8091/hudson/job/sage-tests/1645/consoleFull

Stefan



More information about the cython-devel mailing list