Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fabefc3

Browse files
committed
Issue 21635: Fix caching in difflib.SequenceMatcher.get_matching_blocks().
1 parent 8aa9e42 commit fabefc3

3 files changed

Lines changed: 15 additions & 2 deletions

File tree

Lib/difflib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,8 @@ def get_matching_blocks(self):
511511
non_adjacent.append((i1, j1, k1))
512512

513513
non_adjacent.append( (la, lb, 0) )
514-
self.matching_blocks = non_adjacent
515-
return map(Match._make, self.matching_blocks)
514+
self.matching_blocks = list(map(Match._make, non_adjacent))
515+
return self.matching_blocks
516516

517517
def get_opcodes(self):
518518
"""Return list of 5-tuples describing how to turn a into b.

Lib/test/test_difflib.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ def test_comparing_empty_lists(self):
7676
diff_gen = difflib.unified_diff([], [])
7777
self.assertRaises(StopIteration, next, diff_gen)
7878

79+
def test_matching_blocks_cache(self):
80+
# Issue #21635
81+
s = difflib.SequenceMatcher(None, "abxcd", "abcd")
82+
first = s.get_matching_blocks()
83+
second = s.get_matching_blocks()
84+
self.assertEqual(second[0].size, 2)
85+
self.assertEqual(second[1].size, 2)
86+
self.assertEqual(second[2].size, 0)
87+
7988
def test_added_tab_hint(self):
8089
# Check fix for bug #1488943
8190
diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Library
2929

3030
- Issue #21491: socketserver: Fix a race condition in child processes reaping.
3131

32+
- Issue #21635: The difflib SequenceMatcher.get_matching_blocks() method
33+
cache didn't match the actual result. The former was a list of tuples
34+
and the latter was a list of named tuples.
35+
3236
- Issue #21722: The distutils "upload" command now exits with a non-zero
3337
return code when uploading fails. Patch by Martin Dengler.
3438

0 commit comments

Comments
 (0)