2
# tag: coverage,trace,nogil,fastgil
5
PYTHON setup.py build_ext -i
9
######## setup.py ########
11
from distutils.core import setup
12
from Cython.Build import cythonize
14
setup(ext_modules = cythonize([
15
'coverage_test_*.pyx',
19
######## .coveragerc ########
21
plugins = Cython.Coverage
24
######## coverage_test_nogil_fastgil.pyx ########
25
# cython: linetrace=True,fast_gil=True
26
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1 CYTHON_USE_SYS_MONITORING=0
27
include "_coverage_test_nogil.pxi"
30
######## coverage_test_nogil_nofastgil.pyx ########
31
# cython: linetrace=True,fast_gil=False
32
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1 CYTHON_USE_SYS_MONITORING=0
33
include "_coverage_test_nogil.pxi"
36
######## _coverage_test_nogil.pxi ########
40
cdef int func1(int a, int b) nogil: # 4
44
cdef int c = func2(a) + b # 8
48
cdef int func2(int a) with gil: # 12
52
def call(int a, int b): # 16
55
result = func1(b, a) # 19
59
######## coverage_test.py ########
63
# io.StringIO in Py2.x cannot handle str ...
64
from StringIO import StringIO
66
from io import StringIO
68
from coverage import coverage
71
def run_coverage(module_name):
72
print("Testing module %s" % module_name)
76
module = __import__(module_name)
77
module_name = module.__name__
78
module_path = module_name + '.pyx'
79
assert not any(module.__file__.endswith(ext)
80
for ext in '.py .pyc .pyo .pyw .pyx .pxi'.split()), \
82
assert module.call(1, 2) == (1 * 2) + 2 + 1
87
#cov.report([module], file=out)
88
lines = out.getvalue().splitlines()
89
assert any(module_path in line for line in lines), \
90
"'%s' not found in coverage report:\n\n%s" % (module_path, out.getvalue())
92
module_pxi = "_coverage_test_nogil.pxi"
93
mod_file, exec_lines, excl_lines, missing_lines, _ = cov.analysis2(os.path.abspath(module_pxi))
94
assert module_pxi in mod_file
96
executed = set(exec_lines) - set(missing_lines)
97
# check that everything that runs with the gil owned was executed (missing due to pxi: 4, 12, 16)
98
assert all(line in executed for line in [13, 17, 18, 20]), '%s / %s' % (exec_lines, missing_lines)
99
# check that everything that runs in nogil sections was executed
100
assert all(line in executed for line in [6, 7, 8, 9]), '%s / %s' % (exec_lines, missing_lines)
103
if __name__ == '__main__':
104
for module_name in ["coverage_test_nogil_fastgil", "coverage_test_nogil_nofastgil"]:
105
run_coverage(module_name)