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 00d219e commit ff01ec0
Show file tree
Hide file tree
Showing 3 changed files with 39 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
5 changes: 3 additions & 2 deletions pex/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,12 @@ def make_sdist(name='my_project', version='0.0.0', zip_safe=True, install_reqs=N

@contextlib.contextmanager
def make_bdist(name='my_project', version='0.0.0', installer_impl=EggInstaller, zipped=False,
zip_safe=True):
zip_safe=True, **kwargs):
with make_installer(name=name,
version=version,
installer_impl=installer_impl,
zip_safe=zip_safe) as installer:
zip_safe=zip_safe,
**kwargs) as installer:
dist_location = installer.bdist()
if zipped:
yield DistributionHelper.distribution_from_path(dist_location)
Expand Down
35 changes: 34 additions & 1 deletion tests/test_pex.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@
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.pex_info import PexInfo
from pex.testing import (
IS_PYPY,
ensure_python_interpreter,
make_bdist,
make_installer,
named_temporary_file,
run_simple_pex_test,
Expand Down Expand Up @@ -309,3 +314,31 @@ 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 pex_root:
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=os.path.join(pex_root, 'interpreters'),
repos=None, # Default to PyPI.
use_wheel=True
)
pex_info = PexInfo.default(custom_interpreter)
pex_info.pex_root = pex_root
with temporary_dir() as pex_chroot:
pex_builder = PEXBuilder(path=pex_chroot,
interpreter=custom_interpreter,
pex_info=pex_info)
with make_bdist(installer_impl=WheelInstaller, interpreter=custom_interpreter) as bdist:
pex_builder.add_distribution(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 ff01ec0

Please sign in to comment.