Skip to content

Commit

Permalink
gh-89427: Provide the original prompt value for VIRTUAL_ENV_PROMPT
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jimporter committed Jul 13, 2023
1 parent 2f3ee02 commit d65e6ee
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
27 changes: 13 additions & 14 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,21 @@ def _check_output_of_default_create(self):

def test_config_file_command_key(self):
attrs = [
(None, None),
('symlinks', '--copies'),
('with_pip', '--without-pip'),
('system_site_packages', '--system-site-packages'),
('clear', '--clear'),
('upgrade', '--upgrade'),
('upgrade_deps', '--upgrade-deps'),
('prompt', '--prompt'),
(None, None, None),
('symlinks', False, '--copies'),
('with_pip', False, '--without-pip'),
('system_site_packages', True, '--system-site-packages'),
('clear', True, '--clear'),
('upgrade', True, '--upgrade'),
('upgrade_deps', True, '--upgrade-deps'),
('prompt', 'foobar', '--prompt="foobar"'),
]
for attr, opt in attrs:
for attr, value, opt in attrs:
rmtree(self.env_dir)
if not attr:
b = venv.EnvBuilder()
else:
b = venv.EnvBuilder(
**{attr: False if attr in ('with_pip', 'symlinks') else True})
b = venv.EnvBuilder(**{attr: value})
b.upgrade_dependencies = Mock() # avoid pip command to upgrade deps
b._setup_pip = Mock() # avoid pip setup
self.run_with_capture(b.create, self.env_dir)
Expand All @@ -185,15 +184,15 @@ def test_prompt(self):
self.run_with_capture(builder.create, self.env_dir)
context = builder.ensure_directories(self.env_dir)
data = self.get_text_file_contents('pyvenv.cfg')
self.assertEqual(context.prompt, '(%s) ' % env_name)
self.assertEqual(context.prompt, env_name)
self.assertNotIn("prompt = ", data)

rmtree(self.env_dir)
builder = venv.EnvBuilder(prompt='My prompt')
self.run_with_capture(builder.create, self.env_dir)
context = builder.ensure_directories(self.env_dir)
data = self.get_text_file_contents('pyvenv.cfg')
self.assertEqual(context.prompt, '(My prompt) ')
self.assertEqual(context.prompt, 'My prompt')
self.assertIn("prompt = 'My prompt'\n", data)

rmtree(self.env_dir)
Expand All @@ -202,7 +201,7 @@ def test_prompt(self):
self.run_with_capture(builder.create, self.env_dir)
context = builder.ensure_directories(self.env_dir)
data = self.get_text_file_contents('pyvenv.cfg')
self.assertEqual(context.prompt, '(%s) ' % cwd)
self.assertEqual(context.prompt, cwd)
self.assertIn("prompt = '%s'\n" % cwd, data)

def test_upgrade_dependencies(self):
Expand Down
3 changes: 1 addition & 2 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ def create_if_needed(d):
context = types.SimpleNamespace()
context.env_dir = env_dir
context.env_name = os.path.split(env_dir)[1]
prompt = self.prompt if self.prompt is not None else context.env_name
context.prompt = '(%s) ' % prompt
context.prompt = self.prompt if self.prompt is not None else context.env_name
create_if_needed(env_dir)
executable = sys._base_executable
if not executable: # see gh-96861
Expand Down
2 changes: 1 addition & 1 deletion Lib/venv/scripts/common/activate
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
PS1="__VENV_PROMPT__${PS1:-}"
PS1="(__VENV_PROMPT__) ${PS1:-}"
export PS1
fi

Expand Down
2 changes: 1 addition & 1 deletion Lib/venv/scripts/nt/activate.bat
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if defined _OLD_VIRTUAL_PROMPT set PROMPT=%_OLD_VIRTUAL_PROMPT%
if defined _OLD_VIRTUAL_PYTHONHOME set PYTHONHOME=%_OLD_VIRTUAL_PYTHONHOME%

set _OLD_VIRTUAL_PROMPT=%PROMPT%
set PROMPT=__VENV_PROMPT__%PROMPT%
set PROMPT=(__VENV_PROMPT__) %PROMPT%

if defined PYTHONHOME set _OLD_VIRTUAL_PYTHONHOME=%PYTHONHOME%
set PYTHONHOME=
Expand Down
2 changes: 1 addition & 1 deletion Lib/venv/scripts/posix/activate.csh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ setenv VIRTUAL_ENV_PROMPT "__VENV_PROMPT__"
set _OLD_VIRTUAL_PROMPT="$prompt"

if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
set prompt = "__VENV_PROMPT__$prompt"
set prompt = "(__VENV_PROMPT__) $prompt"
endif

alias pydoc python -m pydoc
Expand Down
2 changes: 1 addition & 1 deletion Lib/venv/scripts/posix/activate.fish
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
set -l old_status $status

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

# Restore the return status of the previous command.
echo "exit $old_status" | .
Expand Down

0 comments on commit d65e6ee

Please sign in to comment.