From 0c9a3d744105f172bc5de627936ea574d60ae00d Mon Sep 17 00:00:00 2001 From: Michael Ekstrand Date: Mon, 4 Mar 2024 09:36:23 -0500 Subject: [PATCH] Support coverage of Cython code --- .coveragerc | 0 pyproject.toml | 3 +++ setup.py | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) delete mode 100644 .coveragerc diff --git a/.coveragerc b/.coveragerc deleted file mode 100644 index e69de29bb..000000000 diff --git a/pyproject.toml b/pyproject.toml index ac2ed2adf..be5a69f79 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -157,3 +157,6 @@ exclude = [ ] reportMissingImports = true reportMissingTypeStubs = false + +[tool.coverage.run] +plugins = ["Cython.Coverage"] diff --git a/setup.py b/setup.py index c62f019b3..8b53a6d34 100644 --- a/setup.py +++ b/setup.py @@ -1,3 +1,5 @@ +import os + from setuptools import Extension, setup try: @@ -7,8 +9,17 @@ 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(".", "/") @@ -16,10 +27,11 @@ def _make_extension(name: str, opts: None) -> Extension: 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)