[Cython] Problem with final cdef methods

Stefan Behnel stefan_ml at behnel.de
Thu Aug 29 08:57:19 CEST 2013


Nikita Nemkin, 29.08.2013 08:24:
> On Thu, 29 Aug 2013 11:03:01 +0600, Stefan Behnel wrote:
>> http://trac.cython.org/cython_trac/ticket/819
> 
> As your ticket mentions, there is a fundamental problem with method
> inheritance.
> (and with the way Cython generates forward declarations...)
> 
> Any inherited method assumes 2 types simultaneously: the type of the base
> method with 1st parameter being a base class pointer and the "actual" type
> of the method with 1st parameter being derived class pointer.
> 1st type, stored in method's Entry.type, is essentially the vtable slot
> type. It's used when virtual method calls a made. It's also used for
> generating
> forward declarations and for casting in vtable init code.
> 2nd type (stored in CFuncDef node) is used ONLY to generate function
> definition.
> 
> The mismatch between these 2 types is the root of the problem.
> Before final methods were introduced, the 2nd ("actual") type was unimportant.
> Method was cast to the vtable slot type in vtable init code and forgotten.
> But final functions, bypassing vtable mechanic, rely soley on the 2nd type.

Thanks for writing up the analysis.


> I have solved it for myself by storing BOTH types in the method entry
> (Entry.type for the actual CFuncDef type and Entry.prev_type for the vtable
> slot type). By using correct types in
> generate_exttype_final_methods_declaration()
> and generate_exttype_vtable_init_code() the problem is avoided.
> You can see the patch here
> https://github.com/nnemkin/cython/compare/final_subtypes

Interesting. Your change dates back a while already. Were you planning to
clean it up in some way before you submit it as pull request?


> Notes for the patch:
> * I removed a bit of wtf code from CFuncType.declaration_code,

I'm not surprised. :)


> * There is a disabled bug test named "inherited_final_method",

And you had to disable it, because ... ?


> you may want to remove/merge it with the test you modified.

I already looked at it before I changed the main test. It appears to be a
regression test for a specific bug that's different from the problem at
hand, that's why I didn't touch it.

Stefan



More information about the cython-devel mailing list