mirror of
https://github.com/python/cpython.git
synced 2025-11-25 04:34:37 +00:00
Patch #1413711: Certain patterns of differences were making difflib
touch the recursion limit. The applied patch inlines the recursive __helper method in a non-recursive way.
This commit is contained in:
parent
c81e3a63af
commit
548148810b
3 changed files with 34 additions and 17 deletions
|
|
@ -473,26 +473,31 @@ class SequenceMatcher:
|
||||||
|
|
||||||
if self.matching_blocks is not None:
|
if self.matching_blocks is not None:
|
||||||
return self.matching_blocks
|
return self.matching_blocks
|
||||||
self.matching_blocks = []
|
|
||||||
la, lb = len(self.a), len(self.b)
|
la, lb = len(self.a), len(self.b)
|
||||||
self.__helper(0, la, 0, lb, self.matching_blocks)
|
|
||||||
self.matching_blocks.append( (la, lb, 0) )
|
|
||||||
return self.matching_blocks
|
|
||||||
|
|
||||||
|
indexed_blocks = []
|
||||||
|
queue = [(0, la, 0, lb)]
|
||||||
|
while queue:
|
||||||
# builds list of matching blocks covering a[alo:ahi] and
|
# builds list of matching blocks covering a[alo:ahi] and
|
||||||
# b[blo:bhi], appending them in increasing order to answer
|
# b[blo:bhi], appending them in increasing order to answer
|
||||||
|
alo, ahi, blo, bhi = queue.pop()
|
||||||
|
|
||||||
def __helper(self, alo, ahi, blo, bhi, answer):
|
|
||||||
i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi)
|
|
||||||
# a[alo:i] vs b[blo:j] unknown
|
# a[alo:i] vs b[blo:j] unknown
|
||||||
# a[i:i+k] same as b[j:j+k]
|
# a[i:i+k] same as b[j:j+k]
|
||||||
# a[i+k:ahi] vs b[j+k:bhi] unknown
|
# a[i+k:ahi] vs b[j+k:bhi] unknown
|
||||||
|
i, j, k = x = self.find_longest_match(alo, ahi, blo, bhi)
|
||||||
|
|
||||||
if k:
|
if k:
|
||||||
if alo < i and blo < j:
|
if alo < i and blo < j:
|
||||||
self.__helper(alo, i, blo, j, answer)
|
queue.append((alo, i, blo, j))
|
||||||
answer.append(x)
|
indexed_blocks.append((i, x))
|
||||||
if i+k < ahi and j+k < bhi:
|
if i+k < ahi and j+k < bhi:
|
||||||
self.__helper(i+k, ahi, j+k, bhi, answer)
|
queue.append((i+k, ahi, j+k, bhi))
|
||||||
|
indexed_blocks.sort()
|
||||||
|
|
||||||
|
self.matching_blocks = [elem[1] for elem in indexed_blocks]
|
||||||
|
self.matching_blocks.append( (la, lb, 0) )
|
||||||
|
return self.matching_blocks
|
||||||
|
|
||||||
def get_opcodes(self):
|
def get_opcodes(self):
|
||||||
"""Return list of 5-tuples describing how to turn a into b.
|
"""Return list of 5-tuples describing how to turn a into b.
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import difflib
|
||||||
from test.test_support import run_unittest, findfile
|
from test.test_support import run_unittest, findfile
|
||||||
import unittest
|
import unittest
|
||||||
import doctest
|
import doctest
|
||||||
|
import sys
|
||||||
|
|
||||||
class TestSFbugs(unittest.TestCase):
|
class TestSFbugs(unittest.TestCase):
|
||||||
|
|
||||||
|
|
@ -143,6 +144,14 @@ class TestSFpatches(unittest.TestCase):
|
||||||
|
|
||||||
self.assertEqual(actual,expect)
|
self.assertEqual(actual,expect)
|
||||||
|
|
||||||
|
def test_recursion_limit(self):
|
||||||
|
# Check if the problem described in patch #1413711 exists.
|
||||||
|
limit = sys.getrecursionlimit()
|
||||||
|
old = [(i%2 and "K:%d" or "V:A:%d") % i for i in range(limit*2)]
|
||||||
|
new = [(i%2 and "K:%d" or "V:B:%d") % i for i in range(limit*2)]
|
||||||
|
difflib.SequenceMatcher(None, old, new).get_opcodes()
|
||||||
|
|
||||||
|
|
||||||
Doctests = doctest.DocTestSuite(difflib)
|
Doctests = doctest.DocTestSuite(difflib)
|
||||||
|
|
||||||
run_unittest(TestSFpatches, TestSFbugs, Doctests)
|
run_unittest(TestSFpatches, TestSFbugs, Doctests)
|
||||||
|
|
|
||||||
|
|
@ -676,6 +676,9 @@ Library
|
||||||
|
|
||||||
- ` uu.encode()`` and ``uu.decode()`` now support unicode filenames.
|
- ` uu.encode()`` and ``uu.decode()`` now support unicode filenames.
|
||||||
|
|
||||||
|
- Patch #1413711: Certain patterns of differences were making difflib
|
||||||
|
touch the recursion limit.
|
||||||
|
|
||||||
Build
|
Build
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue