Check for existence of venv directory

Only call pyenv-prefix if the path doesn’t exist.
This commit is contained in:
Sam Doran 2026-04-23 17:28:15 -04:00
parent fed3b04d2f
commit 5ac3b3a1d9
No known key found for this signature in database
GPG key ID: 01602635F94328AA
3 changed files with 23 additions and 15 deletions

View file

@ -49,7 +49,14 @@ for version in "${versions[@]}"; do
echo "pyenv-virtualenv: version \`${version}' is not a virtualenv" 1>&2
exit 1
fi
PYENV_PREFIX_PATH="$(pyenv-prefix "${version}")"
# Calling pyenv-prefix is expensive. Only do so if the path doesn't exist.
# See https://github.com/pyenv/pyenv-virtualenv/pull/502
PYENV_PREFIX_PATH="${PYENV_ROOT}/versions/${version}"
if [[ ! -d "$PYENV_PREFIX_PATH" ]]; then
PYENV_PREFIX_PATH="$(pyenv-prefix "${version}")"
fi
if [ -x "${PYENV_PREFIX_PATH}/bin/python" ]; then
if [ -f "${PYENV_PREFIX_PATH}/bin/activate" ]; then
if [ -f "${PYENV_PREFIX_PATH}/bin/conda" ]; then

View file

@ -75,11 +75,9 @@ print_version() {
fi
local path="${2:?}"
if [[ -L "$path" ]]; then
# Only resolve the link itself for printing, do not resolve further.
# Doing otherwise would misinform the user of what the link contains.
version_repr="$version --> $(readlink "$path")"
else
version_repr="$version"
version_repr="$version (created from $(pyenv-virtualenv-prefix "$version" 2>/dev/null))"
fi
if [[ ${BASH_VERSINFO[0]} -gt 3 && ${current_versions["$1"]} ]] || \
{ [[ ${BASH_VERSINFO[0]} -le 3 ]] && exists "$1" "${current_versions[@]}"; }; then

View file

@ -4,9 +4,12 @@ load test_helper
setup() {
export PYENV_ROOT="${TMP}/pyenv"
mkdir -p "${PYENV_ROOT}/versions/2.7.6/envs/venv27"
mkdir -p "${PYENV_ROOT}/versions/3.3.3/envs/venv33"
setup_m_venv "2.7.6/envs/venv27"
echo "home = ${PYENV_ROOT}/versions/2.7.6/bin" > "${PYENV_ROOT}/versions/2.7.6/envs/venv27/pyvenv.cfg"
ln -s "${PYENV_ROOT}/versions/2.7.6/envs/venv27" "${PYENV_ROOT}/versions/venv27"
setup_m_venv "3.3.3/envs/venv33"
echo "home = ${PYENV_ROOT}/versions/3.3.3/bin" > "${PYENV_ROOT}/versions/3.3.3/envs/venv33/pyvenv.cfg"
ln -s "${PYENV_ROOT}/versions/3.3.3/envs/venv33" "${PYENV_ROOT}/versions/venv33"
}
@ -17,8 +20,8 @@ setup() {
assert_success
assert_output <<OUT
2.7.6/envs/venv27
3.3.3/envs/venv33
2.7.6/envs/venv27 (created from ${PYENV_ROOT}/versions/2.7.6)
3.3.3/envs/venv33 (created from ${PYENV_ROOT}/versions/3.3.3)
venv27 --> ${PYENV_ROOT}/versions/2.7.6/envs/venv27
venv33 --> ${PYENV_ROOT}/versions/3.3.3/envs/venv33
OUT
@ -34,8 +37,8 @@ OUT
assert_success
assert_output <<OUT
2.7.6/envs/venv27
* 3.3.3/envs/venv33 (set by PYENV_VERSION)
2.7.6/envs/venv27 (created from ${PYENV_ROOT}/versions/2.7.6)
* 3.3.3/envs/venv33 (created from ${PYENV_ROOT}/versions/3.3.3) (set by PYENV_VERSION)
venv27 --> ${PYENV_ROOT}/versions/2.7.6/envs/venv27
venv33 --> ${PYENV_ROOT}/versions/3.3.3/envs/venv33
OUT
@ -73,8 +76,8 @@ OUT
assert_success
assert_output <<OUT
2.7.6/envs/venv27
3.3.3/envs/venv33
2.7.6/envs/venv27 (created from ${PYENV_ROOT}/versions/2.7.6)
3.3.3/envs/venv33 (created from ${PYENV_ROOT}/versions/3.3.3)
OUT
unstub pyenv-version-name
@ -88,8 +91,8 @@ OUT
assert_success
assert_output <<OUT
2.7.6/envs/venv27
3.3.3/envs/venv33
2.7.6/envs/venv27 (created from ${PYENV_ROOT}/versions/2.7.6)
3.3.3/envs/venv33 (created from ${PYENV_ROOT}/versions/3.3.3)
* venv27 --> ${PYENV_ROOT}/versions/2.7.6/envs/venv27 (set by PYENV_VERSION)
venv33 --> ${PYENV_ROOT}/versions/3.3.3/envs/venv33
OUT
@ -98,7 +101,7 @@ OUT
unstub pyenv-version-origin
}
@test "no warning with --bare and no virtualenvs" {
@test "no output with --bare and no virtualenvs" {
rm -rf "${PYENV_ROOT}/versions"
mkdir -p "${PYENV_ROOT}/versions"