Skip to content

Commit

Permalink
Fixup interpreter constraints.
Browse files Browse the repository at this point in the history
These were too narrow previously, we want to be able to use a 3.6 pex
on any 3.6 for example - with caveats that are out of scope for this PR.
  • Loading branch information
jsirois committed Apr 4, 2019
1 parent 4d8e682 commit d08fa8c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 37 deletions.
10 changes: 2 additions & 8 deletions build-support/bin/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -118,17 +118,11 @@ else
fi
export PY="${PY:-python${py_major_minor}}"

# Also set PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS. We set this to the exact Python version
# to resolve any potential ambiguity when multiple Python interpreters are discoverable, such as
# Python 2.7.10 vs. 2.7.13. When running with Python 3, we must also set this constraint to ensure
# all spawned subprocesses use Python 3 rather than the default of Python 2. This is in part
# necessary to avoid the _Py_Dealloc error (#6985).
py_major_minor_patch=$(${PY} -c 'import sys; print(".".join(map(str, sys.version_info[0:3])))')
export PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS="${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS:-['CPython==${py_major_minor_patch}']}"
export PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS="['CPython==${py_major_minor}.*']"
banner "Setting interpreter constraints to ${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS}"

if [[ "${run_bootstrap:-false}" == "true" ]]; then
start_travis_section "Bootstrap" "Bootstrapping pants as a Python ${py_major_minor_patch} PEX"
start_travis_section "Bootstrap" "Bootstrapping pants as a Python ${py_major_minor} PEX"
(
if [[ "${run_bootstrap_clean:-false}" == "true" ]]; then
./build-support/python/clean.sh || die "Failed to clean before bootstrapping pants."
Expand Down
3 changes: 1 addition & 2 deletions build-support/bin/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ fi
# to resolve any potential ambiguity when multiple Python interpreters are discoverable, such as
# Python 2.7.13 vs. 2.7.15. We must also set this when running with Python 3 to ensure
# that spawned subprocesses use Python 3.
py_major_minor_patch=$(${PY} -c 'import sys; print(".".join(map(str, sys.version_info[0:3])))')
export PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS="${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS:-['CPython==${py_major_minor_patch}']}"
export PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS="['CPython==${py_major_minor}.*']"

function run_local_pants() {
${ROOT}/pants "$@"
Expand Down
38 changes: 16 additions & 22 deletions pants
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,37 @@

REPO_ROOT=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)

interpreter_constraints_sentinel_file_path='.python-interpreter-constraints'
interpreter_id_sentinel_file_path='.python-interpreter-constraints'

function clean_if_interpreter_constraints_changed() {
new_interpreter_constraints="$1"
function clean_if_interpreter_changed() {
new_interpreter_id="$1"

if [[ -n "${recursive_pants_shell_script_invocation:-}" ]]; then
return 0
fi

interpreter_constraints_file="${REPO_ROOT}/${interpreter_constraints_sentinel_file_path}"
interpreter_id_file="${REPO_ROOT}/${interpreter_id_sentinel_file_path}"

if [[ -f "${interpreter_constraints_file}" ]]; then
previous_interpreter_constraints="$(cat "${interpreter_constraints_file}")"
if [[ "${new_interpreter_constraints}" == "${previous_interpreter_constraints}" ]]; then
if [[ -f "${interpreter_id_file}" ]]; then
previous_interpreter_id="$(cat "${interpreter_id_file}")"
if [[ "${new_interpreter_id}" == "${previous_interpreter_id}" ]]; then
return 0
fi
else
cat >&2 <<EOF
${interpreter_constraints_sentinel_file_path} not found in the buildroot.
${interpreter_id_sentinel_file_path} not found in the buildroot.
Forcing an initial clean-all.
EOF
fi

export recursive_pants_shell_script_invocation='y'
cat >&2 <<EOF
Different interpreter constraints were set than were used in the previous Pants run.
Clearing the cache and preparing a Python environment with these interpreter constraints:
${new_interpreter_constraints}
A different interpreter is being used than in the previous Pants run.
Clearing the cache and preparing a Python environment with the new interpreter:
${new_interpreter_id}
EOF
./pants clean-all
echo "${new_interpreter_constraints}" > "${interpreter_constraints_file}"
echo "${new_interpreter_id}" > "${interpreter_id_file}"
unset recursive_pants_shell_script_invocation
}

Expand Down Expand Up @@ -94,18 +94,12 @@ source ${HERE}/build-support/bin/native/bootstrap_code.sh
# Default to using Python 3 if not otherwise specified.
export PY="${PY:-python3}"

# Set interpreter constraints to exactly match the interpreter used for the venv, if not already set.
# Note that $PY only impacts which virtualenv we use for the parent Pants process; we must also set
# PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS to constrain spawned subprocesses such as tests. Without
# any interpreter constraints, when PY is set to Python 3 we get the _Py_Dealloc exception (#6985) as
# Pants will try to use Python 2 for subprocesses. Without setting minor version constraints, when using Python 3
# we could end up using a different Python minor version for subprocesses than we do for Pants.
py_major_minor_patch=$(${PY} -c 'import sys; print(".".join(map(str, sys.version_info[0:3])))')
export PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS="${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS:-['CPython==${py_major_minor_patch}']}"

# We can currently assume the CI environment does not switch python versions within a shard.
if [[ "${ONLY_USING_SINGLE_PYTHON_VERSION:-false}" != 'true' ]]; then
clean_if_interpreter_constraints_changed "${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS}"
interpreter_id="$(
${PY} -c 'import os, sys; print("{}\n{}".format(sys.executable, sys.version))'
)"
clean_if_interpreter_changed "${interpreter_id}"
fi

PANTS_EXE="${HERE}/src/python/pants/bin/pants_loader.py"
Expand Down
5 changes: 0 additions & 5 deletions pants2
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,4 @@ REPO_ROOT=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd -P)

export PY="${PY:-python2.7}"

# Allow spawned subprocesses, such as unit tests, to execute with either Python 2 and Python 3.
# So long as the target does not have a compatibility constraint that requires only Python 3, the
# interpreter selection will default to using Python 2 as this is the minimum acceptable interpreter.
export PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS="${PANTS_PYTHON_SETUP_INTERPRETER_CONSTRAINTS:-['CPython>=2.7,<3','CPython>=3.6,<4']}"

exec ${REPO_ROOT}/pants "$@"

0 comments on commit d08fa8c

Please sign in to comment.