You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I'm not actually sure if that is supposed to not work, but its not documented, so either it should be added to the docs or be considered a bug.
Anyway, when having two extension types, one inheriting the other and deriving a method, e.g. a cdef one, which gets called within the base class's cinit() method, the derived method doesn't get called. To Reproduce
Code to reproduce the behaviour:
cdef class Foo:
def__cinit__(self):
self.talk()
cdef void talk(self):
print("i'm talking")
cdef class Bar(Foo):
cdef void talk(self):
print("now you're talking!")
Expected behavior
I'd expect it to print "now you're talking!", instead of printing "i'm talking".
A rather obvious solution would be to switch from cinit() to init() here (at least i'd expect that to work, didn't try it yet though). I'm using this "feature" to initialize different attributes of my class, the subtype having more attributes than the base class, therefore the subtype derives the initialization cdef method to initialize all of its additional attributes. I just noticed that this never happens due to the base class only calling its own cdef instead of the derived ones.
Environment (please complete the following information):
OS: tested on Windows 10
Python version 3.8.2
Cython version 3.0a6
The text was updated successfully, but these errors were encountered:
I wouldn't expect this to work or consider it a bug. You're right that it isn't explicitly documented though and a PR to correct this would probably be welcome.
The call order is roughly: "base __cinit__"->"derived __cinit__"->"__init__ functions as defined by Python". Therefore base __cinit__ is called very early and has basically no knowledge that it might be in a derived class. If you're familiar with C++ then I believe that virtual functions in C++ constructors work very similarly.
I would expect your suggestion of moving code to __init__ to work. A single class can have both a __cinit__ or __init__ method so you can use them both as needed.
The virtual function table of the subclass probably isn't initialised yet at this point. That's reasonable, because __cinit__() is called on the way back from Python's object allocator. If it's not documented yet, then it's worth stating somewhere that the code in __cinit__() must not touch (or use) the state of subclasses, since they are not initialised yet when it is called.
I consider the behaviour undefined in this case. You should not do this.
As suggested, use __init__() for anything that requires a fully initialised object.
Describe the bug
I'm not actually sure if that is supposed to not work, but its not documented, so either it should be added to the docs or be considered a bug.
Anyway, when having two extension types, one inheriting the other and deriving a method, e.g. a cdef one, which gets called within the base class's cinit() method, the derived method doesn't get called.
To Reproduce
Code to reproduce the behaviour:
Expected behavior
I'd expect it to print "now you're talking!", instead of printing "i'm talking".
A rather obvious solution would be to switch from cinit() to init() here (at least i'd expect that to work, didn't try it yet though). I'm using this "feature" to initialize different attributes of my class, the subtype having more attributes than the base class, therefore the subtype derives the initialization cdef method to initialize all of its additional attributes. I just noticed that this never happens due to the base class only calling its own cdef instead of the derived ones.
Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: