From f22cdd0e92ddc30fcd7a741f8c885f929cd2a384 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Fri, 10 Jan 2020 22:26:51 +0000 Subject: [PATCH 1/3] bpo-38901: Allow setting a venv's prompt to the basename of the current directory. 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 | 3 ++- Lib/test/test_venv.py | 9 +++++++++ Lib/venv/__init__.py | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Doc/library/venv.rst b/Doc/library/venv.rst index 5494c0c878bc52..727584b9a94e38 100644 --- a/Doc/library/venv.rst +++ b/Doc/library/venv.rst @@ -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 diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 741ac109bbc8c5..a3b78c4e44e52e 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -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' diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 81cb1d13e21638..a220ef784c1344 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -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 From 4b2cb68ba7f398f63650403b4aa2991e54d2f274 Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Fri, 10 Jan 2020 22:31:15 +0000 Subject: [PATCH 2/3] Added NEWS entry. --- .../next/Library/2020-01-10-22-30-48.bpo-38901.OdVIIb.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2020-01-10-22-30-48.bpo-38901.OdVIIb.rst 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 index 00000000000000..304d53289e0ef2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-01-10-22-30-48.bpo-38901.OdVIIb.rst @@ -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. From 630d68d48f6209bc24f1f4e016e67d6d721b74eb Mon Sep 17 00:00:00 2001 From: Vinay Sajip Date: Sun, 12 Jan 2020 06:19:21 +0000 Subject: [PATCH 3/3] Update venv.rst --- Doc/library/venv.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/venv.rst b/Doc/library/venv.rst index 727584b9a94e38..d778486b0a5d94 100644 --- a/Doc/library/venv.rst +++ b/Doc/library/venv.rst @@ -122,7 +122,7 @@ 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). If the special string ``"."` is provided, the basename of the + 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