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
18 changes: 18 additions & 0 deletions Lib/test/test_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,24 @@ def test_unoverwritable_fails(self):
self.assertRaises((ValueError, OSError), venv.create, self.env_dir)
self.clear_directory(self.env_dir)

@unittest.skipUnless(can_symlink(), 'Needs symlinks')
def test_symlinked_directory_error(self):
"""
Test that venv creation fails when a directory inside the target
directory is a symlink.
"""
outside = tempfile.mkdtemp()
self.addCleanup(rmtree, outside)
for n in range(1, len(self.lib) + 1):
subdirs = self.lib[:n]
with self.subTest(subdirs=subdirs):
rmtree(self.env_dir)
os.makedirs(os.path.join(self.env_dir, *subdirs[:-1]))
os.symlink(outside, os.path.join(self.env_dir, *subdirs),
target_is_directory=True)
self.assertRaises(ValueError, venv.create, self.env_dir)
self.assertEqual(os.listdir(outside), [])

def test_upgrade(self):
"""
Test upgrading an existing environment directory.
Expand Down
6 changes: 6 additions & 0 deletions Lib/venv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ def ensure_directories(self, env_dir):
"""

def create_if_needed(d):
path = env_dir
for part in os.path.relpath(d, env_dir).split(os.sep)[:-1]:
path = os.path.join(path, part)
if os.path.islink(path) or os.path.isjunction(path):
raise ValueError(f'Refusing to create a venv in {env_dir} '
f'because {path} is a link.')
if not os.path.exists(d):
os.makedirs(d)
elif os.path.islink(d) or os.path.isfile(d):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`venv`: a symbolic link or junction in place of a directory inside the
environment now raises :exc:`ValueError` instead of being followed.
Loading