cython

Форк
0
/
coverage_nogil.srctree 
105 строк · 3.0 Кб
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
plugins = Cython.Coverage
22

23

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"
28

29

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"
34

35

36
######## _coverage_test_nogil.pxi ########
37
#  1
38
#  2
39
#  3
40
cdef int func1(int a, int b) nogil:  #  4
41
    cdef int x                       #  5
42
    with gil:                        #  6
43
        x = 1                        #  7
44
    cdef int c = func2(a) + b        #  8
45
    return x + c                     #  9
46
# 10
47
# 11
48
cdef int func2(int a) with gil:  # 12
49
    return a * 2                 # 13
50
# 14
51
# 15
52
def call(int a, int b):          # 16
53
    a, b = b, a                  # 17
54
    with nogil:                  # 18
55
        result = func1(b, a)     # 19
56
    return result                # 20
57

58

59
######## coverage_test.py ########
60

61
import os.path
62
try:
63
    # io.StringIO in Py2.x cannot handle str ...
64
    from StringIO import StringIO
65
except ImportError:
66
    from io import StringIO
67

68
from coverage import coverage
69

70

71
def run_coverage(module_name):
72
    print("Testing module %s" % module_name)
73
    cov = coverage()
74
    cov.start()
75

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()), \
81
        module.__file__
82
    assert module.call(1, 2) == (1 * 2) + 2 + 1
83

84
    cov.stop()
85
    out = StringIO()
86
    cov.report(file=out)
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())
91

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
95

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)
101

102

103
if __name__ == '__main__':
104
    for module_name in ["coverage_test_nogil_fastgil", "coverage_test_nogil_nofastgil"]:
105
        run_coverage(module_name)
106

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.