Skip to content

Commit

Permalink
Fix --diff output when encountering EOF
Browse files Browse the repository at this point in the history
`split("\n")` includes a final empty element `""` if the final line
ends with `\n` (as it should for POSIX-compliant text files), which
then became an extra `"\n"`.

`splitlines()` solves that, but there's a caveat, as it will split
on other types of line breaks too (like `\r`), which may not be
desired.

Fixes psf#526.
  • Loading branch information
akien-mga committed Apr 2, 2020
1 parent 9ed2542 commit fca7df5
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions black.py
Expand Up @@ -3876,8 +3876,8 @@ def diff(a: str, b: str, a_name: str, b_name: str) -> str:
"""Return a unified diff string between strings `a` and `b`."""
import difflib

a_lines = [line + "\n" for line in a.split("\n")]
b_lines = [line + "\n" for line in b.split("\n")]
a_lines = [line + "\n" for line in a.splitlines()]
b_lines = [line + "\n" for line in b.splitlines()]
return "".join(
difflib.unified_diff(a_lines, b_lines, fromfile=a_name, tofile=b_name, n=5)
)
Expand Down

0 comments on commit fca7df5

Please sign in to comment.