Skip to content

Commit 3b7538b

Browse files
committed
env: override default scheme for Apple Python venv
1 parent 665918f commit 3b7538b

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

src/build/env.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,17 @@ def _find_executable_and_scripts(path): # type: (str) -> Tuple[str, str, str]
246246
"""
247247
config_vars = sysconfig.get_config_vars().copy() # globally cached, copy before altering it
248248
config_vars['base'] = path
249-
env_scripts = sysconfig.get_path('scripts', vars=config_vars)
250-
if not env_scripts:
251-
raise RuntimeError("Couldn't get environment scripts path")
252-
exe = 'pypy3' if platform.python_implementation() == 'PyPy' else 'python'
253-
if os.name == 'nt':
254-
exe = '{}.exe'.format(exe)
255-
executable = os.path.join(env_scripts, exe)
249+
is_apple_python = 'osx_framework_library' in sysconfig.get_scheme_names()
250+
paths = (
251+
sysconfig.get_paths(scheme='posix_prefix', vars=config_vars)
252+
if is_apple_python
253+
else sysconfig.get_paths(vars=config_vars)
254+
)
255+
executable = os.path.join(paths['scripts'], 'python.exe' if os.name == 'nt' else 'python')
256256
if not os.path.exists(executable):
257257
raise RuntimeError('Virtual environment creation failed, executable {} missing'.format(executable))
258258

259-
purelib = sysconfig.get_path('purelib', vars=config_vars)
260-
if not purelib:
261-
raise RuntimeError("Couldn't get environment purelib folder")
262-
return executable, env_scripts, purelib
259+
return executable, paths['scripts'], paths['purelib']
263260

264261

265262
__all__ = (

tests/test_env.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,31 @@ def test_isolated_environment_install(mocker):
5151
]
5252

5353

54+
@pytest.mark.skipif(IS_PY2, reason='venv module used on Python 3 only')
55+
@pytest.mark.skipif(sys.platform != 'darwin', reason='workaround for Apple Python')
56+
def test_can_get_venv_paths_with_conflicting_default_scheme(mocker):
57+
mocker.patch.object(build.env, 'virtualenv', None)
58+
get_scheme_names = mocker.patch('sysconfig.get_scheme_names', return_value=('osx_framework_library',))
59+
with build.env.IsolatedEnvBuilder():
60+
pass
61+
assert get_scheme_names.call_count == 1
62+
63+
5464
@pytest.mark.skipif(IS_PY2, reason='venv module used on Python 3 only')
5565
@pytest.mark.skipif(IS_PYPY3, reason='PyPy3 uses get path to create and provision venv')
5666
def test_executable_missing_post_creation(mocker):
5767
mocker.patch.object(build.env, 'virtualenv', None)
58-
original_get_path = sysconfig.get_path
68+
original_get_paths = sysconfig.get_paths
5969

60-
def _get_path(name, vars): # noqa
70+
def _get_paths(vars): # noqa
6171
shutil.rmtree(vars['base'])
62-
return original_get_path(name, vars=vars)
72+
return original_get_paths(vars=vars)
6373

64-
get_path = mocker.patch('sysconfig.get_path', side_effect=_get_path)
74+
get_paths = mocker.patch('sysconfig.get_paths', side_effect=_get_paths)
6575
with pytest.raises(RuntimeError, match='Virtual environment creation failed, executable .* missing'):
6676
with build.env.IsolatedEnvBuilder():
6777
pass
68-
assert get_path.call_count == 1
78+
assert get_paths.call_count == 1
6979

7080

7181
def test_isolated_env_abstract():

0 commit comments

Comments
 (0)