]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-89427: Provide the original prompt value for VIRTUAL_ENV_PROMPT (GH-106726)
authorJim Porter <826865+jimporter@users.noreply.github.com>
Tue, 23 Jan 2024 08:53:04 +0000 (00:53 -0800)
committerGitHub <noreply@github.com>
Tue, 23 Jan 2024 08:53:04 +0000 (08:53 +0000)
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.

Lib/test/test_venv.py
Lib/venv/__init__.py
Lib/venv/scripts/common/activate
Lib/venv/scripts/nt/activate.bat
Lib/venv/scripts/posix/activate.csh
Lib/venv/scripts/posix/activate.fish

index 6852625c36c62bdf765532662cb25ee1bc403853..6dda00efd7bbb6989d4b72c90c5280eb4761786c 100644 (file)
@@ -169,7 +169,7 @@ class BasicTest(BaseTest):
             ('--clear', 'clear', True),
             ('--upgrade', 'upgrade', True),
             ('--upgrade-deps', 'upgrade_deps', True),
-            ('--prompt', 'prompt', True),
+            ('--prompt="foobar"', 'prompt', 'foobar'),
             ('--without-scm-ignore-files', 'scm_ignore_files', frozenset()),
         ]
         for opt, attr, value in options:
@@ -201,7 +201,7 @@ class BasicTest(BaseTest):
         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)
@@ -209,7 +209,7 @@ class BasicTest(BaseTest):
         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)
@@ -218,7 +218,7 @@ class BasicTest(BaseTest):
         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):
index f04ca8fafcc33ba6dc130b271cc17d537cd488b2..4856594755ae577aaea06e7c848ca14be6d55871 100644 (file)
@@ -129,8 +129,7 @@ class EnvBuilder:
         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
index a4e0609045a9d5e1f4c63022aec0559feaf2ad65..cbd4873f01224646b220d24ad8f341e1044f1a62 100644 (file)
@@ -66,7 +66,7 @@ fi
 
 if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
     _OLD_VIRTUAL_PS1="${PS1:-}"
-    PS1="__VENV_PROMPT__${PS1:-}"
+    PS1="(__VENV_PROMPT__) ${PS1:-}"
     export PS1
 fi
 
index c1c3c82ee37f1079bd20354c8e551bb736184261..2c98122362a060f6bfbc46a2a4080b85fe1a0a8d 100644 (file)
@@ -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=
index 9caf138a919a8675d0df242d35c55966a78c5798..c707f1988b0acc22a55b528a206b99aa45e68c54 100644 (file)
@@ -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
index 565df23d1e2a13d195116e7ded1abf8c5802c428..25c42756789bbca149013775c73ddfebeb5958a8 100644 (file)
@@ -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" | .