Issue 21635: Fix caching in difflib.SequenceMatcher.get_matching_blocks().
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 5dac1d6..3880d84 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -523,8 +523,8 @@
non_adjacent.append((i1, j1, k1))
non_adjacent.append( (la, lb, 0) )
- self.matching_blocks = non_adjacent
- return map(Match._make, self.matching_blocks)
+ self.matching_blocks = map(Match._make, non_adjacent)
+ return self.matching_blocks
def get_opcodes(self):
"""Return list of 5-tuples describing how to turn a into b.
diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py
index 310bf99..35f2c36 100644
--- a/Lib/test/test_difflib.py
+++ b/Lib/test/test_difflib.py
@@ -59,6 +59,15 @@
diff_gen = difflib.unified_diff([], [])
self.assertRaises(StopIteration, diff_gen.next)
+ def test_matching_blocks_cache(self):
+ # Issue #21635
+ s = difflib.SequenceMatcher(None, "abxcd", "abcd")
+ first = s.get_matching_blocks()
+ second = s.get_matching_blocks()
+ self.assertEqual(second[0].size, 2)
+ self.assertEqual(second[1].size, 2)
+ self.assertEqual(second[2].size, 0)
+
def test_added_tab_hint(self):
# Check fix for bug #1488943
diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))