8000 bpo-38901: Allow setting a venv's prompt to the basename of the current directory. by vsajip · Pull Request #17946 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Doc/library/venv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ creation according to their needs, the :class:`EnvBuilder` class.

* ``prompt`` -- a S 8000 tring 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

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_venv.py
8000
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ def test_prompt(self):
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'
Expand Down
2 changes: 2 additions & 0 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def __init__(self, system_site_packages=False, clear=False,
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

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
0