bpo-33224: PEP 479 fix for difflib.mdiff() (GH-6381)

This commit is contained in:
Raymond Hettinger 2018-04-05 11:19:57 -07:00 committed by GitHub
parent 7286dbd8b2
commit 01b731fc2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 8 deletions

View File

@ -1634,6 +1634,7 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
lines_to_write -= 1
# Now yield the context lines after the change
lines_to_write = context-1
try:
while(lines_to_write):
from_line, to_line, found_diff = next(line_pair_iterator)
# If another change within the context, extend the context
@ -1642,6 +1643,9 @@ def _mdiff(fromlines, tolines, context=None, linejunk=None,
else:
lines_to_write -= 1
yield from_line, to_line, found_diff
except StopIteration:
# Catch exception from next() and return normally
return
_file_template = """

View File

@ -93,6 +93,14 @@ class TestSFbugs(unittest.TestCase):
self.assertEqual("+ \t\tI am a bug", diff[2])
self.assertEqual("? +\n", diff[3])
def test_mdiff_catch_stop_iteration(self):
# Issue #33224
self.assertEqual(
list(difflib._mdiff(["2"], ["3"], 1)),
[((1, '\x00-2\x01'), (1, '\x00+3\x01'), True)],
)
patch914575_from1 = """
1. Beautiful is beTTer than ugly.
2. Explicit is better than implicit.

View File

@ -0,0 +1,2 @@
Update difflib.mdiff() for PEP 479. Convert an uncaught StopIteration in a
generator into a return-statement.