Skip to content

Commit e439d85

Browse files
committed
Forward warnings option to subprocess
1 parent e89c52f commit e439d85

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/pyproject_hooks/_impl.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import tempfile
55
from contextlib import contextmanager
6+
from itertools import chain
67
from os.path import abspath
78
from os.path import join as pjoin
89
from subprocess import STDOUT, check_call, check_output
@@ -380,6 +381,15 @@ def build_sdist(
380381
},
381382
)
382383

384+
def _get_warnopts(self) -> Iterator[str]:
385+
"""
386+
Reconstruct Python's warn options that are active for the current process,
387+
so that it can be forwarded to a subprocess
388+
"""
389+
# `sys.warnoptions` is documented/mentioned in
390+
# https://docs.python.org/3.13/library/warnings.html#describing-warning-filters
391+
return chain.from_iterable(("-W", opt) for opt in sys.warnoptions)
392+
383393
def _call_hook(self, hook_name: str, kwargs: Mapping[str, Any]) -> Any:
384394
extra_environ = {"_PYPROJECT_HOOKS_BUILD_BACKEND": self.build_backend}
385395

@@ -394,8 +404,9 @@ def _call_hook(self, hook_name: str, kwargs: Mapping[str, Any]) -> Any:
394404
# Run the hook in a subprocess
395405
with _in_proc_script_path() as script:
396406
python = self.python_executable
407+
opts = self._get_warnopts()
397408
self._subprocess_runner(
398-
[python, abspath(str(script)), hook_name, td],
409+
[python, *opts, abspath(str(script)), hook_name, td],
399410
cwd=self.source_dir,
400411
extra_environ=extra_environ,
401412
)

tests/test_call_hooks.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import zipfile
55
from os.path import abspath, dirname
66
from os.path import join as pjoin
7+
from subprocess import CalledProcessError
78
from unittest.mock import Mock
89

910
import pytest
@@ -233,3 +234,25 @@ def test_warnings():
233234
with modified_env({"PYTHONPATH": BUILDSYS_PKGS}):
234235
with pytest.warns(BuildBackendWarning, match="this is my example warning"):
235236
hooks.get_requires_for_build_wheel({})
237+
238+
239+
def test_forward_warnopts(monkeypatch, recwarn, capsys):
240+
hooks = get_hooks("pkg-with-warnings")
241+
242+
monkeypatch.setattr(
243+
BuildBackendHookCaller, "_get_warnopts", lambda _: ["-W", "ignore"]
244+
)
245+
with modified_env({"PYTHONPATH": BUILDSYS_PKGS}):
246+
hooks.get_requires_for_build_wheel({})
247+
assert len(recwarn) == 0 # All subprocess warnings were ignored.
248+
249+
monkeypatch.setattr(
250+
BuildBackendHookCaller, "_get_warnopts", lambda _: ["-W", "error"]
251+
)
252+
with modified_env({"PYTHONPATH": BUILDSYS_PKGS}):
253+
with pytest.raises(CalledProcessError):
254+
# The `match` argument does not work against the exception in the
255+
# subprocess, but we have a sanity check in place: the same call
256+
# worked above when the warnings were ignored, so we known this is
257+
# caused by the new warnings filter.
258+
hooks.get_requires_for_build_wheel({})

0 commit comments

Comments
 (0)