diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 43c2a552ee90b7..d175f2253bf12a 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -664,6 +664,33 @@ def test_unoverwritable_fails(self): self.clear_directory(self.env_dir) @unittest.skipUnless(can_symlink(), 'Needs symlinks') + def test_clear_symlink_not_followed(self): + # gh-158068: venv --clear must not wipe the contents of a directory + # that env_dir merely points to via a symlink. + target = os.path.join(self.env_dir, 'target') + os.mkdir(target) + keep = os.path.join(target, 'keep') + with open(keep, 'wb') as f: + f.write(b'Still here?') + link = os.path.join(self.env_dir, 'link') + os.symlink(target, link) + + with self.assertRaisesRegex(ValueError, 'Unable to create directory'): + venv.create(link, clear=True) + # The symlink target and its content are left untouched. + self.assertTrue(os.path.islink(link)) + self.assertTrue(os.path.exists(keep)) + + def test_clear_file_not_removed(self): + # gh-158068: venv --clear must not treat a regular file as env_dir. + target = os.path.join(self.env_dir, 'afile') + with open(target, 'wb') as f: + f.write(b'Still here?') + + with self.assertRaisesRegex(ValueError, 'Unable to create directory'): + venv.create(target, clear=True) + self.assertTrue(os.path.isfile(target)) + def test_symlinked_directory_error(self): """ Test that venv creation fails when a directory inside the target diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 60fe070b1f9f4b..e7cdfde896f11a 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -149,14 +149,19 @@ def create_if_needed(d): 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): - raise ValueError('Unable to create directory %r' % d) + elif not os.path.isdir(d): + raise ValueError(f'Unable to create directory {d!r}: ' + 'the existing file is not a directory') 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 os.path.exists(env_dir) and self.clear: - self.clear_directory(env_dir) + if self.clear: + if os.path.islink(env_dir) or (os.path.exists(env_dir) and not os.path.isdir(env_dir)): + raise ValueError(f'Unable to create directory {env_dir!r}: ' + 'the existing file is not a directory') + if os.path.exists(env_dir): + self.clear_directory(env_dir) context = types.SimpleNamespace() context.env_dir = env_dir context.env_name = os.path.split(env_dir)[1] diff --git a/Misc/NEWS.d/next/Library/2026-09-24-14-00-00.gh-issue-158068.Xf2ogr.rst b/Misc/NEWS.d/next/Library/2026-09-24-14-00-00.gh-issue-158068.Xf2ogr.rst new file mode 100644 index 00000000000000..29b2f4ef11ec93 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-09-24-14-00-00.gh-issue-158068.Xf2ogr.rst @@ -0,0 +1,4 @@ +:mod:`venv` no longer clears the target directory when it is a symlink or a +regular file. Previously ``venv --clear`` followed a symlink and could delete +the contents of the linked directory. It now raises :exc:`ValueError` before +removing anything.