Skip to content

Commit bdb4183

Browse files
committed
gh-161973: Fix SequenceMatcher.get_grouped_opcodes() mutating the cached opcodes
1 parent 797087c commit bdb4183

4 files changed

Lines changed: 75 additions & 1 deletion

File tree

‎Lib/difflib.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def get_grouped_opcodes(self, n=3):
570570
('equal', 35, 38, 31, 34)]]
571571
"""
572572

573-
codes = self.get_opcodes()
573+
codes = self.get_opcodes().copy()
574574
if not codes:
575575
codes = [("equal", 0, 1, 0, 1)]
576576
# Fixup leading and trailing groups if they show no changes.

‎Lib/test/test_difflib.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,27 @@ def test_matching_blocks_cache(self):
179179
self.assertEqual(second[1].size, 2)
180180
self.assertEqual(second[2].size, 0)
181181

182+
def test_grouped_opcodes_does_not_mutate_opcodes(self):
183+
# gh-161973: get_grouped_opcodes() must not mutate the cached
184+
# opcodes, so a later get_opcodes() still returns the full list.
185+
old = ["line %d\n" % i for i in range(10)]
186+
new = old[:]
187+
new[5] = "changed\n"
188+
sm = difflib.SequenceMatcher(None, old, new)
189+
expected = [
190+
("equal", 0, 5, 0, 5),
191+
("replace", 5, 6, 5, 6),
192+
("equal", 6, 10, 6, 10),
193+
]
194+
self.assertEqual(sm.get_opcodes(), expected)
195+
list(sm.get_grouped_opcodes(1))
196+
self.assertEqual(sm.get_opcodes(), expected)
197+
# A later request for wider context must not inherit the trimming.
198+
fresh = difflib.SequenceMatcher(None, old, new)
199+
self.assertEqual(
200+
list(sm.get_grouped_opcodes(3)), list(fresh.get_grouped_opcodes(3))
201+
)
202+
182203
def test_added_tab_hint(self):
183204
# Check fix for bug #1488943
184205
diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
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.

‎cpython-161973.patch‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)