Merge pull request #2486 from RustPython/coolreader18/update-difflib

Update difflib.py
This commit is contained in:
Jeong YunWon
2021-02-20 12:44:09 +09:00
committed by GitHub
2 changed files with 13 additions and 28 deletions

37
Lib/difflib.py vendored
View File

@@ -733,20 +733,15 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
# Strip scores for the best n matches
return [x for score, x in result]
def _count_leading(line, ch):
"""
Return number of `ch` characters at the start of `line`.
Example:
def _keep_original_ws(s, tag_s):
"""Replace whitespace with the original whitespace characters in `s`"""
return ''.join(
c if tag_c == " " and c.isspace() else tag_c
for c, tag_c in zip(s, tag_s)
)
>>> _count_leading(' abc', ' ')
3
"""
i, n = 0, len(line)
while i < n and line[i] == ch:
i += 1
return i
class Differ:
r"""
@@ -1033,7 +1028,7 @@ class Differ:
def _qformat(self, aline, bline, atags, btags):
r"""
Format "?" output and deal with leading tabs.
Format "?" output and deal with tabs.
Example:
@@ -1047,22 +1042,16 @@ class Differ:
'+ \tabcdefGhijkl\n'
'? \t ^ ^ ^\n'
"""
# Can hurt, but will probably help most of the time.
common = min(_count_leading(aline, "\t"),
_count_leading(bline, "\t"))
common = min(common, _count_leading(atags[:common], " "))
common = min(common, _count_leading(btags[:common], " "))
atags = atags[common:].rstrip()
btags = btags[common:].rstrip()
atags = _keep_original_ws(aline, atags).rstrip()
btags = _keep_original_ws(bline, btags).rstrip()
yield "- " + aline
if atags:
yield "? %s%s\n" % ("\t" * common, atags)
yield f"? {atags}\n"
yield "+ " + bline
if btags:
yield "? %s%s\n" % ("\t" * common, btags)
yield f"? {btags}\n"
# With respect to junk, an earlier version of ndiff simply refused to
# *start* a match with a junk element. The result was cases like this:
@@ -1085,7 +1074,7 @@ import re
def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
r"""
Return 1 for ignorable line: iff `line` is blank or contains a single '#'.
Return True for ignorable line: iff `line` is blank or contains a single '#'.
Examples:
@@ -1101,7 +1090,7 @@ def IS_LINE_JUNK(line, pat=re.compile(r"\s*(?:#\s*)?$").match):
def IS_CHARACTER_JUNK(ch, ws=" \t"):
r"""
Return 1 for ignorable character: iff `ch` is a space or tab.
Return True for ignorable character: iff `ch` is a space or tab.
Examples:

View File

@@ -85,8 +85,6 @@ class TestSFbugs(unittest.TestCase):
self.assertEqual(second[1].size, 2)
self.assertEqual(second[2].size, 0)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_added_tab_hint(self):
# Check fix for bug #1488943
diff = list(difflib.Differ().compare(["\tI am a buggy"],["\t\tI am a bug"]))
@@ -95,8 +93,6 @@ class TestSFbugs(unittest.TestCase):
self.assertEqual("+ \t\tI am a bug", diff[2])
self.assertEqual("? +\n", diff[3])
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_hint_indented_properly_with_tabs(self):
diff = list(difflib.Differ().compare(["\t \t \t^"], ["\t \t \t^\n"]))
self.assertEqual("- \t \t \t^", diff[0])