From 48e214e930aa0f1b32edff485f38cf3650f3999d Mon Sep 17 00:00:00 2001 From: 0000matteo0000 Date: Wed, 19 Jul 2023 15:55:12 +0200 Subject: [PATCH 1/3] fix shutil.copytree() failing to override existing symbolic links --- Lib/shutil.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Lib/shutil.py b/Lib/shutil.py index c24b786fb639242..99d259f8942ffdb 100644 --- a/Lib/shutil.py +++ b/Lib/shutil.py @@ -590,6 +590,8 @@ def _copytree(entries, src, dst, symlinks, ignore, copy_function, # We can't just leave it to `copy_function` because legacy # code with a custom `copy_function` may rely on copytree # doing the right thing. + if dirs_exist_ok and os.path.lexists(dstname): + os.unlink(dstname) os.symlink(linkto, dstname) copystat(srcobj, dstname, follow_symlinks=not symlinks) else: From 86b4e6d7d24e14a1b4b5caab32fec0200987c849 Mon Sep 17 00:00:00 2001 From: 0000matteo0000 Date: Fri, 13 Nov 2026 16:00:14 +0100 Subject: [PATCH 2/3] test shutil.copytree symlink replacement --- Lib/test/test_shutil.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index c0b48ea306b71ed..ed59e7e111236d4 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -854,6 +854,22 @@ def test_copytree_symlinks(self): if hasattr(os, 'lchflags'): self.assertEqual(dst_stat.st_flags, src_stat.st_flags) + @os_helper.skip_unless_symlink + def test_copytree_symlink_replaces_existing(self): + src = self.mkdtemp() + os.symlink('new', os.path.join(src, 'link')) + for existing in ('file', 'symlink'): + with self.subTest(existing=existing): + dst = self.mkdtemp() + dst_link = os.path.join(dst, 'link') + if existing == 'file': + create_file(dst_link, 'original') + else: + os.symlink('old', dst_link) + shutil.copytree(src, dst, symlinks=True, dirs_exist_ok=True) + self.assertTrue(os.path.islink(dst_link)) + self.assertEqual(os.readlink(dst_link), 'new') + def test_copytree_with_exclude(self): # creating data join = os.path.join From 71d54fefaaa606ed67fc05e92a9cd411a0f9a1bc Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Wed, 19 Jul 2023 16:30:34 +0000 Subject: [PATCH 3/3] Add NEWS entry for shutil.copytree symlink replacement --- .../next/Library/2023-07-19-16-30-33.gh-issue-105919.LBV7TS.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2023-07-19-16-30-33.gh-issue-105919.LBV7TS.rst diff --git a/Misc/NEWS.d/next/Library/2023-07-19-16-30-33.gh-issue-105919.LBV7TS.rst b/Misc/NEWS.d/next/Library/2023-07-19-16-30-33.gh-issue-105919.LBV7TS.rst new file mode 100644 index 000000000000000..3fc22c064496ed2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-07-19-16-30-33.gh-issue-105919.LBV7TS.rst @@ -0,0 +1 @@ +fixed shutil.copytree(), now it overrides existing symbolic links correctly