1# mode: run 2# tag: coverage,trace 3 4""" 5PYTHON setup.py build_ext -i 6PYTHON -c "import shutil; shutil.move('ext_src/ext_pkg', 'ext_pkg')" 7PYTHON -m coverage run coverage_test.py 8PYTHON -m coverage report 9""" 10 11######## setup.py ######## 12from distutils.core import setup, Extension 13from Cython.Build import cythonize 14 15setup(ext_modules = cythonize([ 16 'pkg/*.pyx', 17])) 18 19setup( 20 name='ext_pkg', 21 package_dir={'': 'ext_src'}, 22 ext_modules = cythonize([ 23 Extension('ext_pkg._mul', ['ext_src/ext_pkg/mul.py']) 24 ]), 25) 26 27 28######## .coveragerc ######## 29[run] 30plugins = Cython.Coverage 31 32 33######## pkg/__init__.py ######## 34from .test_ext_import import test_add 35 36 37######## pkg/test_ext_import.pyx ######## 38# cython: linetrace=True 39# distutils: define_macros=CYTHON_TRACE=1 40 41import ext_pkg 42 43 44cpdef test_add(int a, int b): 45 return a + ext_pkg.test_mul(b, 2) 46 47 48######## ext_src/ext_pkg/__init__.py ######## 49from .mul import test_mul 50 51 52######## ext_src/ext_pkg/mul.py ######## 53from __future__ import absolute_import 54 55 56def test_mul(a, b): 57 return a * b 58 59 60try: 61 from ._mul import * 62except ImportError: 63 pass 64 65 66######## coverage_test.py ######## 67 68from pkg import test_add 69 70 71assert 5 == test_add(1, 2) 72