Skip to content

Commit 46a3917

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 8679809 commit 46a3917

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

packaging/pep517_backend/_backend.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def _prebuild_c_extensions(
261261

262262
cythonize_args = _make_cythonize_cli_args_from_config(
263263
config,
264+
cython_line_tracing_requested=cython_line_tracing_requested,
264265
)
265266
with _patched_cython_env(
266267
config['env'],

packaging/pep517_backend/_cython_configuration.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,37 @@ def get_local_cythonize_config() -> Config:
144144
return config_mapping['tool']['local']['cythonize'] # type: ignore[no-any-return]
145145

146146

147+
def _configure_cython_line_tracing(
148+
config_kwargs: dict[str, str | dict[str, str]],
149+
*,
150+
cython_line_tracing_requested: bool,
151+
) -> None:
152+
"""Configure Cython line tracing directives if requested."""
153+
# If line tracing is requested, add it to the directives
154+
if cython_line_tracing_requested:
155+
directives = config_kwargs.setdefault('directive', {})
156+
assert isinstance(directives, dict) # noqa: S101 # typing
157+
directives['linetrace'] = 'True'
158+
directives['profile'] = 'True'
159+
160+
147161
def make_cythonize_cli_args_from_config(
148162
config: Config,
163+
*,
164+
cython_line_tracing_requested: bool = False,
149165
) -> list[str]:
150166
"""Compose ``cythonize`` CLI args from config."""
151167
py_ver_arg = f'-{_python_version_tuple.major!s}'
152168

153169
cli_flags = get_enabled_cli_flags_from_config(config['flags'])
154-
cli_kwargs = get_cli_kwargs_from_config(config['kwargs'])
170+
config_kwargs = config['kwargs']
171+
172+
_configure_cython_line_tracing(
173+
config_kwargs,
174+
cython_line_tracing_requested=cython_line_tracing_requested,
175+
)
176+
177+
cli_kwargs = get_cli_kwargs_from_config(config_kwargs)
155178

156179
return cli_flags + [py_ver_arg] + cli_kwargs + ['--'] + config['src']
157180

packaging/pep517_backend/_transformers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def _emit_opt_pairs(
2525
yield '='.join(map(str, (flag_opt, *pair)))
2626

2727

28-
def get_cli_kwargs_from_config(
28+
def get_cli_kwargs_from_config( # noqa: WPS234
2929
kwargs_map: dict[str, str | dict[str, str]],
3030
) -> list[str]:
3131
"""Make a list of options with values from config."""

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)