Skip to content

Commit c82dda1

Browse files
ZackerySpytzblfpd
andauthored
bpo-35328: Set VIRTUAL_ENV_PROMPT at venv activation (GH-21587)
Co-Authored-By: Baptiste Darthenay <baptiste.darthenay@gmail.com>
1 parent 1332226 commit c82dda1

File tree

7 files changed

+17
-1
lines changed

7 files changed

+17
-1
lines changed

Lib/venv/scripts/common/Activate.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ function global:deactivate ([switch]$NonDestructive) {
9696
Remove-Item -Path env:VIRTUAL_ENV
9797
}
9898

99+
# Just remove VIRTUAL_ENV_PROMPT altogether.
100+
if (Test-Path -Path Env:VIRTUAL_ENV_PROMPT) {
101+
Remove-Item -Path env:VIRTUAL_ENV_PROMPT
102+
}
103+
99104
# Just remove the _PYTHON_VENV_PROMPT_PREFIX altogether:
100105
if (Get-Variable -Name "_PYTHON_VENV_PROMPT_PREFIX" -ErrorAction SilentlyContinue) {
101106
Remove-Variable -Name _PYTHON_VENV_PROMPT_PREFIX -Scope Global -Force
@@ -228,6 +233,7 @@ if (-not $Env:VIRTUAL_ENV_DISABLE_PROMPT) {
228233
Write-Host -NoNewline -ForegroundColor Green "($_PYTHON_VENV_PROMPT_PREFIX) "
229234
_OLD_VIRTUAL_PROMPT
230235
}
236+
$env:VIRTUAL_ENV_PROMPT = $Prompt
231237
}
232238

233239
# Clear PYTHONHOME

Lib/venv/scripts/common/activate

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ deactivate () {
2828
fi
2929

3030
unset VIRTUAL_ENV
31+
unset VIRTUAL_ENV_PROMPT
3132
if [ ! "${1:-}" = "nondestructive" ] ; then
3233
# Self destruct!
3334
unset -f deactivate
@@ -56,6 +57,8 @@ if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
5657
_OLD_VIRTUAL_PS1="${PS1:-}"
5758
PS1="__VENV_PROMPT__${PS1:-}"
5859
export PS1
60+
VIRTUAL_ENV_PROMPT="__VENV_PROMPT__"
61+
export VIRTUAL_ENV_PROMPT
5962
fi
6063

6164
# This should detect bash and zsh, which have a hash command that must

Lib/venv/scripts/nt/activate.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ if defined _OLD_VIRTUAL_PATH set PATH=%_OLD_VIRTUAL_PATH%
2525
if not defined _OLD_VIRTUAL_PATH set _OLD_VIRTUAL_PATH=%PATH%
2626

2727
set PATH=%VIRTUAL_ENV%\__VENV_BIN_NAME__;%PATH%
28+
set VIRTUAL_ENV_PROMPT=__VENV_PROMPT__
2829

2930
:END
3031
if defined _OLD_CODEPAGE (

Lib/venv/scripts/nt/deactivate.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ if defined _OLD_VIRTUAL_PATH (
1717
set _OLD_VIRTUAL_PATH=
1818

1919
set VIRTUAL_ENV=
20+
set VIRTUAL_ENV_PROMPT=
2021

2122
:END

Lib/venv/scripts/posix/activate.csh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# Created by Davide Di Blasi <davidedb@gmail.com>.
44
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>
55

6-
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate'
6+
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; unsetenv VIRTUAL_ENV_PROMPT; test "\!:*" != "nondestructive" && unalias deactivate'
77

88
# Unset irrelevant variables.
99
deactivate nondestructive
@@ -18,6 +18,7 @@ set _OLD_VIRTUAL_PROMPT="$prompt"
1818

1919
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
2020
set prompt = "__VENV_PROMPT__$prompt"
21+
setenv VIRTUAL_ENV_PROMPT "__VENV_PROMPT__"
2122
endif
2223

2324
alias pydoc python -m pydoc

Lib/venv/scripts/posix/activate.fish

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ function deactivate -d "Exit virtual environment and return to normal shell env
2020
end
2121

2222
set -e VIRTUAL_ENV
23+
set -e VIRTUAL_ENV_PROMPT
2324
if test "$argv[1]" != "nondestructive"
2425
# Self-destruct!
2526
functions -e deactivate
@@ -61,4 +62,5 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
6162
end
6263

6364
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
65+
set -gx VIRTUAL_ENV_PROMPT "__VENV_PROMPT__"
6466
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Set the environment variable ``VIRTUAL_ENV_PROMPT`` at :mod:`venv`
2+
activation.

0 commit comments

Comments
 (0)