Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e1c638da
Commit
e1c638da
authored
Aug 21, 2019
by
Anthony Sottile
Committed by
Tim Peters
Aug 21, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix difflib `?` hint in diff output when dealing with tabs (#15201)
parent
092911d5
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
20 additions
and
23 deletions
+20
-23
Lib/difflib.py
Lib/difflib.py
+11
-22
Lib/test/test_difflib.py
Lib/test/test_difflib.py
+7
-1
Misc/NEWS.d/next/Library/2019-08-10-12-33-27.bpo-37810.d4zbvB.rst
...S.d/next/Library/2019-08-10-12-33-27.bpo-37810.d4zbvB.rst
+2
-0
No files found.
Lib/difflib.py
View file @
e1c638da
...
@@ -733,20 +733,15 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
...
@@ -733,20 +733,15 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
# Strip scores for the best n matches
# Strip scores for the best n matches
return
[
x
for
score
,
x
in
result
]
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
:
class
Differ
:
r"""
r"""
...
@@ -1033,7 +1028,7 @@ class Differ:
...
@@ -1033,7 +1028,7 @@ class Differ:
def
_qformat
(
self
,
aline
,
bline
,
atags
,
btags
):
def
_qformat
(
self
,
aline
,
bline
,
atags
,
btags
):
r"""
r"""
Format "?" output and deal with
leading
tabs.
Format "?" output and deal with tabs.
Example:
Example:
...
@@ -1047,22 +1042,16 @@ class Differ:
...
@@ -1047,22 +1042,16 @@ class Differ:
'+ \tabcdefGhijkl\n'
'+ \tabcdefGhijkl\n'
'? \t ^ ^ ^\n'
'? \t ^ ^ ^\n'
"""
"""
atags
=
_keep_original_ws
(
aline
,
atags
).
rstrip
()
# Can hurt, but will probably help most of the time.
btags
=
_keep_original_ws
(
bline
,
btags
).
rstrip
()
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
()
yield
"- "
+
aline
yield
"- "
+
aline
if
atags
:
if
atags
:
yield
"? %s%s
\
n
"
%
(
"
\
t
"
*
common
,
atags
)
yield
f"?
{
atags
}\
n
"
yield
"+ "
+
bline
yield
"+ "
+
bline
if
btags
:
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
# 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:
# *start* a match with a junk element. The result was cases like this:
...
...
Lib/test/test_difflib.py
View file @
e1c638da
...
@@ -89,10 +89,16 @@ class TestSFbugs(unittest.TestCase):
...
@@ -89,10 +89,16 @@ class TestSFbugs(unittest.TestCase):
# Check fix for bug #1488943
# Check fix for bug #1488943
diff
=
list
(
difflib
.
Differ
().
compare
([
"
\
t
I am a buggy"
],[
"
\
t
\
t
I am a bug"
]))
diff
=
list
(
difflib
.
Differ
().
compare
([
"
\
t
I am a buggy"
],[
"
\
t
\
t
I am a bug"
]))
self
.
assertEqual
(
"-
\
t
I am a buggy"
,
diff
[
0
])
self
.
assertEqual
(
"-
\
t
I am a buggy"
,
diff
[
0
])
self
.
assertEqual
(
"?
--
\
n
"
,
diff
[
1
])
self
.
assertEqual
(
"?
\
t
--
\
n
"
,
diff
[
1
])
self
.
assertEqual
(
"+
\
t
\
t
I am a bug"
,
diff
[
2
])
self
.
assertEqual
(
"+
\
t
\
t
I am a bug"
,
diff
[
2
])
self
.
assertEqual
(
"? +
\
n
"
,
diff
[
3
])
self
.
assertEqual
(
"? +
\
n
"
,
diff
[
3
])
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
])
self
.
assertEqual
(
"+
\
t
\
t
\
t
^
\
n
"
,
diff
[
1
])
self
.
assertEqual
(
"?
\
t
\
t
\
t
+
\
n
"
,
diff
[
2
])
def
test_mdiff_catch_stop_iteration
(
self
):
def
test_mdiff_catch_stop_iteration
(
self
):
# Issue #33224
# Issue #33224
self
.
assertEqual
(
self
.
assertEqual
(
...
...
Misc/NEWS.d/next/Library/2019-08-10-12-33-27.bpo-37810.d4zbvB.rst
0 → 100644
View file @
e1c638da
Fix :mod:`difflib` ``?`` hint in diff output when dealing with tabs. Patch
by Anthony Sottile.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment