From 233aeb8f0e0d1a8b8a76c15f16eec84d624793e9 Mon Sep 17 00:00:00 2001 From: cybs-joe <74exitz@gmail.com> Date: Thu, 26 Nov 2026 22:11:57 +0200 Subject: [PATCH 1/2] gh-162689: Fix Path.copy() same-file check with stale cached info --- Lib/pathlib/__init__.py | 3 ++- Lib/test/test_pathlib/test_pathlib.py | 28 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/Lib/pathlib/__init__.py b/Lib/pathlib/__init__.py index ffec9c545ee11f..205a9cabc06f35 100644 --- a/Lib/pathlib/__init__.py +++ b/Lib/pathlib/__init__.py @@ -1303,7 +1303,8 @@ def copy(self, target, **kwargs): if not hasattr(target, 'with_segments'): target = self.with_segments(target) ensure_distinct_paths(self, target) - target._copy_from(self, **kwargs) + # Use fresh paths to avoid stale cached file information. + target.joinpath()._copy_from(self.joinpath(), **kwargs) return target.joinpath() # Empty join to ensure fresh metadata. def copy_into(self, target_dir, **kwargs): diff --git a/Lib/test/test_pathlib/test_pathlib.py b/Lib/test/test_pathlib/test_pathlib.py index 8013b0df26ec81..111638a8403b10 100644 --- a/Lib/test/test_pathlib/test_pathlib.py +++ b/Lib/test/test_pathlib/test_pathlib.py @@ -1341,6 +1341,34 @@ def test_copy_file_preserve_metadata(self): if hasattr(source_st, 'st_flags'): self.assertEqual(source_st.st_flags, target_st.st_flags) + @os_helper.skip_unless_hardlink + def test_copy_file_to_hardlink_with_stale_info(self): + base = self.cls(self.base) + source = base / 'fileA' + target = base / 'copyA' + source.write_bytes(b'original data') + target.write_bytes(b'old backup') + target.info.is_file() + target.unlink() + target.hardlink_to(source) + self.assertTrue(source.samefile(target)) + self.assertRaises(OSError, source.copy, target) + self.assertEqual(source.read_bytes(), b'original data') + + @os_helper.skip_unless_hardlink + def test_copy_file_from_hardlink_with_stale_info(self): + base = self.cls(self.base) + source = base / 'fileA' + target = base / 'copyA' + target.write_bytes(b'original data') + source.write_bytes(b'old backup') + source.info.is_file() + source.unlink() + source.hardlink_to(target) + self.assertTrue(source.samefile(target)) + self.assertRaises(OSError, source.copy, target) + self.assertEqual(target.read_bytes(), b'original data') + @needs_symlinks def test_copy_file_to_existing_symlink(self): base = self.cls(self.base) From 8e75a52f4c92a7a7e1b29af235541e701855fbcd Mon Sep 17 00:00:00 2001 From: cybs-joe <74exitz@gmail.com> Date: Thu, 26 Nov 2026 22:44:42 +0200 Subject: [PATCH 2/2] Add NEWS entry --- .../Library/2026-11-26-22-44-20.gh-issue-162689.dvyxKD.rst | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-26-22-44-20.gh-issue-162689.dvyxKD.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-26-22-44-20.gh-issue-162689.dvyxKD.rst b/Misc/NEWS.d/next/Library/2026-11-26-22-44-20.gh-issue-162689.dvyxKD.rst new file mode 100644 index 00000000000000..2d5a3668a365a5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-26-22-44-20.gh-issue-162689.dvyxKD.rst @@ -0,0 +1,4 @@ +Fix :meth:`pathlib.Path.copy` to correctly detect when the source and target +refer to the same file, even when cached path information is stale. +Previously, copying between hard links to the same file could empty the +source file and lose its data without raising an error.