From 89737094622e73e49cae73dd62e676cc40e9809b Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Thu, 16 Oct 2025 01:28:10 +0200 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=93=A6Only=20enable=20line=20tracing?= =?UTF-8?q?=20when=20w/Cython=20tracing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch modifies the build backend to dynamically enable Cython line tracing only when explicitly requested via the `with-cython-tracing=true` config setting. Previously, having `linetrace = "True"` in `pyproject.toml` was making our PyPI-published wheels slower. Now, line tracing is opt-in: - Regular builds: `pip install .` (no line tracing) - Tracing builds: `pip install . --config-setting=with-cython-tracing=true` (enables line tracing) When tracing is requested, the build backend automatically adds the `linetrace=True` and `profile=True` Cython directives and sets the appropriate C compiler flags. Resolves #767 --- packaging/pep517_backend/_backend.py | 1 + .../pep517_backend/_cython_configuration.py | 25 ++++++++++++++++++- packaging/pep517_backend/_transformers.py | 2 +- pyproject.toml | 2 -- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/packaging/pep517_backend/_backend.py b/packaging/pep517_backend/_backend.py index 41884c44e..4e7a1a5ca 100644 --- a/packaging/pep517_backend/_backend.py +++ b/packaging/pep517_backend/_backend.py @@ -261,6 +261,7 @@ def _prebuild_c_extensions( cythonize_args = _make_cythonize_cli_args_from_config( config, + cython_line_tracing_requested=cython_line_tracing_requested, ) with _patched_cython_env( config['env'], diff --git a/packaging/pep517_backend/_cython_configuration.py b/packaging/pep517_backend/_cython_configuration.py index fb6de27bb..810a83d84 100644 --- a/packaging/pep517_backend/_cython_configuration.py +++ b/packaging/pep517_backend/_cython_configuration.py @@ -144,14 +144,37 @@ def get_local_cythonize_config() -> Config: return config_mapping['tool']['local']['cythonize'] # type: ignore[no-any-return] +def _configure_cython_line_tracing( + config_kwargs: dict[str, str | dict[str, str]], + *, + cython_line_tracing_requested: bool, +) -> None: + """Configure Cython line tracing directives if requested.""" + # If line tracing is requested, add it to the directives + if cython_line_tracing_requested: + directives = config_kwargs.setdefault('directive', {}) + assert isinstance(directives, dict) # noqa: S101 # typing + directives['linetrace'] = 'True' + directives['profile'] = 'True' + + def make_cythonize_cli_args_from_config( config: Config, + *, + cython_line_tracing_requested: bool = False, ) -> list[str]: """Compose ``cythonize`` CLI args from config.""" py_ver_arg = f'-{_python_version_tuple.major!s}' cli_flags = get_enabled_cli_flags_from_config(config['flags']) - cli_kwargs = get_cli_kwargs_from_config(config['kwargs']) + config_kwargs = config['kwargs'] + + _configure_cython_line_tracing( + config_kwargs, + cython_line_tracing_requested=cython_line_tracing_requested, + ) + + cli_kwargs = get_cli_kwargs_from_config(config_kwargs) return cli_flags + [py_ver_arg] + cli_kwargs + ['--'] + config['src'] diff --git a/packaging/pep517_backend/_transformers.py b/packaging/pep517_backend/_transformers.py index 10b1a3efb..3c25c719e 100644 --- a/packaging/pep517_backend/_transformers.py +++ b/packaging/pep517_backend/_transformers.py @@ -25,7 +25,7 @@ def _emit_opt_pairs( yield '='.join(map(str, (flag_opt, *pair))) -def get_cli_kwargs_from_config( +def get_cli_kwargs_from_config( # noqa: WPS234 kwargs_map: dict[str, str | dict[str, str]], ) -> list[str]: """Make a list of options with values from config.""" diff --git a/pyproject.toml b/pyproject.toml index 7da743420..6779c00e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -113,8 +113,6 @@ parallel = 1 # Ref: https://github.com/cython/cython/blob/d6e6de9/Cython/Compiler/Options.py#L170-L242 embedsignature = "True" emit_code_comments = "True" -linetrace = "True" -profile = "True" [tool.local.cythonize.kwargs.compile-time-env] # This section can contain compile time env vars From 789ad28f83bdd6239c0e04ef1d974fc0754190bc Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Thu, 16 Oct 2025 01:37:37 +0200 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9D=20Add=20a=20change=20note=20fo?= =?UTF-8?q?r=20PR=20#778?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/changelog-fragments/778.packaging.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 docs/changelog-fragments/778.packaging.rst diff --git a/docs/changelog-fragments/778.packaging.rst b/docs/changelog-fragments/778.packaging.rst new file mode 100644 index 000000000..512c650be --- /dev/null +++ b/docs/changelog-fragments/778.packaging.rst @@ -0,0 +1,18 @@ +The build backend now dynamically enables Cython line tracing only when +explicitly requested via the ``with-cython-tracing=true`` config setting. +Previously, having ``linetrace = "True"`` in :file:`pyproject.toml` was +making our PyPI-published wheels slower due to debug symbols. + +Now, line tracing is truly opt-in: + +- Regular builds: ``pip install .`` (no line tracing) +- Tracing builds: + ``pip install . --config-setting=with-cython-tracing=true`` + + (enables line tracing) + +When tracing is requested, the build backend automatically adds the +``linetrace=True`` and ``profile=True`` Cython directives and sets the +appropriate C compiler flags. + +-- by :user:`webknjaz` From f8121c554576e8880d96f9892fda0dbea7166cbd Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Mon, 20 Oct 2025 14:52:41 +0200 Subject: [PATCH 3/3] Disable profiling to test changes in PR --- .github/workflows/ci-cd.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 2e0d449df..47d66abe5 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -149,8 +149,7 @@ jobs: ) && true || false }} - profiling-enabled: >- - ${{ steps.profiling-check.outputs.profiling-enabled || false }} + profiling-enabled: false cache-key-for-dep-files: >- ${{ steps.calc-cache-key-files.outputs.cache-key-for-dep-files }} git-tag: ${{ steps.git-tag.outputs.tag }}