Skip to content

Commit 1edff31

Browse files
committed
exits as error if the virtual environment doesn't have python executable (#104)
conda's environment might not have `python` executable. If the prefix doesn't contain `python` in it, `pyenv-which` might be ran into infinite loop if some of `which` hooks invoke `pyenv-virtualenv-prefix`.
1 parent 353062d commit 1edff31

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

bin/pyenv-virtualenv-prefix

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,16 @@ for version in "${versions[@]}"; do
3737
exit 1
3838
fi
3939
PYENV_PREFIX_PATH="$(pyenv-prefix "${version}")"
40-
if [ -f "${PYENV_PREFIX_PATH}/bin/activate" ]; then
41-
VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)"
42-
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${VIRTUALENV_PREFIX_PATH:-${PYENV_PREFIX_PATH}}")
40+
if [ -x "${PYENV_PREFIX_PATH}/bin/python" ]; then
41+
if [ -f "${PYENV_PREFIX_PATH}/bin/activate" ]; then
42+
VIRTUALENV_PREFIX_PATH="$(real_prefix "${version}" || base_prefix "${version}" || true)"
43+
VIRTUALENV_PREFIX_PATHS=("${VIRTUALENV_PREFIX_PATHS[@]}" "${VIRTUALENV_PREFIX_PATH:-${PYENV_PREFIX_PATH}}")
44+
else
45+
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
46+
exit 1
47+
fi
4348
else
44-
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
49+
echo "pyenv-virtualenv: \`python' not found in version \`${version}'" 1>&2
4550
exit 1
4651
fi
4752
done

test/prefix.bats

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@ setup() {
66
export PYENV_ROOT="${TMP}/pyenv"
77
}
88

9-
create_virtualenv() {
9+
create_version() {
1010
mkdir -p "${PYENV_ROOT}/versions/$1/bin"
11+
touch "${PYENV_ROOT}/versions/$1/bin/python"
12+
chmod +x "${PYENV_ROOT}/versions/$1/bin/python"
13+
}
14+
15+
remove_version() {
16+
rm -fr "${PYENV_ROOT}/versions/$1"
17+
}
18+
19+
create_virtualenv() {
20+
create_version "$@"
1121
touch "${PYENV_ROOT}/versions/$1/bin/activate"
1222
}
1323

1424
remove_virtualenv() {
15-
rm -fr "${PYENV_ROOT}/versions/$1"
25+
remove_version "$@"
1626
}
1727

1828
@test "display prefix with using sys.real_prefix" {
@@ -118,13 +128,13 @@ OUT
118128
@test "should fail if the version is not a virtualenv" {
119129
stub pyenv-version-name "echo 3.4.0"
120130
stub pyenv-prefix "3.4.0 : echo \"${PYENV_ROOT}/versions/3.4.0\""
121-
mkdir -p "${PYENV_ROOT}/versions/3.4.0"
131+
create_version "3.4.0"
122132

123133
PYENV_VERSION="3.4.0" run pyenv-virtualenv-prefix
124134

125135
unstub pyenv-version-name
126136
unstub pyenv-prefix
127-
rmdir "${PYENV_ROOT}/versions/3.4.0"
137+
remove_version "3.4.0"
128138

129139
assert_failure
130140
assert_output <<OUT
@@ -139,15 +149,15 @@ OUT
139149
stub pyenv-exec "false" \
140150
"echo \"${PYENV_ROOT}/versions/3.3.3\""
141151
create_virtualenv "venv33"
142-
mkdir -p "${PYENV_ROOT}/versions/3.4.0"
152+
create_version "3.4.0"
143153

144154
PYENV_VERSION="venv33:3.4.0" run pyenv-virtualenv-prefix
145155

146156
unstub pyenv-version-name
147157
unstub pyenv-prefix
148158
unstub pyenv-exec
149159
remove_virtualenv "venv33"
150-
rmdir "${PYENV_ROOT}/versions/3.4.0"
160+
remove_version "3.4.0"
151161

152162
assert_failure
153163
assert_output <<OUT

test/test_helper.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,16 @@ create_conda() {
123123
mkdir -p "${PYENV_ROOT}/versions/$version/bin"
124124
touch "${PYENV_ROOT}/versions/$version/bin/activate"
125125
touch "${PYENV_ROOT}/versions/$version/bin/conda"
126+
touch "${PYENV_ROOT}/versions/$version/bin/python"
126127
chmod +x "${PYENV_ROOT}/versions/$version/bin/conda"
128+
chmod +x "${PYENV_ROOT}/versions/$version/bin/python"
127129
local conda_env
128130
for conda_env; do
129131
mkdir -p "${PYENV_ROOT}/versions/$version/envs/$conda_env/bin"
130132
touch "${PYENV_ROOT}/versions/$version/envs/$conda_env/bin/activate"
131133
touch "${PYENV_ROOT}/versions/$version/envs/$conda_env/bin/conda"
134+
touch "${PYENV_ROOT}/versions/$version/envs/$conda_env/bin/python"
132135
chmod +x "${PYENV_ROOT}/versions/$version/envs/$conda_env/bin/conda"
136+
chmod +x "${PYENV_ROOT}/versions/$version/envs/$conda_env/bin/python"
133137
done
134138
}

0 commit comments

Comments
 (0)