From a61aa16ef19be91dc2f678fee0422d417f9666ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filipe=20La=C3=ADns?= Date: Tue, 15 Sep 2020 19:55:03 +0100 Subject: [PATCH] allow using a custom Python interpreter to run the PEP517 hooks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Filipe LaĆ­ns --- pep517/wrappers.py | 7 ++++++- tests/test_call_hooks.py | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/pep517/wrappers.py b/pep517/wrappers.py index 85f574b..5ac8ddd 100644 --- a/pep517/wrappers.py +++ b/pep517/wrappers.py @@ -116,6 +116,7 @@ class Pep517HookCaller(object): pyproject.toml. backend_path : The backend path, as per PEP 517, from pyproject.toml. runner : A callable that invokes the wrapper subprocess. + python_executable : The Python executable used to invoke the backend The 'runner', if provided, must expect the following: cmd : a list of strings representing the command and arguments to @@ -131,6 +132,7 @@ def __init__( build_backend, backend_path=None, runner=None, + python_executable=None, ): if runner is None: runner = default_subprocess_runner @@ -143,6 +145,9 @@ def __init__( ] self.backend_path = backend_path self._subprocess_runner = runner + if not python_executable: + python_executable = sys.executable + self.python_executable = python_executable @contextmanager def subprocess_runner(self, runner): @@ -262,7 +267,7 @@ def _call_hook(self, hook_name, kwargs): # Run the hook in a subprocess with _in_proc_script_path() as script: self._subprocess_runner( - [sys.executable, str(script), hook_name, td], + [self.python_executable, str(script), hook_name, td], cwd=self.source_dir, extra_environ=extra_environ ) diff --git a/tests/test_call_hooks.py b/tests/test_call_hooks.py index 8beed34..4ea53eb 100644 --- a/tests/test_call_hooks.py +++ b/tests/test_call_hooks.py @@ -6,12 +6,18 @@ import pytest import toml import zipfile +import sys from mock import Mock from pep517.wrappers import Pep517HookCaller, default_subprocess_runner from pep517.wrappers import UnsupportedOperation, BackendUnavailable + +if sys.version_info[0] == 2: + FileNotFoundError = IOError + + SAMPLES_DIR = pjoin(dirname(abspath(__file__)), 'samples') BUILDSYS_PKGS = pjoin(SAMPLES_DIR, 'buildsys_pkgs') @@ -131,3 +137,17 @@ def test_runner_replaced_on_exception(monkeypatch): hooks.get_requires_for_build_wheel() runner.assert_called_once() + + +def test_custom_python_executable(monkeypatch, tmpdir): + monkeypatch.setenv('PYTHONPATH', BUILDSYS_PKGS) + + runner = Mock(autospec=default_subprocess_runner) + hooks = get_hooks('pkg1', runner=runner, python_executable='some-python') + + with hooks.subprocess_runner(runner): + with pytest.raises(FileNotFoundError): + # output.json is missing because we didn't actually run the hook + hooks.get_requires_for_build_wheel() + runner.assert_called_once() + assert runner.call_args[0][0][0] == 'some-python'