10000 bpo-37663: have venv activation scripts all consistently use __VENV_P… · python/cpython@48ede6b · GitHub
[go: up one dir, main page]

Skip to content

Commit 48ede6b

Browse files
authored
bpo-37663: have venv activation scripts all consistently use __VENV_PROMPT__ for prompt customization (GH-14941)
The activation scripts generated by venv were inconsistent in how they changed the shell's prompt. Some used `__VENV_PROMPT__` exclusively, some used `__VENV_PROMPT__` if it was set even though by default `__VENV_PROMPT__` is always set and the fallback matched the default, and one ignored `__VENV_PROMPT__` and used `__VENV_NAME__` instead (and even used a differing format to the default prompt). This change now has all activation scripts use `__VENV_PROMPT__` only and relies on the fact that venv sets that value by default. The color of the customization is also now set in fish to the blue from the Python logo for as hex color support is built into that shell (much like PowerShell where the built-in green color is used).
1 parent df2d4a6 commit 48ede6b

File tree

5 files changed

+30
-46
lines changed

5 files changed

+30
-46
lines changed

Doc/whatsnew/3.9.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ finalization crashed with a Python fatal error if a daemon thread was still
118118
running.
119119
(Contributed by Victor Stinner in :issue:`37266`.)
120120

121+
venv
122+
----
123+
124+
The activation scripts provided by :mod:`venv` now all specify their prompt
125+
customization consistently by always using the value specified by
126+
``__VENV_PROMPT__``. Previously some scripts unconditionally used
127+
``__VENV_PROMPT__``, others only if it happened to be set (which was the default
128+
case), and one used ``__VENV_NAME__`` instead.
129+
(Contributed by Brett Cannon in :issue:`37663`.)
130+
121131
pprint
122132
------
123133

@@ -191,12 +201,14 @@ Removed
191201
Use :meth:`~threading.Thread.is_alive()` instead.
192202
(Contributed by Dong-hee Na in :issue:`37804`.)
193203

204+
194205
Porting to Python 3.9
195206
=====================
196207

197208
This section lists previously described changes and other bugfixes
198209
that may require changes to your code.
199210

211+
200212
Changes in the Python API
201213
-------------------------
202214

@@ -205,3 +217,6 @@ Changes in the Python API
205217
catching the specific exception type and supporting both Python 3.9 and
206218
earlier versions will need to catch both:
207219
``except (ImportError, ValueError):``
220+
221+
* The :mod:`venv` activation scripts no longer special-case when
222+
``__VENV_PROMPT__`` is set to ``""``.

Lib/venv/scripts/common/activate

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,7 @@ fi
5454

5555
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
5656
_OLD_VIRTUAL_PS1="${PS1:-}"
57-
if [ "x__VENV_PROMPT__" != x ] ; then
58-
PS1="__VENV_PROMPT__${PS1:-}"
59-
else
60-
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
61-
# special case for Aspen magic directories
62-
# see http://www.zetadev.com/software/aspen/
63-
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
64-
else
65-
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
66-
fi
67-
fi
57+
PS1="__VENV_PROMPT__${PS1:-}"
6858
export PS1
6959
fi
7060

Lib/venv/scripts/posix/activate.csh

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,7 @@ setenv PATH "$VIRTUAL_ENV/__VENV_BIN_NAME__:$PATH"
1717
set _OLD_VIRTUAL_PROMPT="$prompt"
1818

1919
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
20-
if ("__VENV_NAME__" != "") then
21-
set env_name = "__VENV_NAME__"
22-
else
23-
if (`basename "VIRTUAL_ENV"` == "__") then
24-
# special case for Aspen magic directories
25-
# see http://www.zetadev.com/software/aspen/
26-
set env_name = `basename \`dirname "$VIRTUAL_ENV"\``
27-
else
28-
set env_name = `basename "$VIRTUAL_ENV"`
29-
endif
30-
endif
31-
set prompt = "[$env_name] $prompt"
32-
unset env_name
20+
set prompt = "__VENV_PROMPT__$prompt"
3321
endif
3422

3523
alias pydoc python -m pydoc

Lib/venv/scripts/posix/activate.fish

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# This file must be used with ". bin/activate.fish" *from fish* (http://fishshell.org)
2-
# you cannot run it directly
1+
# This file must be used with ". bin/activate.fish" *from fish* (http://fishshell.org);
2+
# you cannot run it directly.
33

44
function deactivate -d "Exit virtualenv and return to normal shell environment"
55
# reset old environment variables
@@ -21,20 +21,20 @@ function deactivate -d "Exit virtualenv and return to normal shell environment"
2121

2222
set -e VIRTUAL_ENV
2323
if test "$argv[1]" != "nondestructive"
24-
# Self destruct!
24+
# Self-destruct!
2525
functions -e deactivate
2626
end
2727
end
2828

29-
# unset irrelevant variables
29+
# Unset irrelevant variables.
3030
deactivate nondestructive
3131

3232
set -gx VIRTUAL_ENV "__VENV_DIR__"
3333

3434
set -gx _OLD_VIRTUAL_PATH $PATH
3535
set -gx PATH "$VIRTUAL_ENV/__VENV_BIN_NAME__" $PATH
3636

37-
# unset PYTHONHOME if set
37+
# Unset PYTHONHOME if set.
3838
if set -q PYTHONHOME
3939
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
4040
set -e PYTHONHOME
@@ -43,31 +43,20 @@ end
4343
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
4444
# fish uses a function instead of an env var to generate the prompt.
4545

46-
# save the current fish_prompt function as the function _old_fish_prompt
46+
# Save the current fish_prompt function as the function _old_fish_prompt.
4747
functions -c fish_prompt _old_fish_prompt
4848

49-
# with the original prompt function renamed, we can override with our own.
49+
# With the original prompt function renamed, we can override with our own.
5050
function fish_prompt
51-
# Save the return status of the last command
51+
# Save the return status of the last command.
5252
set -l old_status $status
5353

54-
# Prompt override?
55-
if test -n "__VENV_PROMPT__"
56-
printf "%s%s" "__VENV_PROMPT__" (set_color normal)
57-
else
58-
CA0A # ...Otherwise, prepend env
59-
set -l _checkbase (basename "$VIRTUAL_ENV")
60-
if test $_checkbase = "__"
61-
# special case for Aspen magic directories
62-
# see http://www.zetadev.com/software/aspen/
63-
printf "%s[%s]%s " (set_color -b blue white) (basename (dirname "$VIRTUAL_ENV")) (set_color normal)
64-
else
65-
printf "%s(%s)%s" (set_color -b blue white) (basename "$VIRTUAL_ENV") (set_color normal)
66-
end
67-
end
54+
# Output the venv prompt; color taken from the blue of the Python logo.
55+
printf "%s%s%s" (set_color 4B8BBE) "__VENV_PROMPT__" (set_color normal)
6856

6957
# Restore the return status of the previous command.
7058
echo "exit $old_status" | .
59+
# Output the original/"old" prompt.
7160
_old_fish_prompt
7261
end
7362

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Bring consistency to venv shell activation scripts by always using
2+
__VENV_PROMPT__.

0 commit comments

Comments
 (0)
0