[Python-checkins] python/dist/src/Lib difflib.py,1.14,1.15

nnorwitz@users.sourceforge.net nnorwitz@users.sourceforge.net
2003年7月01日 07:57:10 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv25889/Lib
Modified Files:
	difflib.py 
Log Message:
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Backport candidate
Index: difflib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/difflib.py,v
retrieving revision 1.14
retrieving revision 1.15
diff -C2 -d -r1.14 -r1.15
*** difflib.py	17 Jun 2003 16:53:25 -0000	1.14
--- difflib.py	1 Jul 2003 14:57:08 -0000	1.15
***************
*** 30,33 ****
--- 30,38 ----
 'unified_diff']
 
+ def _calculate_ratio(matches, length):
+ if length:
+ return 2.0 * matches / length
+ return 1.0
+ 
 class SequenceMatcher:
 
***************
*** 612,616 ****
 matches = reduce(lambda sum, triple: sum + triple[-1],
 self.get_matching_blocks(), 0)
! return 2.0 * matches / (len(self.a) + len(self.b))
 
 def quick_ratio(self):
--- 617,621 ----
 matches = reduce(lambda sum, triple: sum + triple[-1],
 self.get_matching_blocks(), 0)
! return _calculate_ratio(matches, len(self.a) + len(self.b))
 
 def quick_ratio(self):
***************
*** 641,645 ****
 if numb > 0:
 matches = matches + 1
! return 2.0 * matches / (len(self.a) + len(self.b))
 
 def real_quick_ratio(self):
--- 646,650 ----
 if numb > 0:
 matches = matches + 1
! return _calculate_ratio(matches, len(self.a) + len(self.b))
 
 def real_quick_ratio(self):
***************
*** 653,657 ****
 # can't have more matches than the number of elements in the
 # shorter sequence
! return 2.0 * min(la, lb) / (la + lb)
 
 def get_close_matches(word, possibilities, n=3, cutoff=0.6):
--- 658,662 ----
 # can't have more matches than the number of elements in the
 # shorter sequence
! return _calculate_ratio(min(la, lb), la + lb)
 
 def get_close_matches(word, possibilities, n=3, cutoff=0.6):

AltStyle によって変換されたページ (->オリジナル) /