[Python-bugs-list] [ python-Bugs-763023 ] difflib.py: line 528 in ratio() zero division not caught

SourceForge.net noreply@sourceforge.net
Mon, 30 Jun 2003 21:40:08 -0700


Bugs item #763023, was opened at 2003-06-30 01:24
Message generated for change (Comment added) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=763023&group_id=5470

Category: Python Library
Group: Python 2.2.3
Status: Open
>Resolution: Accepted
Priority: 5
Submitted By: Matthias Klose (doko)
Assigned to: Raymond Hettinger (rhettinger)
Summary:  difflib.py: line 528 in ratio() zero division not caught

Initial Comment:
2.2.3 and 2.3b1:

>>> from difflib import * 
>>> s = SequenceMatcher(None, [], []) 
>>> s.ratio() 
Traceback (most recent call last): 
  File "<stdin>", line 1, in ? 
    File "/usr/lib/python2.2/difflib.py", line 528, in
ratio 
        return 2.0 * matches / (len(self.a) + len(self.b)) 
      ZeroDivisionError: float division 


----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-30 23:40

Message:
Logged In: YES 
user_id=80475

Looks good.
See attached test to replace the existing testfile.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-06-30 22:37

Message:
Logged In: YES 
user_id=33168

There should be a test for this too, but I'm not real
familiar with doctest.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-06-30 22:36

Message:
Logged In: YES 
user_id=33168

How about we split the difference, say 0.5. :-)

Raymond (or Tim) could you review the attached patch?  Ok
adding this new (non-public) function?

----------------------------------------------------------------------

Comment By: Tim Peters (tim_one)
Date: 2003-06-30 17:29

Message:
Logged In: YES 
user_id=31435

Heh.  How a ZeroDivisionError coming out of a method named 
*ratio* can be a surprise is beyond me <wink>.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-30 10:34

Message:
Logged In: YES 
user_id=80475

One other thought.  The same fix should be made to all three 
ratio methods (including quick and real-quick).

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-06-30 10:30

Message:
Logged In: YES 
user_id=80475

When the denominator is zero, it means that both sequences 
are of zero-length; therefore, the two sequences are equal, 
so the return value should be 1.0.

The try / except ZeroDivision error approach look good.

----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-06-30 10:19

Message:
Logged In: YES 
user_id=33168

This affects 2.3 as well (line 614).  The return line can be
wrapped with try/except ZeroDivisionError.  Raymond, should
0.0 be returned in this case?  Should something else be done?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=763023&group_id=5470