Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9bd2aa46
Commit
9bd2aa46
authored
Jul 01, 2003
by
Neal Norwitz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix SF bug #763023, difflib.py: ratio() zero division not caught
Backport candidate
parent
5b1aa2fb
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
23 additions
and
3 deletions
+23
-3
Lib/difflib.py
Lib/difflib.py
+8
-3
Lib/test/test_difflib.py
Lib/test/test_difflib.py
+12
-0
Misc/NEWS
Misc/NEWS
+3
-0
No files found.
Lib/difflib.py
View file @
9bd2aa46
...
...
@@ -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.
...
...
Lib/test/test_difflib.py
View file @
9bd2aa46
import
difflib
from
test
import
test_support
import
unittest
class
TestSFbugs
(
unittest
.
TestCase
):
def
test_ratio_for_null_seqn
(
self
):
# Check clearing of SF bug 763023
s
=
difflib
.
SequenceMatcher
(
None
,
[],
[])
self
.
assertEqual
(
s
.
ratio
(),
1
)
self
.
assertEqual
(
s
.
quick_ratio
(),
1
)
self
.
assertEqual
(
s
.
real_quick_ratio
(),
1
)
test_support
.
run_unittest
(
TestSFbugs
)
test_support
.
run_doctest
(
difflib
)
Misc/NEWS
View file @
9bd2aa46
...
...
@@ -24,6 +24,9 @@ Extension modules
Library
-------
- SF bug 763023: fix uncaught ZeroDivisionError in difflib ratio methods
when there are no lines.
Tools/Demos
-----------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment