Bug(?) with pointer to cdef class method of different types with except *

9 views
Skip to first unread message

Source

unread,
Mar 31, 2022, 2:17:44 PM3/31/22
to cython-users
So I've written a basic system that checks a map for string input and calls a function pointer associated with it if found, this is imo the best organization of the system for a variety of reasons, one of them being that it's actually more performant than if elif's, it reduces accidental programming errors by having the functions return bools and do various things based on the returned result which is simpler and easier than remembering to add additional if's in every single function, it's neater, more beautiful, more performant, better organized, etc.

The issue I just discovered is when trying to add except * to these cdef class methods which is a nice and important feature.

I found out you can add except * to the ctypedef, however the caveat being somehow that this only works for cdef functions and cdef class methods that are of type void, NOT bool or int for example.

If they are of type bint/bool or int I get the error "Cannot assign type 'int (testclass) except *' to 'mptr'".
Code:
ctypedef int (*mptr)(testclass) except *
cdef class testclass:
                #cdef void test(self) except *:
                cdef int test(self) except *:
                        pass
cdef mptr m = t.test

If you change from int to void or if you remove except * it works.
The same problem does not exist with regular cdef functions.
And I haven't been able to find even an appropriate ctypedef definition to use for a cppclass (instead of a cdef class) to declare a cppclass method pointer in cython, only in pure C++.
I could extern from, but I'd have to rewrite my entire codebase in C++ for the functions to be able to call all the code that's currently written in cython, which is a long-term goal of mine but is gonna take forever and might never be fully realized.
Reply all
Reply to author
Forward
0 new messages