Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8edc802

Browse files
authored
gh-89427: Provide the original prompt value for VIRTUAL_ENV_PROMPT (GH-106726)
This improves the implementation in gh-106643. Previously, venv passed "(<prompt>) " to the activation scripts, but we want to provide the original value so that users can inspect it in the $VIRTUAL_ENV_PROMPT env var. Note: Lib/venv/scripts/common/Activate.ps1 surrounded the prompt value with parens a second time, so no change was necessary in that file.
1 parent b822b85 commit 8edc802

File tree

6 files changed

+9
-10
lines changed

6 files changed

+9
-10
lines changed

Lib/test/test_venv.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def test_config_file_command_key(self):
169169
('--clear', 'clear', True),
170170
('--upgrade', 'upgrade', True),
171171
('--upgrade-deps', 'upgrade_deps', True),
172-
('--prompt', 'prompt', True),
172+
('--prompt="foobar"', 'prompt', 'foobar'),
173173
('--without-scm-ignore-files', 'scm_ignore_files', frozenset()),
174174
]
175175
for opt, attr, value in options:
@@ -201,15 +201,15 @@ def test_prompt(self):
201201
self.run_with_capture(builder.create, self.env_dir)
202202
context = builder.ensure_directories(self.env_dir)
203203
data = self.get_text_file_contents('pyvenv.cfg')
204-
self.assertEqual(context.prompt, '(%s) ' % env_name)
204+
self.assertEqual(context.prompt, env_name)
205205
self.assertNotIn("prompt = ", data)
206206

207207
rmtree(self.env_dir)
208208
builder = venv.EnvBuilder(prompt='My prompt')
209209
self.run_with_capture(builder.create, self.env_dir)
210210
context = builder.ensure_directories(self.env_dir)
211211
data = self.get_text_file_contents('pyvenv.cfg')
212-
self.assertEqual(context.prompt, '(My prompt) ')
212+
self.assertEqual(context.prompt, 'My prompt')
213213
self.assertIn("prompt = 'My prompt'\n", data)
214214

215215
rmtree(self.env_dir)
@@ -218,7 +218,7 @@ def test_prompt(self):
218218
self.run_with_capture(builder.create, self.env_dir)
219219
context = builder.ensure_directories(self.env_dir)
220220
data = self.get_text_file_contents('pyvenv.cfg')
221-
self.assertEqual(context.prompt, '(%s) ' % cwd)
221+
self.assertEqual(context.prompt, cwd)
222222
self.assertIn("prompt = '%s'\n" % cwd, data)
223223

224224
def test_upgrade_dependencies(self):

Lib/venv/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ def create_if_needed(d):
129129
context = types.SimpleNamespace()
130130
context.env_dir = env_dir
131131
context.env_name = os.path.split(env_dir)[1]
132-
prompt = self.prompt if self.prompt is not None else context.env_name
133-
context.prompt = '(%s) ' % prompt
132+
context.prompt = self.prompt if self.prompt is not None else context.env_name
134133
create_if_needed(env_dir)
135134
executable = sys._base_executable
136135
if not executable: # see gh-96861

Lib/venv/scripts/common/activate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fi
6666

6767
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
6868
_OLD_VIRTUAL_PS1="${PS1:-}"
69-
PS1="__VENV_PROMPT__${PS1:-}"
69+
PS1="(__VENV_PROMPT__) ${PS1:-}"
7070
export PS1
7171
fi
7272

Lib/venv/scripts/nt/activate.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if defined _OLD_VIRTUAL_PROMPT set PROMPT=%_OLD_VIRTUAL_PROMPT%
1616
if defined _OLD_VIRTUAL_PYTHONHOME set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%
1717

1818
set _OLD_VIRTUAL_PROMPT=%PROMPT%
19-
set PROMPT=__VENV_PROMPT__%PROMPT%
19+
set PROMPT=(__VENV_PROMPT__) %PROMPT%
2020

2121
if defined PYTHONHOME set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%
2222
set PYTHONHOME=

Lib/venv/scripts/posix/activate.csh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ setenv VIRTUAL_ENV_PROMPT "__VENV_PROMPT__"
1919
set _OLD_VIRTUAL_PROMPT="$prompt"
2020

2121
if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
22-
set prompt = "__VENV_PROMPT__$prompt"
22+
set prompt = "(__VENV_PROMPT__) $prompt"
2323
endif
2424

2525
alias pydoc python -m pydoc

Lib/venv/scripts/posix/activate.fish

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
5757
set -l old_status $status
5858

5959
# Output the venv prompt; color taken from the blue of the Python logo.
60-
printf "%s%s%s" (set_color 4B8BBE) "__VENV_PROMPT__" (set_color normal)
60+
printf "%s(%s)%s " (set_color 4B8BBE) "__VENV_PROMPT__" (set_color normal)
6161

6262
# Restore the return status of the previous command.
6363
echo "exit $old_status" | .

0 commit comments

Comments
 (0)