Skip to content

Commit 28c1790

Browse files
miss-islingtonrhettinger
authored andcommitted
bpo-33224: PEP 479 fix for difflib.mdiff() (GH-6381) (GH-6390)
1 parent baf304e commit 28c1790

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

Lib/difflib.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,14 +1634,18 @@ def _line_pair_iterator():
16341634
lines_to_write -= 1
16351635
# Now yield the context lines after the change
16361636
lines_to_write = context-1
1637-
while(lines_to_write):
1638-
from_line, to_line, found_diff = next(line_pair_iterator)
1639-
# If another change within the context, extend the context
1640-
if found_diff:
1641-
lines_to_write = context-1
1642-
else:
1643-
lines_to_write -= 1
1644-
yield from_line, to_line, found_diff
1637+
try:
1638+
while(lines_to_write):
1639+
from_line, to_line, found_diff = next(line_pair_iterator)
1640+
# If another change within the context, extend the context
1641+
if found_diff:
1642+
lines_to_write = context-1
1643+
else:
1644+
lines_to_write -= 1
1645+
yield from_line, to_line, found_diff
1646+
except StopIteration:
1647+
# Catch exception from next() and return normally
1648+
return
16451649

16461650

16471651
_file_template = """

Lib/test/test_difflib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ def test_added_tab_hint(self):
9393
self.assertEqual("+ \t\tI am a bug", diff[2])
9494
self.assertEqual("? +\n", diff[3])
9595

96+
def test_mdiff_catch_stop_iteration(self):
97+
# Issue #33224
98+
self.assertEqual(
99+
list(difflib._mdiff(["2"], ["3"], 1)),
100+
[((1, '\x00-2\x01'), (1, '\x00+3\x01'), True)],
101+
)
102+
103+
96104
patch914575_from1 = """
97105
1. Beautiful is beTTer than ugly.
98106
2. Explicit is better than implicit.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Update difflib.mdiff() for PEP 479. Convert an uncaught StopIteration in a
2+
generator into a return-statement.

0 commit comments

Comments
 (0)