Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use coverage analysis with Cython

I'm trying to run coverage analysis on some Cython code using pytest-cov and coveralls.io. I've got as far as building the extension modules with tracing enabled, and running the analysis with the help of the links below:

http://docs.cython.org/src/tutorial/profiling_tutorial.html

http://blog.behnel.de/posts/coverage-analysis-for-cython-modules.html

However, I'm getting some results that I can't explain. It seems that many of the def/cdef/cpdef lines in the code are showing as not running, despite code within them being OK. The results aren't even consistent as some lines seem OK.

Example report: https://coveralls.io/files/1871744040

I don't know if I'm calling something wrong, if this is a bug, or if I'm just not interpreting the results correctly.

coveralls screenshot

In the example above, the get_cost method seems OK, but the __set__ method for the property above is not called, despite the lines within the function having been called.

Update: It seems the issue is with Cython classes. If the class is defined with def rather than cdef the problem goes away. I guess there isn't full support for this yet.

like image 872
Snorfalorpagus Avatar asked Jan 21 '16 22:01

Snorfalorpagus


1 Answers

If the Cython tracing facility does not seem to work as intended, it should be possible to use gcov for the coverage analysis of cython code. This way one could verify if some line of the generated C code is executed or not.

With a simple main.pyx

import mymod

def main():
    mymod.test()

and mymod.pyx

def test():
    return 42

and then

cython --embed main.pyx
cython mymod.pyx

gcc -O1 -fPIC -fprofile-arcs -ftest-coverage -Wall -I/usr/include/python2.7 -c -o main.o main.c
gcc main.o -fprofile-arcs -lpython2.7 -lgcov -o main
gcc -O1 -fPIC -fprofile-arcs -ftest-coverage -Wall -I/usr/include/python2.7 -c -o mymod.o mymod.c
gcc -shared mymod.o -fprofile-arcs -lgcov -lpython2.7 -o mymod.so

an executable was created. After executing ./main main.gcda and mymod.gcda were created for gcov.

like image 165
J.J. Hakala Avatar answered Sep 22 '22 23:09

J.J. Hakala