Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_pex_exec_with_pex_python_path_only():
with temporary_dir() as td:
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
# set pex python path
pex_python_path = ':'.join([
ensure_python_interpreter(PY27),
ensure_python_interpreter(PY36)
])
pexrc.write("PEX_PYTHON_PATH=%s" % pex_python_path)
pex_out_path = os.path.join(td, 'pex.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'-o', pex_out_path])
res.assert_success()
# test that pex bootstrapper selects lowest version interpreter
# in pex python path (python2.7)
stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 0
assert str(pex_python_path.split(':')[0]).encode() in stdout
def test_pex_exec_with_pex_python_path_and_pex_python_but_no_constraints():
with temporary_dir() as td:
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
# set both PPP and PP
pex_python_path = ':'.join([
ensure_python_interpreter(PY27),
ensure_python_interpreter(PY36)
])
pexrc.write("PEX_PYTHON_PATH=%s\n" % pex_python_path)
pex_python = '/path/to/some/python'
pexrc.write("PEX_PYTHON=%s" % pex_python)
pex_out_path = os.path.join(td, 'pex.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'-o', pex_out_path])
res.assert_success()
# test that pex bootstrapper selects lowest version interpreter
# in pex python path (python2.7)
stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 0
def test_interpreter_resolution_pex_python_path_precedence_over_pex_python():
with temporary_dir() as td:
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
# set both PPP and PP
pex_python_path = ':'.join([
ensure_python_interpreter(PY27),
ensure_python_interpreter(PY36)
])
pexrc.write("PEX_PYTHON_PATH=%s\n" % pex_python_path)
pex_python = '/path/to/some/python'
pexrc.write("PEX_PYTHON=%s" % pex_python)
pex_out_path = os.path.join(td, 'pex.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'--interpreter-constraint=>3,<3.8',
'-o', pex_out_path])
res.assert_success()
stdin_payload = b'import sys; print(sys.executable); sys.exit(0)'
stdout, rc = run_simple_pex(pex_out_path, stdin=stdin_payload)
assert rc == 0
correct_interpreter_path = pex_python_path.split(':')[1].encode()
def test_find_compatible_interpreters():
py27 = ensure_python_interpreter(PY27)
py35 = ensure_python_interpreter(PY35)
py36 = ensure_python_interpreter(PY36)
path = [py27, py35, py36]
assert [py35, py36] == find_interpreters(path, '>3')
assert [py27] == find_interpreters(path, '<3')
assert [py36] == find_interpreters(path, '>{}'.format(PY35))
assert [py35] == find_interpreters(path, '>{}, <{}'.format(PY27, PY36))
assert [py36] == find_interpreters(path, '>=3.6')
assert [] == find_interpreters(path, '<2')
assert [] == find_interpreters(path, '>4')
assert [] == find_interpreters(path, '>{}, <{}'.format(PY27, PY35))
# All interpreters on PATH including whatever interpreter is currently running.
all_known_interpreters = set(PythonInterpreter.all())
def test_interpreter_constraints_to_pex_info_py3():
py3_interpreter = ensure_python_interpreter(PY36)
with temporary_dir() as output_dir:
# target python 3
pex_out_path = os.path.join(output_dir, 'pex_py3.pex')
res = run_pex_command(['--disable-cache', '--interpreter-constraint=>3', '-o', pex_out_path],
env=make_env(PATH=os.path.dirname(py3_interpreter)))
res.assert_success()
pex_info = PexInfo.from_pex(pex_out_path)
assert ['>3'] == pex_info.interpreter_constraints
def test_interpreter_resolution_with_pex_python_path():
with temporary_dir() as td:
pexrc_path = os.path.join(td, '.pexrc')
with open(pexrc_path, 'w') as pexrc:
# set pex python path
pex_python_path = ':'.join([
ensure_python_interpreter(PY27),
ensure_python_interpreter(PY36)
])
pexrc.write("PEX_PYTHON_PATH=%s" % pex_python_path)
# constraints to build pex cleanly; PPP + pex_bootstrapper.py
# will use these constraints to override sys.executable on pex re-exec
interpreter_constraint1 = '>3' if sys.version_info[0] == 3 else '<3'
interpreter_constraint2 = '<3.8' if sys.version_info[0] == 3 else '>=2.7'
pex_out_path = os.path.join(td, 'pex.pex')
res = run_pex_command(['--disable-cache',
'--rcfile=%s' % pexrc_path,
'--interpreter-constraint=%s,%s' % (interpreter_constraint1, interpreter_constraint2),
'-o', pex_out_path])
res.assert_success()
def test_resolve_current_platform(p537_resolve_cache):
resolve_current = functools.partial(resolve_p537_wheel_names,
cache=p537_resolve_cache,
platforms=['current'])
other_python_version = PY36 if PY_VER == (3, 5) else PY35
other_python = PythonInterpreter.from_binary(ensure_python_interpreter(other_python_version))
current_python = PythonInterpreter.get()
resolved_other = resolve_current(interpreters=[other_python])
resolved_current = resolve_current()
assert 1 == len(resolved_other)
assert 1 == len(resolved_current)
assert resolved_other != resolved_current
assert resolved_current == resolve_current(interpreters=[current_python])
assert resolved_current == resolve_current(interpreters=[current_python, current_python])
# Here we have 2 local interpreters satisfying current but with different platforms and thus
# different dists for 2 total dists.
assert 2 == len(resolve_current(interpreters=[current_python, other_python]))
def test_setup_python_multiple_transitive_markers():
py27_interpreter = ensure_python_interpreter(PY27)
py36_interpreter = ensure_python_interpreter(PY36)
with temporary_dir() as out:
pex = os.path.join(out, 'pex.pex')
results = run_pex_command(['jsonschema==2.6.0',
'--disable-cache',
'--python-shebang=#!/usr/bin/env python',
'--python={}'.format(py27_interpreter),
'--python={}'.format(py36_interpreter),
'-o', pex])
results.assert_success()
pex_program = [pex, '-c']
py2_only_program = pex_program + ['import functools32']
both_program = pex_program + [
'import jsonschema, os, sys; print(os.path.realpath(sys.executable))'
]
def python_35_interpreter():
# Python 3.5 supports implicit namespace packages.
return PythonInterpreter.from_binary(ensure_python_interpreter(PY35))
def test_pex_reexec_constraints_dont_match_current_pex_python_path_min_py_version_selected():
py36_interpreter = ensure_python_interpreter(PY36)
py27_interpreter = ensure_python_interpreter(PY27)
_assert_exec_chain(exec_chain=[py27_interpreter],
pex_python_path=[py36_interpreter, py27_interpreter])