Skip to content

Commit

Permalink
Fix PEX environment setup.
Browse files Browse the repository at this point in the history
Previously, if a PEX had a custom interpreter, it was not threaded
through to the environments it activated. Add a test that the
designated interpreter is used.

Fixes #522
  • Loading branch information
jsirois committed Jul 27, 2018
1 parent d1f946c commit 427ae59
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pex/pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def _activate(self):
pex_info = self._pex_info.copy()
pex_info.update(self._pex_info_overrides)
pex_info.merge_pex_path(self._vars.PEX_PATH)
self._envs.append(PEXEnvironment(self._pex, pex_info))
self._envs.append(PEXEnvironment(self._pex, pex_info, interpreter=self._interpreter))
# N.B. by this point, `pex_info.pex_path` will contain a single pex path
# merged from pex_path in `PEX-INFO` and `PEX_PATH` set in the environment.
# `PEX_PATH` entries written into `PEX-INFO` take precedence over those set
Expand All @@ -83,7 +83,7 @@ def _activate(self):
for pex_path in filter(None, pex_info.pex_path.split(os.pathsep)):
pex_info = PexInfo.from_pex(pex_path)
pex_info.update(self._pex_info_overrides)
self._envs.append(PEXEnvironment(pex_path, pex_info))
self._envs.append(PEXEnvironment(pex_path, pex_info, interpreter=self._interpreter))

# activate all of them
for env in self._envs:
Expand Down
4 changes: 2 additions & 2 deletions pex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ def write_zipfile(directory, dest, reverse=False):

@contextlib.contextmanager
def make_installer(name='my_project', version='0.0.0', installer_impl=EggInstaller, zip_safe=True,
install_reqs=None):
install_reqs=None, **kwargs):
interp = {'project_name': name,
'version': version,
'zip_safe': zip_safe,
'install_requires': install_reqs or []}
with temporary_content(PROJECT_CONTENT, interp=interp) as td:
yield installer_impl(td)
yield installer_impl(td, **kwargs)


@contextlib.contextmanager
Expand Down
31 changes: 30 additions & 1 deletion tests/test_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import pytest
from twitter.common.contextutil import temporary_file

from pex.compatibility import WINDOWS, nested, to_bytes
from pex.bin.pex import get_interpreter
from pex.compatibility import PY2, WINDOWS, nested, to_bytes
from pex.installer import EggInstaller, WheelInstaller
from pex.pex import PEX
from pex.pex_builder import PEXBuilder
from pex.testing import (
IS_PYPY,
ensure_python_interpreter,
make_installer,
named_temporary_file,
run_simple_pex_test,
Expand Down Expand Up @@ -309,3 +312,29 @@ def test_pex_verify_entry_point_module_should_fail():
PEX(pex_builder.path(),
interpreter=pex_builder.interpreter,
verify_entry_point=True)


@pytest.mark.skipif(IS_PYPY)
def test_activate_interpreter_different_from_current():
with temporary_dir() as interpreter_cache:
interp_version = '3.6.3' if PY2 else '2.7.10'
custom_interpreter = get_interpreter(
python_interpreter=ensure_python_interpreter(interp_version),
interpreter_cache_dir=interpreter_cache,
repos=None, # Default to PyPI.
use_wheel=True
)
with make_installer(installer_impl=WheelInstaller,
interpreter=custom_interpreter) as installer:
with temporary_dir() as chroot:
pex_builder = PEXBuilder(path=chroot, interpreter=custom_interpreter)
bdist = installer.bdist()
pex_builder.add_dist_location(bdist)
pex_builder.set_entry_point('sys:exit')
pex_builder.freeze()

pex = PEX(pex_builder.path(), interpreter=custom_interpreter)
try:
pex._activate()
except SystemExit as e:
pytest.fail('PEX activation of %s failed with %s' % (pex, e))

0 comments on commit 427ae59

Please sign in to comment.