O'Reilly logo

Learning Cython Programming by Philip Herron

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

General Cython caveats

There are several caveats worth noting when it comes to Cython while mixing C and the Python code. As for me, I tend to keep the interfaces between the C and Python projects as simple as possible so that it's clear when and where something wrong is going on.

Type checking

You may have noticed that in 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, for example:

char * buf = <char *> malloc (sizeof (...))

In basic type casting, Cython supports <type?> for type checking for example:

char * buf  = <char *?> malloc (...)

This will do some type checking and will throw an error if the type being cast is not a subclass of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required