diff options
author | Neal Norwitz <nnorwitz@gmail.com> | 2003-07-01 14:59:46 (GMT) |
---|---|---|
committer | Neal Norwitz <nnorwitz@gmail.com> | 2003-07-01 14:59:46 (GMT) |
commit | e7dfe21bed410baff8191aebd679f647c9e95a0a (patch) | |
tree | fd5391274e451d0b05dec84624ca1062fb2f93dc /Lib/difflib.py | |
parent | 37ca8c12dc0fb34736749d1224e1e013f4afd5b4 (diff) | |
download | cpython-e7dfe21bed410baff8191aebd679f647c9e95a0a.zip cpython-e7dfe21bed410baff8191aebd679f647c9e95a0a.tar.gz cpython-e7dfe21bed410baff8191aebd679f647c9e95a0a.tar.bz2 |
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Backport candidate
Diffstat (limited to 'Lib/difflib.py')
-rw-r--r-- | Lib/difflib.py | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py index 198114d..eb0eccf 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -29,6 +29,11 @@ __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', 'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff', 'unified_diff'] +def _calculate_ratio(matches, length): + if length: + return 2.0 * matches / length + return 1.0 + class SequenceMatcher: """ @@ -611,7 +616,7 @@ class SequenceMatcher: matches = reduce(lambda sum, triple: sum + triple[-1], self.get_matching_blocks(), 0) - return 2.0 * matches / (len(self.a) + len(self.b)) + return _calculate_ratio(matches, len(self.a) + len(self.b)) def quick_ratio(self): """Return an upper bound on ratio() relatively quickly. @@ -640,7 +645,7 @@ class SequenceMatcher: avail[elt] = numb - 1 if numb > 0: matches = matches + 1 - return 2.0 * matches / (len(self.a) + len(self.b)) + return _calculate_ratio(matches, len(self.a) + len(self.b)) def real_quick_ratio(self): """Return an upper bound on ratio() very quickly. @@ -652,7 +657,7 @@ class SequenceMatcher: la, lb = len(self.a), len(self.b) # can't have more matches than the number of elements in the # shorter sequence - return 2.0 * min(la, lb) / (la + lb) + return _calculate_ratio(min(la, lb), la + lb) def get_close_matches(word, possibilities, n=3, cutoff=0.6): """Use SequenceMatcher to return list of the best "good enough" matches. |