Skip to content

Commit 8eb8386

Browse files
committed
📦Only enable line tracing when w/Cython tracing
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 ansible#767
1 parent bc6dba2 commit 8eb8386

File tree

4 files changed

+30
-6
lines changed

4 files changed

+30
-6
lines changed

packaging/pep517_backend/_backend.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,10 @@ def _prebuild_c_extensions(
256256
with build_dir_ctx:
257257
config = _get_local_cython_config()
258258

259-
cythonize_args = _make_cythonize_cli_args_from_config(config)
259+
cythonize_args = _make_cythonize_cli_args_from_config(
260+
config,
261+
cython_line_tracing_requested,
262+
)
260263
with _patched_cython_env(config['env'], cython_line_tracing_requested):
261264
_cythonize_cli_cmd(cythonize_args)
262265
with patched_distutils_cmd_install():

packaging/pep517_backend/_cython_configuration.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,32 @@ def get_local_cythonize_config() -> dict:
129129
return config_mapping['tool']['local']['cythonize']
130130

131131

132-
def make_cythonize_cli_args_from_config(config: dict) -> 'list[str]':
132+
def _configure_cython_line_tracing(
133+
config_kwargs: dict[str, str | dict[str, str]],
134+
cython_line_tracing_requested: bool,
135+
) -> None:
136+
"""Configure Cython line tracing directives if requested."""
137+
# If line tracing is requested, add it to the directives
138+
if cython_line_tracing_requested:
139+
directives = config_kwargs.setdefault('directive', {})
140+
assert isinstance(directives, dict) # Type narrowing for mypy
141+
directives['linetrace'] = 'True'
142+
directives['profile'] = 'True'
143+
144+
145+
def make_cythonize_cli_args_from_config(
146+
config: dict,
147+
cython_line_tracing_requested: bool = False,
148+
) -> 'list[str]':
133149
"""Compose ``cythonize`` CLI args from config."""
134150
py_ver_arg = f'-{_python_version_tuple.major!s}' # noqa: WPS305
135151

136152
cli_flags = get_enabled_cli_flags_from_config(config['flags'])
137-
cli_kwargs = get_cli_kwargs_from_config(config['kwargs'])
153+
config_kwargs = config['kwargs']
154+
155+
_configure_cython_line_tracing(config_kwargs, cython_line_tracing_requested)
156+
157+
cli_kwargs = get_cli_kwargs_from_config(config_kwargs)
138158

139159
return cli_flags + [py_ver_arg] + cli_kwargs + ['--'] + config['src']
140160

packaging/pep517_backend/_transformers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from itertools import chain
66
from re import sub as _substitute_with_regexp
7+
from typing import Union
78

89

910
def _emit_opt_pairs(opt_pair):
@@ -20,7 +21,9 @@ def _emit_opt_pairs(opt_pair):
2021
)
2122

2223

23-
def get_cli_kwargs_from_config(kwargs_map):
24+
def get_cli_kwargs_from_config( # noqa: WPS234
25+
kwargs_map: dict[str, Union[str, dict[str, str]]],
26+
) -> list[str]:
2427
"""Make a list of options with values from config."""
2528
return list(chain.from_iterable(map(_emit_opt_pairs, kwargs_map.items())))
2629

pyproject.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ parallel = 1
114114
# Ref: https://github.com/cython/cython/blob/d6e6de9/Cython/Compiler/Options.py#L170-L242
115115
embedsignature = "True"
116116
emit_code_comments = "True"
117-
linetrace = "True"
118-
profile = "True"
119117

120118
[tool.local.cythonize.kwargs.compile-time-env]
121119
# This section can contain compile time env vars

0 commit comments

Comments
 (0)