summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2003-07-01 15:12:33 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2003-07-01 15:12:33 (GMT)
commita3d818fc7bbd44639c65c25635525b84ecffa1de (patch)
treeedca67e5845ea2dbb189d9402f6860b660018d6d
parent91bde2bf9920569241bc62beef26dce98e250c14 (diff)
downloadcpython-a3d818fc7bbd44639c65c25635525b84ecffa1de.zip
cpython-a3d818fc7bbd44639c65c25635525b84ecffa1de.tar.gz
cpython-a3d818fc7bbd44639c65c25635525b84ecffa1de.tar.bz2
Fix SF bug #763023, difflib.py: ratio() zero division not caught
-rw-r--r--Lib/difflib.py11
-rw-r--r--Misc/NEWS3
2 files changed, 11 insertions, 3 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 97ced5f..ec9eb6c 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -24,6 +24,11 @@ Class Differ:
__all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher',
'Differ', 'IS_CHARACTER_JUNK', 'IS_LINE_JUNK']
+def _calculate_ratio(matches, length):
+ if length:
+ return 2.0 * matches / length
+ return 1.0
+
class SequenceMatcher:
"""
@@ -525,7 +530,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.
@@ -554,7 +559,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.
@@ -566,7 +571,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.
diff --git a/Misc/NEWS b/Misc/NEWS
index 2d83441..9ac0e11 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -5,6 +5,9 @@ What's New in Python 2.2.4?
- SF #753592: webchecker/wsgui now handles user supplied directories.
+- SF bug 763023: fix uncaught ZeroDivisionError in difflib ratio methods
+ when there are no lines.
+
What's New in Python 2.2.3 (final) ?
Release date: 30-May-2003
====================================