| 1 |
# mode: run |
| 2 |
# tag: coverage,trace,nogil,fastgil |
| 3 |
|
| 4 |
""" |
| 5 |
PYTHON setup.py build_ext -i |
| 6 |
PYTHON coverage_test.py |
| 7 |
""" |
| 8 |
|
| 9 |
######## setup.py ######## |
| 10 |
|
| 11 |
from distutils.core import setup |
| 12 |
from Cython.Build import cythonize |
| 13 |
|
| 14 |
setup(ext_modules = cythonize([ |
| 15 |
'coverage_test_*.pyx', |
| 16 |
])) |
| 17 |
|
| 18 |
|
| 19 |
######## .coveragerc ######## |
| 20 |
[run] |
| 21 |
core = ctrace |
| 22 |
plugins = Cython.Coverage |
| 23 |
|
| 24 |
|
| 25 |
######## coverage_test_nogil_fastgil.pyx ######## |
| 26 |
# cython: linetrace=True,fast_gil=True |
| 27 |
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1 CYTHON_USE_SYS_MONITORING=0 |
| 28 |
include "_coverage_test_nogil.pxi" |
| 29 |
|
| 30 |
|
| 31 |
######## coverage_test_nogil_nofastgil.pyx ######## |
| 32 |
# cython: linetrace=True,fast_gil=False |
| 33 |
# distutils: define_macros=CYTHON_TRACE=1 CYTHON_TRACE_NOGIL=1 CYTHON_USE_SYS_MONITORING=0 |
| 34 |
include "_coverage_test_nogil.pxi" |
| 35 |
|
| 36 |
|
| 37 |
######## _coverage_test_nogil.pxi ######## |
| 38 |
# 1 |
| 39 |
# 2 |
| 40 |
# 3 |
| 41 |
cdef int func1(int a, int b) nogil: # 4 |
| 42 |
cdef int x # 5 |
| 43 |
with gil: # 6 |
| 44 |
x = 1 # 7 |
| 45 |
cdef int c = func2(a) + b # 8 |
| 46 |
return x + c # 9 |
| 47 |
# 10 |
| 48 |
# 11 |
| 49 |
cdef int func2(int a) with gil: # 12 |
| 50 |
return a * 2 # 13 |
| 51 |
# 14 |
| 52 |
# 15 |
| 53 |
def call(int a, int b): # 16 |
| 54 |
a, b = b, a # 17 |
| 55 |
with nogil: # 18 |
| 56 |
result = func1(b, a) # 19 |
| 57 |
return result # 20 |
| 58 |
|
| 59 |
|
| 60 |
######## coverage_test.py ######## |
| 61 |
|
| 62 |
import os.path |
| 63 |
try: |
| 64 |
# io.StringIO in Py2.x cannot handle str ... |
| 65 |
from StringIO import StringIO |
| 66 |
except ImportError: |
| 67 |
from io import StringIO |
| 68 |
|
| 69 |
from coverage import coverage |
| 70 |
|
| 71 |
|
| 72 |
def run_coverage(module_name): |
| 73 |
print("Testing module %s" % module_name) |
| 74 |
cov = coverage() |
| 75 |
cov.start() |
| 76 |
|
| 77 |
module = __import__(module_name) |
| 78 |
module_name = module.__name__ |
| 79 |
module_path = module_name + '.pyx' |
| 80 |
assert not any(module.__file__.endswith(ext) |
| 81 |
for ext in '.py .pyc .pyo .pyw .pyx .pxi'.split()), \ |
| 82 |
module.__file__ |
| 83 |
assert module.call(1, 2) == (1 * 2) + 2 + 1 |
| 84 |
|
| 85 |
cov.stop() |
| 86 |
out = StringIO() |
| 87 |
cov.report(file=out) |
| 88 |
#cov.report([module], file=out) |
| 89 |
lines = out.getvalue().splitlines() |
| 90 |
assert any(module_path in line for line in lines), \ |
| 91 |
"'%s' not found in coverage report:\n\n%s" % (module_path, out.getvalue()) |
| 92 |
|
| 93 |
module_pxi = "_coverage_test_nogil.pxi" |
| 94 |
mod_file, exec_lines, excl_lines, missing_lines, _ = cov.analysis2(os.path.abspath(module_pxi)) |
| 95 |
assert module_pxi in mod_file |
| 96 |
|
| 97 |
executed = set(exec_lines) - set(missing_lines) |
| 98 |
# check that everything that runs with the gil owned was executed (missing due to pxi: 4, 12, 16) |
| 99 |
assert all(line in executed for line in [13, 17, 18, 20]), '%s / %s' % (exec_lines, missing_lines) |
| 100 |
# check that everything that runs in nogil sections was executed |
| 101 |
assert all(line in executed for line in [6, 7, 8, 9]), '%s / %s' % (exec_lines, missing_lines) |
| 102 |
|
| 103 |
|
| 104 |
if __name__ == '__main__': |
| 105 |
for module_name in ["coverage_test_nogil_fastgil", "coverage_test_nogil_nofastgil"]: |
| 106 |
run_coverage(module_name) |