Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d2c5b4b

Browse files
committed
SequenceMatcher(None, [], []).get_grouped_opcodes() now returns a generator
that behaves as if both lists has an empty string in each of them. Closes bug #979794 (and duplicate bug #980117).
1 parent b8e1717 commit d2c5b4b

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/difflib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ def get_grouped_opcodes(self, n=3):
572572
"""
573573

574574
codes = self.get_opcodes()
575+
if not codes:
576+
codes = [("equal", 0, 1, 0, 1)]
575577
# Fixup leading and trailing groups if they show no changes.
576578
if codes[0][0] == 'equal':
577579
tag, i1, i2, j1, j2 = codes[0]

Lib/test/test_difflib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ def test_ratio_for_null_seqn(self):
1212
self.assertEqual(s.quick_ratio(), 1)
1313
self.assertEqual(s.real_quick_ratio(), 1)
1414

15+
def test_comparing_empty_lists(self):
16+
# Check fix for bug #979794
17+
group_gen = difflib.SequenceMatcher(None, [], []).get_grouped_opcodes()
18+
self.assertRaises(StopIteration, group_gen.next)
19+
diff_gen = difflib.unified_diff([], [])
20+
self.assertRaises(StopIteration, diff_gen.next)
21+
1522
Doctests = doctest.DocTestSuite(difflib)
1623

1724
test_support.run_unittest(TestSFbugs, Doctests)

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ Extension modules
2929
Library
3030
-------
3131

32+
- Bug #979794: difflib.get_grouped_opcodes() now handles the case of when it is
33+
comparing two empty lists. Was affecting both context_diff() and
34+
unified_diff(). Was also a duplicate of bug #980117.
35+
3236
- Bug #980938: smtplib now prints debug output to sys.stderr.
3337

3438
- Bug #930024: posixpath.realpath() now handles infinite loops in symlinks by

0 commit comments

Comments
 (0)