From ecd67c82231201a89cbd39834049c8d938ab9061 Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 29 Sep 2019 00:03:44 +0200 Subject: [PATCH] pyenv-exec: no `-a` with `exec` to keep $PATH with system version (#1169) Using `exec -a` caused Python to use $PATH to look up the full program name (for `sys.executable`), which 314937d then tried to fix by changing $PATH also for the system version. This is not necessary anymore when not using the short name with `exec`. This was rejected upstream (https://github.com/rbenv/rbenv/pull/1089#issuecomment-394531896), since it is not a problem with Ruby apparently. Uses $PYENV_ROOT to check if system version is used. Fixes https://github.com/pyenv/pyenv/issues/98. Fixes https://github.com/pyenv/pyenv/issues/789. --- libexec/pyenv-exec | 9 +++++---- test/exec.bats | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/libexec/pyenv-exec b/libexec/pyenv-exec index 51c668e3..26816271 100755 --- a/libexec/pyenv-exec +++ b/libexec/pyenv-exec @@ -41,7 +41,8 @@ for script in "${scripts[@]}"; do done shift 1 -# CPython's `sys.executable` requires the `PYENV_BIN_PATH` to be at the top of the `PATH`. -# https://github.com/pyenv/pyenv/issues/98 -export PATH="${PYENV_BIN_PATH}:${PATH}" -exec -a "$PYENV_COMMAND" "$PYENV_COMMAND_PATH" "$@" +if [ "${PYENV_BIN_PATH#${PYENV_ROOT}}" != "${PYENV_BIN_PATH}" ]; then + # Only add to $PATH for non-system version. + export PATH="${PYENV_BIN_PATH}:${PATH}" +fi +exec "$PYENV_COMMAND_PATH" "$@" diff --git a/test/exec.bats b/test/exec.bats index 73ae3a18..2a7f4856 100644 --- a/test/exec.bats +++ b/test/exec.bats @@ -77,3 +77,39 @@ ${PYENV_ROOT}/versions/3.4/bin/python args OUT } + +@test "sys.executable with system version (#98)" { + system_python=$(which python) + + PYENV_VERSION="custom" + create_executable "python" "" + unset PYENV_VERSION + + pyenv-rehash + run pyenv-exec python -c 'import sys; print(sys.executable)' + assert_success "${system_python}" +} + +@test '$PATH is not modified with system Python' { + # Create a wrapper executable that verifies PATH. + PYENV_VERSION="custom" + create_executable "python" '[[ "$PATH" == "${PYENV_TEST_DIR}/root/versions/custom/bin:"* ]] || { echo "unexpected:$PATH"; exit 2;}' + unset PYENV_VERSION + pyenv-rehash + + # Path is not modified with system Python. + run pyenv-exec python -c 'import os; print(os.getenv("PATH"))' + assert_success "$PATH" + + # Path is modified with custom Python. + PYENV_VERSION=custom run pyenv-exec python + assert_success + + # Path is modified with custom:system Python. + PYENV_VERSION=custom:system run pyenv-exec python + assert_success + + # Path is not modified with system:custom Python. + PYENV_VERSION=system:custom run pyenv-exec python -c 'import os; print(os.getenv("PATH"))' + assert_success "$PATH" +}