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.
This commit is contained in:
Vinay Sajip 2020-01-14 20:49:30 +00:00 committed by GitHub
parent 4b0d91aab4
commit 7d6378051f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 1 deletions

View 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 * ``prompt`` -- a String to be used after virtual environment is activated
(defaults to ``None`` which means directory name of the environment would (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 * ``upgrade_deps`` -- Update the base venv modules to the latest on PyPI

View File

@ -138,6 +138,15 @@ class BasicTest(BaseTest):
self.assertEqual(context.prompt, '(My prompt) ') self.assertEqual(context.prompt, '(My prompt) ')
self.assertIn("prompt = 'My prompt'\n", data) 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): def test_upgrade_dependencies(self):
builder = venv.EnvBuilder() builder = venv.EnvBuilder()
bin_path = 'Scripts' if sys.platform == 'win32' else 'bin' bin_path = 'Scripts' if sys.platform == 'win32' else 'bin'

View File

@ -51,6 +51,8 @@ class EnvBuilder:
self.symlinks = symlinks self.symlinks = symlinks
self.upgrade = upgrade self.upgrade = upgrade
self.with_pip = with_pip self.with_pip = with_pip
if prompt == '.': # see bpo-38901
prompt = os.path.basename(os.getcwd())
self.prompt = prompt self.prompt = prompt
self.upgrade_deps = upgrade_deps self.upgrade_deps = upgrade_deps

View File

@ -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.