[Cython] BUG: cannot override cdef by cpdef method in .pxd

Jeroen Demeyer jdemeyer at cage.ugent.be
Wed Jul 27 11:34:40 EDT 2016


Hello,

I recently learned from Robert Bradshaw that it is legal in Cython to 
override cdef methods by cpdef methods. But doing this in a .pxd file 
causes a compile-time error:

*foo.pyx*:

cdef class Base(object):
     cdef meth(self):
         print("Base.meth()")

cdef class Derived(Base):
     cpdef meth(self):
         print("Derived.meth()")

*foo.pxd*:

cdef class Base(object):
     cdef meth(self)

cdef class Derived(Base):
     cpdef meth(self)

This gives (both on 0.24.1 and on master):

Error compiling Cython file:
------------------------------------------------------------
...
cdef class Base(object):
     cdef meth(self):
         return self

cdef class Derived(Base):
     cpdef meth(self):
          ^
------------------------------------------------------------

foo.pyx:6:10: 'meth' already defined


As second attempt, I could simply not declare the cpdef method in the 
.pxd file. Just use

cdef class Derived(Base):
     pass

This compiles, but leads to bugs with broken vtabs. I am omitting the 
complete code here, but it's clear why this cannot work: other modules 
get the vtab wrong since they don't know about the cpdef slot.


Jeroen.


More information about the cython-devel mailing list