Skip to content
Open
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
17 changes: 17 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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``.
Loading