Skip to content

Commit

Permalink
Support coverage of Cython code
Browse files Browse the repository at this point in the history
  • Loading branch information
mdekstrand committed Mar 4, 2024
1 parent a0f1dea commit 0c9a3d7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
Empty file removed .coveragerc
Empty file.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,6 @@ exclude = [
]
reportMissingImports = true
reportMissingTypeStubs = false

[tool.coverage.run]
plugins = ["Cython.Coverage"]
16 changes: 14 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

from setuptools import Extension, setup

try:
Expand All @@ -7,19 +9,29 @@
except ImportError:
USE_CYTHON = False


COVERAGE = os.environ.get("BUILD_FOR_COVER", None)
EXT_SPECS = {"lenskit.util.kvp": None}

CYTHON_OPTIONS = {}
C_DEFINES = []
if COVERAGE:
print("enabling tracing")
CYTHON_OPTIONS["linetrace"] = True
C_DEFINES.append(("CYTHON_TRACE_NOGIL", "1"))


def _make_extension(name: str, opts: None) -> Extension:
path = name.replace(".", "/")
if USE_CYTHON:
path += ".pyx"
else:
path += ".c"
return Extension(name, [path])
return Extension(name, [path], define_macros=C_DEFINES)


EXTENSIONS = [_make_extension(ext, opts) for (ext, opts) in EXT_SPECS.items()]
if USE_CYTHON:
EXTENSIONS = cythonize(EXTENSIONS)
EXTENSIONS = cythonize(EXTENSIONS, compiler_directives=CYTHON_OPTIONS)
print(EXTENSIONS[0].__dict__)
setup(ext_modules=EXTENSIONS)

0 comments on commit 0c9a3d7

Please sign in to comment.