[Python-checkins] bpo-33224: PEP 479 fix for difflib.mdiff() (GH-6381) (GH-6391)
Raymond Hettinger
webhook-mailer at python.org
Thu Apr 5 15:18:05 EDT 2018
https://github.com/python/cpython/commit/8da15f09458fd4f4fe341861e41723892b25a11b
commit: 8da15f09458fd4f4fe341861e41723892b25a11b
branch: 3.6
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: Raymond Hettinger <rhettinger at users.noreply.github.com>
date: 2018-04-05T12:18:02-07:00
summary:
bpo-33224: PEP 479 fix for difflib.mdiff() (GH-6381) (GH-6391)
(cherry picked from commit 01b731fc2b04744a11e32f93aba8bfb9ddb3dd29)
Co-authored-by: Raymond Hettinger <rhettinger at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2018-04-04-23-41-30.bpo-33224.pyR0jB.rst
M Lib/difflib.py
M Lib/test/test_difflib.py
diff --git a/Lib/difflib.py b/Lib/difflib.py
index 72971d5b87a9..7cebd64978ea 100644
--- a/Lib/difflib.py
+++ b/Lib/difflib.py
@@ -1634,14 +1634,18 @@ def _line_pair_iterator():
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 aaefe6db0291..745ccbd6659e 100644
--- a/Lib/test/test_difflib.py
+++ b/Lib/test/test_difflib.py
@@ -93,6 +93,14 @@ def test_added_tab_hint(self):
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.
diff --git a/Misc/NEWS.d/next/Library/2018-04-04-23-41-30.bpo-33224.pyR0jB.rst b/Misc/NEWS.d/next/Library/2018-04-04-23-41-30.bpo-33224.pyR0jB.rst
new file mode 100644
index 000000000000..87ff100da4b6
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-04-04-23-41-30.bpo-33224.pyR0jB.rst
@@ -0,0 +1,2 @@
+Update difflib.mdiff() for PEP 479. Convert an uncaught StopIteration in a
+generator into a return-statement.
More information about the Python-checkins
mailing list