]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38901: Allow setting a venv's prompt to the basename of the current directory...
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Tue, 14 Jan 2020 20:49:30 +0000 (20:49 +0000)
committerGitHub <noreply@github.com>
Tue, 14 Jan 2020 20:49:30 +0000 (20:49 +0000)
When a prompt value of '.' is specified, os.path.basename(os.getcwd()) is used to
configure the prompt for the created venv.

Doc/library/venv.rst
Lib/test/test_venv.py
Lib/venv/__init__.py
Misc/NEWS.d/next/Library/2020-01-10-22-30-48.bpo-38901.OdVIIb.rst [new file with mode: 0644]

index 5494c0c878bc523d338794de047ec089bacf4908..d778486b0a5d94cc9dd809461128382fca0ae8cf 100644 (file)
@@ -122,7 +122,8 @@ creation according to their needs, the :class:`EnvBuilder` class.
 
     * ``prompt`` -- a String to be used after virtual environment is activated
       (defaults to ``None`` which means directory name of the environment would
-      be used).
+      be used). If the special string ``"."`` is provided, the basename of the
+      current directory is used as the prompt.
 
     * ``upgrade_deps`` -- Update the base venv modules to the latest on PyPI
 
index 741ac109bbc8c5ac79912960b179a7ad2b565248..a3b78c4e44e52ec1f90e3911e3671e9d96fbf5ec 100644 (file)
@@ -138,6 +138,15 @@ class BasicTest(BaseTest):
         self.assertEqual(context.prompt, '(My prompt) ')
         self.assertIn("prompt = 'My prompt'\n", data)
 
+        rmtree(self.env_dir)
+        builder = venv.EnvBuilder(prompt='.')
+        cwd = os.path.basename(os.getcwd())
+        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.assertIn("prompt = '%s'\n" % cwd, data)
+
     def test_upgrade_dependencies(self):
         builder = venv.EnvBuilder()
         bin_path = 'Scripts' if sys.platform == 'win32' else 'bin'
index 81cb1d13e216387cef3261e42ea425066d71bc6a..a220ef784c13443af83da4a4c6a4b30e1e4007bc 100644 (file)
@@ -51,6 +51,8 @@ class EnvBuilder:
         self.symlinks = symlinks
         self.upgrade = upgrade
         self.with_pip = with_pip
+        if prompt == '.':  # see bpo-38901
+            prompt = os.path.basename(os.getcwd())
         self.prompt = prompt
         self.upgrade_deps = upgrade_deps
 
diff --git a/Misc/NEWS.d/next/Library/2020-01-10-22-30-48.bpo-38901.OdVIIb.rst b/Misc/NEWS.d/next/Library/2020-01-10-22-30-48.bpo-38901.OdVIIb.rst
new file mode 100644 (file)
index 0000000..304d532
--- /dev/null
@@ -0,0 +1,3 @@
+When you specify prompt='.' or equivalently python -m venv --prompt . ...
+the basename of the current directory is used to set the created venv's
+prompt when it's activated.