Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Lib/difflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
'unified_diff', 'diff_bytes', 'HtmlDiff', 'Match']

from heapq import nlargest as _nlargest
from collections import namedtuple as _namedtuple
from collections import deque as _deque, namedtuple as _namedtuple
from types import GenericAlias
lazy from _colorize import can_colorize, get_theme

Expand Down Expand Up @@ -1558,7 +1558,7 @@ def _line_pair_iterator():
is defined) does not need to be of module scope.
"""
line_iterator = _line_iterator()
fromlines,tolines=[],[]
fromlines, tolines = _deque(), _deque()
while True:
# Collecting lines of text until we have a from/to pair
while (len(fromlines)==0 or len(tolines)==0):
Expand All @@ -1571,8 +1571,8 @@ def _line_pair_iterator():
if to_line is not None:
tolines.append((to_line,found_diff))
# Once we have a pair, remove them from the collection and yield it
from_line, fromDiff = fromlines.pop(0)
to_line, to_diff = tolines.pop(0)
from_line, fromDiff = fromlines.popleft()
to_line, to_diff = tolines.popleft()
yield (from_line,to_line,fromDiff or to_diff)

# Handle case where user does not want context differencing, just yield
Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_difflib.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ def test_mdiff_catch_stop_iteration(self):
[((1, '\x00-2\x01'), (1, '\x00+3\x01'), True)],
)

def test_mdiff_lopsided_replace(self):
self.assertEqual(
list(difflib._mdiff(["a\n"] * 4, ["b\n"])),
[
((1, '\x00-a\n\x01'), (1, '\x00+b\n\x01'), True),
((2, '\x00-a\n\x01'), ('', '\n'), True),
((3, '\x00-a\n\x01'), ('', '\n'), True),
((4, '\x00-a\n\x01'), ('', '\n'), True),
],
)
self.assertEqual(
list(difflib._mdiff(["a\n"], ["b\n"] * 4)),
[
((1, '\x00-a\n\x01'), (1, '\x00+b\n\x01'), True),
(('', '\n'), (2, '\x00+b\n\x01'), True),
(('', '\n'), (3, '\x00+b\n\x01'), True),
(('', '\n'), (4, '\x00+b\n\x01'), True),
],
)


patch914575_from1 = """
1. Beautiful is beTTer than ugly.
Expand Down
Loading