6

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.

Snorfalorpagus
  • 3,348
  • 2
  • 29
  • 51
  • An update for people who are still looking into this: the Cython issue where this is being discussed is [here](https://github.com/cython/cython/issues/1461). – Vyas Aug 22 '18 at 14:06

1 Answers1

2

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.

J.J. Hakala
  • 6,136
  • 6
  • 27
  • 61
  • Does this method present the coverage in terms of the .c, rather than the .pyx? – Snorfalorpagus Jan 22 '16 at 21:53
  • @Snorfalorpagus as an alternative in case there seems to be something wrong with the cython tracing. If both methods of coverage are used, it should be possible to determine if cython tracing is not working as intended. Writing a bug report might be good in that case. – J.J. Hakala Jan 22 '16 at 22:55