I have been using Cython's pure Python mode for a while now, and have
generally been quite pleased with it. Now I am trying to use it with a
more complex codebase, which has led me to encounter some "rough around
the edges" features. I am using Cython 0.12.1 with Python 2.6.5.
This particular bug has to do with method overriding in a derived class.
Consider the Python file foo.py containing a base class Foo and derived
class Bar, with the method hello() implemented differently in each class:
class Foo:
def hello(self):
print 'Hello from Foo.hello()'
class Bar(Foo):
def hello(self):
print 'Hello from Bar.hello()'
The associated foo.pxd header file is:
cdef class Foo:
cpdef hello(self)
cdef class Bar(Foo):
cpdef hello(self)
My test script is as follows:
from foo import Foo, Bar
f = Foo()
f.hello() # Should print 'Hello from Foo.hello()'
b = Bar()
b.hello() # Should print 'Hello from Bar.hello()'
Before compiling, the test script works as expected. However, after
compiling, a TypeError is raised:
Traceback (most recent call last):
File "test.py", line 7, in <module>
f.hello()
File "foo.py", line 6, in foo.Foo.hello (foo.c:512)
def hello(self):
TypeError: Cannot convert foo.Foo to foo.Bar
I'd be happy to provide the generated C code as well. This seems to be
the offending line:
if (!(likely(((__pyx_v_self) == Py_None) ||
likely(__Pyx_TypeTest(__pyx_v_self, __pyx_ptype_3foo_Bar)))))
{__pyx_filename = __pyx_f[0]; __pyx_lineno = 6; __pyx_clineno =
__LINE__; goto __pyx_L1_error;}
From what I can tell, this only allows objects of type Bar to continue,
and not those of type Foo, despite being a method of the Foo class.
Oddly enough, the line above was duplicated in the generated C code; I
wonder if one of them should test for __pyx_ptype_3foo_Foo instead, but
I don't know much about the Cython internals so I may be way off base.
Note that this is a follow-up and clarification to
http://codespeak.net/pipermail/cython-dev/2009-September/006846.html
which seems to have not been fully addressed.
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev