]> granicus.if.org Git - python/commitdiff
Issue 21635: Fix caching in difflib.SequenceMatcher.get_matching_blocks().
authorRaymond Hettinger <python@rcn.com>
Sat, 21 Jun 2014 18:27:36 +0000 (11:27 -0700)
committerRaymond Hettinger <python@rcn.com>
Sat, 21 Jun 2014 18:27:36 +0000 (11:27 -0700)
Lib/difflib.py
Lib/test/test_difflib.py
Misc/NEWS

index 5dac1d63aea9a058cecec3af6059fc14633315c8..3880d8472eac628c3537d877aab7b110b8a24556 100644 (file)
@@ -523,8 +523,8 @@ class SequenceMatcher:
             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.
index 310bf9978257b66d5fc499b16b3d188a7eb24438..35f2c36ca70a322d6d5b6d872b87a6b53671a977 100644 (file)
@@ -59,6 +59,15 @@ class TestSFbugs(unittest.TestCase):
         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"]))
index 6cd1bd245af24cd2ff4c7326dc899d3b0222fce8..664eba74aff97f589178f8de9ba71dcaccf18446 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -31,6 +31,10 @@ Library
 
 - Issue #21491: SocketServer: Fix a race condition in child processes reaping.
 
+- Issue #21635:  The difflib SequenceMatcher.get_matching_blocks() method
+  cache didn't match the actual result.  The former was a list of tuples
+  and the latter was a list of named tuples.
+
 - Issue #21722: The distutils "upload" command now exits with a non-zero
   return code when uploading fails.  Patch by Martin Dengler.