|
| 1 | +diff --git a/Lib/difflib.py b/Lib/difflib.py |
| 2 | +index 54fc57d..a4f0e8b 100644 |
| 3 | +--- a/Lib/difflib.py |
| 4 | ++++ b/Lib/difflib.py |
| 5 | +@@ -570,7 +570,7 @@ def get_grouped_opcodes(self, n=3): |
| 6 | + ('equal', 35, 38, 31, 34)]] |
| 7 | + """ |
| 8 | + |
| 9 | +- codes = self.get_opcodes() |
| 10 | ++ codes = self.get_opcodes().copy() |
| 11 | + if not codes: |
| 12 | + codes = [("equal", 0, 1, 0, 1)] |
| 13 | + # Fixup leading and trailing groups if they show no changes. |
| 14 | +diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py |
| 15 | +index 8c554d7..539e744 100644 |
| 16 | +--- a/Lib/test/test_difflib.py |
| 17 | ++++ b/Lib/test/test_difflib.py |
| 18 | +@@ -179,6 +179,27 @@ def test_matching_blocks_cache(self): |
| 19 | + self.assertEqual(second[1].size, 2) |
| 20 | + self.assertEqual(second[2].size, 0) |
| 21 | + |
| 22 | ++ def test_grouped_opcodes_does_not_mutate_opcodes(self): |
| 23 | ++ # gh-161973: get_grouped_opcodes() must not mutate the cached |
| 24 | ++ # opcodes, so a later get_opcodes() still returns the full list. |
| 25 | ++ old = ["line %d\n" % i for i in range(10)] |
| 26 | ++ new = old[:] |
| 27 | ++ new[5] = "changed\n" |
| 28 | ++ sm = difflib.SequenceMatcher(None, old, new) |
| 29 | ++ expected = [ |
| 30 | ++ ("equal", 0, 5, 0, 5), |
| 31 | ++ ("replace", 5, 6, 5, 6), |
| 32 | ++ ("equal", 6, 10, 6, 10), |
| 33 | ++ ] |
| 34 | ++ self.assertEqual(sm.get_opcodes(), expected) |
| 35 | ++ list(sm.get_grouped_opcodes(1)) |
| 36 | ++ self.assertEqual(sm.get_opcodes(), expected) |
| 37 | ++ # A later request for wider context must not inherit the trimming. |
| 38 | ++ fresh = difflib.SequenceMatcher(None, old, new) |
| 39 | ++ self.assertEqual( |
| 40 | ++ list(sm.get_grouped_opcodes(3)), list(fresh.get_grouped_opcodes(3)) |
| 41 | ++ ) |
| 42 | ++ |
| 43 | + def test_added_tab_hint(self): |
| 44 | + # Check fix for bug #1488943 |
| 45 | + diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"])) |
| 46 | +diff --git a/Misc/NEWS.d/next/Library/2026-11-14-11-30-00.gh-issue-161973.M9YLnQ.rst b/Misc/NEWS.d/next/Library/2026-11-14-11-30-00.gh-issue-161973.M9YLnQ.rst |
| 47 | +new file mode 100644 |
| 48 | +index 0000000..ed6b022 |
| 49 | +--- /dev/null |
| 50 | ++++ b/Misc/NEWS.d/next/Library/2026-11-14-11-30-00.gh-issue-161973.M9YLnQ.rst |
| 51 | +@@ -0,0 +1 @@ |
| 52 | ++Fix :meth:`difflib.SequenceMatcher.get_grouped_opcodes` mutating the cached opcodes, so a later :meth:`difflib.SequenceMatcher.get_opcodes` call returned the trimmed list instead of the full one. |
0 commit comments