Skip to content

Commit f316080

Browse files
authored
gh-160782: reject line breaks in the venv directory (GH-160972)
1 parent 18be993 commit f316080

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎Lib/test/test_venv.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,19 @@ def test_pathsep_error(self):
11361136
self.assertRaises(ValueError, venv.create, bad_itempath)
11371137
self.assertRaises(ValueError, venv.create, FakePath(bad_itempath))
11381138

1139+
def test_line_break_error(self):
1140+
"""
1141+
Test that venv creation fails when the target directory contains
1142+
a line break.
1143+
"""
1144+
rmtree(self.env_dir)
1145+
for line_break in '\n', '\r':
1146+
with self.subTest(line_break=line_break):
1147+
bad_itempath = f'{self.env_dir}{line_break}x'
1148+
self.assertRaises(ValueError, venv.create, bad_itempath)
1149+
self.assertRaises(ValueError, venv.create,
1150+
FakePath(bad_itempath))
1151+
11391152
@unittest.skipIf(os.name == 'nt', 'not relevant on Windows')
11401153
@requireVenvCreate
11411154
def test_zippath_from_non_installed_posix(self):

‎Lib/venv/__init__.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ def create_if_needed(d):
155155
if os.pathsep in os.fspath(env_dir):
156156
raise ValueError(f'Refusing to create a venv in {env_dir} because '
157157
f'it contains the PATH separator {os.pathsep}.')
158+
if '\n' in os.fspath(env_dir) or '\r' in os.fspath(env_dir):
159+
raise ValueError(f'Refusing to create a venv in {env_dir!r} '
160+
'because its path contains a line break.')
158161
if os.path.exists(env_dir) and self.clear:
159162
self.clear_directory(env_dir)
160163
context = types.SimpleNamespace()
@@ -257,7 +260,8 @@ def create_configuration(self, context):
257260
if self.upgrade_deps:
258261
args.append('--upgrade-deps')
259262
if self.orig_prompt is not None:
260-
args.append(f'--prompt="{self.orig_prompt}"')
263+
prompt = self.orig_prompt.replace('\n', ' ').replace('\r', ' ')
264+
args.append(f'--prompt="{prompt}"')
261265
if not self.scm_ignore_files:
262266
args.append('--without-scm-ignore-files')
263267

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:mod:`venv`: a line break in the prompt or the path no longer starts a new
2+
line in ``pyvenv.cfg``; such a path now raises :exc:`ValueError`.

0 commit comments

Comments
 (0)