Skip to content

Commit beda146

Browse files
committed
allow using a custom Python interpreter to run the PEP517 hooks
Signed-off-by: Filipe Laíns <[email protected]>
1 parent b5d5c20 commit beda146

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

pep517/wrappers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class Pep517HookCaller(object):
116116
pyproject.toml.
117117
backend_path : The backend path, as per PEP 517, from pyproject.toml.
118118
runner : A callable that invokes the wrapper subprocess.
119+
python_executable : The Python executable used to invoke the backend
119120
120121
The 'runner', if provided, must expect the following:
121122
cmd : a list of strings representing the command and arguments to
@@ -131,6 +132,7 @@ def __init__(
131132
build_backend,
132133
backend_path=None,
133134
runner=None,
135+
python_executable=None,
134136
):
135137
if runner is None:
136138
runner = default_subprocess_runner
@@ -143,6 +145,9 @@ def __init__(
143145
]
144146
self.backend_path = backend_path
145147
self._subprocess_runner = runner
148+
if not python_executable:
149+
python_executable = sys.executable
150+
self.python_executable = python_executable
146151

147152
@contextmanager
148153
def subprocess_runner(self, runner):
@@ -262,7 +267,7 @@ def _call_hook(self, hook_name, kwargs):
262267
# Run the hook in a subprocess
263268
with _in_proc_script_path() as script:
264269
self._subprocess_runner(
265-
[sys.executable, str(script), hook_name, td],
270+
[self.python_executable, str(script), hook_name, td],
266271
cwd=self.source_dir,
267272
extra_environ=extra_environ
268273
)

tests/test_call_hooks.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,17 @@ def test_runner_replaced_on_exception(monkeypatch):
131131

132132
hooks.get_requires_for_build_wheel()
133133
runner.assert_called_once()
134+
135+
136+
def test_custom_python_executable(monkeypatch, tmpdir):
137+
monkeypatch.setenv('PYTHONPATH', BUILDSYS_PKGS)
138+
139+
runner = Mock(autospec=default_subprocess_runner)
140+
hooks = get_hooks('pkg1', runner=runner, python_executable='some-python')
141+
142+
with hooks.subprocess_runner(runner):
143+
with pytest.raises(FileNotFoundError):
144+
# output.json is missing because we didn't actually run the hook
145+
hooks.get_requires_for_build_wheel()
146+
runner.assert_called_once()
147+
assert runner.call_args[0][0][0] == 'some-python'

0 commit comments

Comments
 (0)