From c90e7fe73e1ecb35bce0b9afd1cb9e44bf08a1e5 Mon Sep 17 00:00:00 2001 From: Neko Asakura Date: Fri, 30 Oct 2026 10:32:14 -0400 Subject: [PATCH 1/2] gh-160782: reject line breaks in the venv directory and prompt --- Lib/test/test_venv.py | 17 +++++++++++++++++ Lib/venv/__init__.py | 7 +++++++ ...26-10-30-09-59-22.gh-issue-160782.fN0HIu.rst | 2 ++ 3 files changed, 26 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 678615ea172aef..9e879cd901ace9 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -1075,6 +1075,23 @@ def test_pathsep_error(self): self.assertRaises(ValueError, venv.create, bad_itempath) self.assertRaises(ValueError, venv.create, FakePath(bad_itempath)) + def test_line_break_error(self): + """ + Test that venv creation fails when the target directory or the + prompt contains a line break. + """ + rmtree(self.env_dir) + for line_break in '\n', '\r': + with self.subTest(line_break=line_break): + bad_itempath = f'{self.env_dir}{line_break}x' + self.assertRaises(ValueError, venv.create, bad_itempath) + self.assertRaises(ValueError, venv.create, + FakePath(bad_itempath)) + bad_prompt = f'x{line_break}x' + self.assertRaises(ValueError, venv.create, self.env_dir, + prompt=bad_prompt) + self.assertFalse(os.path.exists(self.env_dir)) + @unittest.skipIf(os.name == 'nt', 'not relevant on Windows') @requireVenvCreate def test_zippath_from_non_installed_posix(self): diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 620168be16862e..501070b9185083 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -149,6 +149,13 @@ def create_if_needed(d): if os.pathsep in os.fspath(env_dir): raise ValueError(f'Refusing to create a venv in {env_dir} because ' f'it contains the PATH separator {os.pathsep}.') + if '\n' in os.fspath(env_dir) or '\r' in os.fspath(env_dir): + raise ValueError(f'Refusing to create a venv in {env_dir!r} ' + 'because its path contains a line break.') + if self.prompt is not None and ('\n' in self.prompt + or '\r' in self.prompt): + raise ValueError(f'Refusing to use the prompt {self.prompt!r} ' + 'because it contains a line break.') if os.path.exists(env_dir) and self.clear: self.clear_directory(env_dir) context = types.SimpleNamespace() diff --git a/Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst b/Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst new file mode 100644 index 00000000000000..c69ee6ac18f5bd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst @@ -0,0 +1,2 @@ +:mod:`venv`: a line break in the prompt or the path now raises +:exc:`ValueError` instead of being written into ``pyvenv.cfg``. From 8335fbde56a044a2522a892c813401fff3b1fc39 Mon Sep 17 00:00:00 2001 From: Neko Asakura Date: Wed, 18 Nov 2026 14:00:23 -0500 Subject: [PATCH 2/2] gh-160782: only reject in venv directory --- Lib/test/test_venv.py | 8 ++------ Lib/venv/__init__.py | 7 ++----- .../2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst | 4 ++-- 3 files changed, 6 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 9e879cd901ace9..33975bf00de46a 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -1077,8 +1077,8 @@ def test_pathsep_error(self): def test_line_break_error(self): """ - Test that venv creation fails when the target directory or the - prompt contains a line break. + Test that venv creation fails when the target directory contains + a line break. """ rmtree(self.env_dir) for line_break in '\n', '\r': @@ -1087,10 +1087,6 @@ def test_line_break_error(self): self.assertRaises(ValueError, venv.create, bad_itempath) self.assertRaises(ValueError, venv.create, FakePath(bad_itempath)) - bad_prompt = f'x{line_break}x' - self.assertRaises(ValueError, venv.create, self.env_dir, - prompt=bad_prompt) - self.assertFalse(os.path.exists(self.env_dir)) @unittest.skipIf(os.name == 'nt', 'not relevant on Windows') @requireVenvCreate diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 501070b9185083..364150b59538b4 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -152,10 +152,6 @@ def create_if_needed(d): if '\n' in os.fspath(env_dir) or '\r' in os.fspath(env_dir): raise ValueError(f'Refusing to create a venv in {env_dir!r} ' 'because its path contains a line break.') - if self.prompt is not None and ('\n' in self.prompt - or '\r' in self.prompt): - raise ValueError(f'Refusing to use the prompt {self.prompt!r} ' - 'because it contains a line break.') if os.path.exists(env_dir) and self.clear: self.clear_directory(env_dir) context = types.SimpleNamespace() @@ -258,7 +254,8 @@ def create_configuration(self, context): if self.upgrade_deps: args.append('--upgrade-deps') if self.orig_prompt is not None: - args.append(f'--prompt="{self.orig_prompt}"') + prompt = self.orig_prompt.replace('\n', ' ').replace('\r', ' ') + args.append(f'--prompt="{prompt}"') if not self.scm_ignore_files: args.append('--without-scm-ignore-files') diff --git a/Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst b/Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst index c69ee6ac18f5bd..5a37c87a934b5a 100644 --- a/Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst +++ b/Misc/NEWS.d/next/Library/2026-10-30-09-59-22.gh-issue-160782.fN0HIu.rst @@ -1,2 +1,2 @@ -:mod:`venv`: a line break in the prompt or the path now raises -:exc:`ValueError` instead of being written into ``pyvenv.cfg``. +:mod:`venv`: a line break in the prompt or the path no longer starts a new +line in ``pyvenv.cfg``; such a path now raises :exc:`ValueError`.