diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 43c2a552ee90b7f..6e4b0219b85eb1e 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -681,6 +681,29 @@ def test_symlinked_directory_error(self): self.assertRaises(ValueError, venv.create, self.env_dir) self.assertEqual(os.listdir(outside), []) + @unittest.skipUnless(can_symlink(), 'Needs symlinks') + def test_symlinked_file_replaced(self): + """ + Test that venv creation replaces a symlink where it writes a file. + """ + outside_dir = tempfile.mkdtemp() + self.addCleanup(rmtree, outside_dir) + outside = os.path.join(outside_dir, 'outside') + with open(outside, 'w', encoding='utf-8') as f: + f.write('untouched\n') + links = ['pyvenv.cfg', '.gitignore', + os.path.join(self.bindir, 'activate')] + os.mkdir(os.path.join(self.env_dir, self.bindir)) + for link in links: + os.symlink(outside, self.get_env_file(link)) + venv.create(self.env_dir, scm_ignore_files={'git'}) + for link in links: + with self.subTest(link=link): + self.assertFalse(os.path.islink(self.get_env_file(link))) + self.assertTrue(os.path.isfile(self.get_env_file(link))) + with open(outside, encoding='utf-8') as f: + self.assertEqual(f.read(), 'untouched\n') + def test_upgrade(self): """ Test upgrading an existing environment directory. diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index 60fe070b1f9f4b1..7b62e7187cbee7c 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -18,6 +18,11 @@ logger = logging.getLogger(__name__) +def _unlink_symlink(path): + if os.path.islink(path): + os.unlink(path) + + class EnvBuilder: """ This class exists to allow virtual environment creation to be @@ -229,6 +234,7 @@ def create_configuration(self, context): being processed. """ context.cfg_path = path = os.path.join(context.env_dir, 'pyvenv.cfg') + _unlink_symlink(path) with open(path, 'w', encoding='utf-8') as f: f.write('home = %s\n' % context.python_dir) if self.system_site_packages: @@ -297,6 +303,7 @@ def create_git_ignore_file(self, context): ignored by git. """ gitignore_path = os.path.join(context.env_dir, '.gitignore') + _unlink_symlink(gitignore_path) with open(gitignore_path, 'w', encoding='utf-8') as file: file.write('# Created by venv; ' 'see https://docs.python.org/3/library/venv.html\n') @@ -591,6 +598,7 @@ def skip_file(f): if not os.path.exists(dstdir): os.makedirs(dstdir) dstfile = os.path.join(dstdir, f) + _unlink_symlink(dstfile) if os.name == 'nt' and srcfile.endswith(('.exe', '.pdb')): shutil.copy2(srcfile, dstfile) continue diff --git a/Misc/NEWS.d/next/Library/2026-10-30-10-43-07.gh-issue-160782.AHKQl6.rst b/Misc/NEWS.d/next/Library/2026-10-30-10-43-07.gh-issue-160782.AHKQl6.rst new file mode 100644 index 000000000000000..07f85ab8892e3a3 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-30-10-43-07.gh-issue-160782.AHKQl6.rst @@ -0,0 +1,2 @@ +:mod:`venv`: a symbolic link in place of ``pyvenv.cfg``, ``.gitignore`` or +an activation script is now replaced instead of written through.