There are some caveats worth noting while mixing C and the Python code when it comes to Cython. It's a good idea to refer to these when building something to be production ready.
You may have noticed that in the previous code examples, we were able to cast the void *
pointer from malloc
to our extension types using malloc
. Cython supports some more advanced type checking as follows:
char * buf = <char *> malloc (sizeof (...))
In basic type casting, Cython supports <type?>
for type checking:
char * buf = <char *?> malloc (...)
This will do some type checking and throw an error if the type that is being cast is not a subclass of char *
. So, in this case, it will pass; however, if you were to do the following:
cdef class A:
pass
cdef class B (A):
pass
def myfunc ():
cdef A class1 = A ()
cdef B class2 = B ()
cdef B x = <B?> class1
This will return an error (at runtime):
Traceback (most recent call last): File "main.py", line...