From b7a972a44cde525ce8ff5a0f37e56b36200600fb Mon Sep 17 00:00:00 2001 From: Marco Treglia Date: Fri, 19 Apr 2024 18:44:18 +0200 Subject: [PATCH 1/2] vendor: enable cprofile in Cython bindings --- cython_setuptools/vendor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cython_setuptools/vendor.py b/cython_setuptools/vendor.py index 493b48a..10f19a8 100644 --- a/cython_setuptools/vendor.py +++ b/cython_setuptools/vendor.py @@ -166,7 +166,7 @@ def setup(original_setup_file: str, cythonize: bool = True, **kwargs): except ImportError: pass else: - cython_ext_modules = Build.cythonize(cython_ext_modules, force=True) + cython_ext_modules = Build.cythonize(cython_ext_modules, force=True, compiler_directives={'profile': profile_cython}) ext_modules = kwargs.setdefault("ext_modules", []) ext_modules.extend(cython_ext_modules) From c15539f97d8a676d1a9228af5f0d1aac6e424826 Mon Sep 17 00:00:00 2001 From: Marco Treglia Date: Tue, 14 May 2024 11:24:51 +0200 Subject: [PATCH 2/2] vendor: Add support for CYTHON_TRACE environment variable This commit introduces the ability to enable line tracing in Cython for profiling purposes. This is controlled by the CYTHON_TRACE environment variable. When set, the 'linetrace' and 'binding' Cython directives are enabled, and the CYTHON_TRACE macro is defined. --- cython_setuptools/vendor.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/cython_setuptools/vendor.py b/cython_setuptools/vendor.py index 10f19a8..966dcfb 100644 --- a/cython_setuptools/vendor.py +++ b/cython_setuptools/vendor.py @@ -142,6 +142,12 @@ def setup(original_setup_file: str, cythonize: bool = True, **kwargs): PROFILE_CYTHON=1 python setup.py build_ext --inplace + In addition it's possible to add also the CYTHON_TRACE environment variable + to enable line tracing in Cython for profiling:: + ``CYTHON_TRACE`` environment variable:: + + CYTHON_TRACE=1 PROFILE_CYTHON=1 python setup.py build_ext --inplace`` + Debugging symbols can be added with:: DEBUG=1 python setup.py build_ext --inplace @@ -198,6 +204,15 @@ def create_cython_ext_modules(cython_modules, profile_cython=False, debug=False) if profile_cython: cython_directives = kwargs.setdefault("cython_directives", {}) cython_directives["profile"] = True + cython_trace = _str_to_bool(os.environ.get("CYTHON_TRACE", False)) + if cython_trace: + # Enable line tracing in Cython for profiling + cython_directives["linetrace"] = True + # Enable binding mode in Cython for C API access + cython_directives['binding'] = True + # Define the CYTHON_TRACE macro to enable tracing + define_macros = kwargs.setdefault("define_macros", []) + define_macros.append(('CYTHON_TRACE', '1')) if debug: for args_name in ("extra_compile_args", "extra_link_args"): args = kwargs.setdefault(args_name, [])