bpo-38901: Allow setting a venv's prompt to the basename of the current directory. (GH-17946)
When a prompt value of '.' is specified, os.path.basename(os.getcwd()) is used to
configure the prompt for the created venv.
diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py
index 741ac10..a3b78c4 100644
--- a/Lib/test/test_venv.py
+++ b/Lib/test/test_venv.py
@@ -138,6 +138,15 @@
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'
diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py
index 81cb1d1..a220ef7 100644
--- a/Lib/venv/__init__.py
+++ b/Lib/venv/__init__.py
@@ -51,6 +51,8 @@
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