Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug cython code (.pyx) when using the python debugger (pdb) - Best Practice

I have read Cython debugging, put a break point, and https://groups.google.com/forum/#!topic/apam-python-users/6rsRwcCAms4 and wondering what the best workflow is when debugging cython code that is called from python code?

Ideally I would like to step into .pyx files during a python debugging session initiated from my IDE (pycharm), but it seems this is impossible. Isn't it possible to compile debug information when the pyx files are cythonized so that the debugger can step in?

If this can't be achieved, what are the alternatives (apart from not using cython!)?

As this question specifically asks how to step into cython code, although it is similar to Cython & Python Project Test Driven Development and .pyx file structure advice, it is not the same.

like image 646
rnoodle Avatar asked Oct 01 '15 13:10

rnoodle


2 Answers

If you're only using Cython for speed (i.e. not to wrap C libraries) you could use pure Python mode which lets you define the types either in a separate .pxd file (which exists alongside your code in the .py file), or using decorators.

The advantage of this mode is that your code can run (and be debugged) under plain Python. You're then left with the (hopefully small) class of bugs that are due to the Cython static typing and not your code. The disadvantages are: 1) running your code under plain Python will be slower; 2) the syntax is a little bit messier than the standard Cython syntax; 3) you can't access external C code like this, which is one of the main use-cases for Cython.

Failing that your best bet is the traditional "lots of print statements". print locals() can be useful here! It isn't entirely satisfactory though.

like image 161
DavidW Avatar answered Nov 19 '22 05:11

DavidW


It seems like the official way is your best option. It would be great if there were an easy alternative but from the link you included here it seems like there isn't. This wiki document seems to have a few additional tips that the official doc is missing.

like image 3
emschorsch Avatar answered Nov 19 '22 06:11

emschorsch