summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
Diffstat (limited to 'Lib')
-rw-r--r--Lib/difflib.py20
-rw-r--r--Lib/test/test_difflib.py8
2 files changed, 20 insertions, 8 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 72971d5..7cebd64 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -1634,14 +1634,18 @@ 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
- while(lines_to_write):
- from_line, to_line, found_diff = next(line_pair_iterator)
- # If another change within the context, extend the context
- if found_diff:
- lines_to_write = context-1
- else:
- lines_to_write -= 1
- yield from_line, to_line, found_diff
+ try:
+ while(lines_to_write):
+ from_line, to_line, found_diff = next(line_pair_iterator)
+ # If another change within the context, extend the context
+ if found_diff:
+ lines_to_write = context-1
+ else:
+ lines_to_write -= 1
+ yield from_line, to_line, found_diff
+ except StopIteration:
+ # Catch exception from next() and return normally
+ return
_file_template = """
diff --git a/Lib/test/test_difflib.py b/Lib/test/test_difflib.py
index aaefe6d..745ccbd 100644
--- a/Lib/test/test_difflib.py
+++ b/Lib/test/test_difflib.py
@@ -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.