From cb36fa90f9fbd0d9e79fec88b34574cf1d19f338 Mon Sep 17 00:00:00 2001 From: Stefano Rivera Date: Wed, 20 Apr 2022 17:30:31 -0400 Subject: [PATCH] env: handle Debian's posix_local scheme The issue and solution is the same as OSX. The venv scheme wasn't standardised yet, so it's not using it yet. --- src/build/env.py | 7 +++++++ tests/test_env.py | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/build/env.py b/src/build/env.py index c558f703..4803613d 100644 --- a/src/build/env.py +++ b/src/build/env.py @@ -307,6 +307,13 @@ def _find_executable_and_scripts(path: str) -> Tuple[str, str, str]: # See https://bugs.python.org/issue45413 # and https://github.com/pypa/virtualenv/issues/2208 paths = sysconfig.get_paths(scheme='venv', vars=config_vars) + elif 'posix_local' in scheme_names: + # The Python that ships on Debian/Ubuntu varies the default scheme to + # install to /usr/local + # But it does not (yet) set the "venv" scheme. + # If we're the Debian "posix_local" scheme is available, but "venv" + # is not, we use "posix_prefix" instead which is venv-compatible there. + paths = sysconfig.get_paths(scheme='posix_prefix', vars=config_vars) elif 'osx_framework_library' in scheme_names: # The Python that ships with the macOS developer tools varies the # default scheme depending on whether the ``sys.prefix`` is part of a framework. diff --git a/tests/test_env.py b/tests/test_env.py index e8fa7c5a..37446ca7 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -5,6 +5,7 @@ import platform import subprocess import sys +import sysconfig import pytest @@ -56,6 +57,17 @@ def test_can_get_venv_paths_with_conflicting_default_scheme(mocker): assert get_scheme_names.call_count == 1 +@pytest.mark.skipif('posix_local' not in sysconfig.get_scheme_names(), reason='workaround for Debian/Ubuntu Python') +def test_can_get_venv_paths_with_posix_local_default_scheme(mocker): + get_paths = mocker.spy(sysconfig, 'get_paths') + # We should never call this, but we patch it to ensure failure if we do + get_default_scheme = mocker.patch('sysconfig.get_default_scheme', return_value='posix_local') + with build.env.IsolatedEnvBuilder(): + pass + get_paths.assert_called_once_with(scheme='posix_prefix', vars=mocker.ANY) + assert get_default_scheme.call_count == 0 + + def test_executable_missing_post_creation(mocker): venv_create = mocker.patch('venv.EnvBuilder.create') with pytest.raises(RuntimeError, match='Virtual environment creation failed, executable .* missing'):