Skip to content

Commit 722d96f

Browse files
[lit] Remove python 2.7 code paths in builtin diff
Lit's builtin diff command had some Python 2.7 code paths lying around that we can probably get rid of at this point. LLVM at this point requires Python 3.8 at minimum. Keeping lit working at a lower version is a reasonable goal, but I think we can probably drop python 2 support at this point given how long it has been deprecated and how long LLVM has supported Python 3. Reviewers: jdenny-ornl, ilovepi, petrhosek Reviewed By: ilovepi Pull Request: llvm/llvm-project#157558
1 parent 36baf24 commit 722d96f

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

lit/builtin_commands/diff.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,15 @@ def compareTwoFiles(flags, filepaths):
5959

6060
def compareTwoBinaryFiles(flags, filepaths, filelines):
6161
exitCode = 0
62-
if hasattr(difflib, "diff_bytes"):
63-
# python 3.5 or newer
64-
diffs = difflib.diff_bytes(
65-
difflib.unified_diff,
66-
filelines[0],
67-
filelines[1],
68-
filepaths[0].encode(),
69-
filepaths[1].encode(),
70-
n=flags.num_context_lines,
71-
)
72-
diffs = [diff.decode(errors="backslashreplace") for diff in diffs]
73-
else:
74-
# python 2.7
75-
if flags.unified_diff:
76-
func = difflib.unified_diff
77-
else:
78-
func = difflib.context_diff
79-
diffs = func(
80-
filelines[0],
81-
filelines[1],
82-
filepaths[0],
83-
filepaths[1],
84-
n=flags.num_context_lines,
85-
)
62+
diffs = difflib.diff_bytes(
63+
difflib.unified_diff,
64+
filelines[0],
65+
filelines[1],
66+
filepaths[0].encode(),
67+
filepaths[1].encode(),
68+
n=flags.num_context_lines,
69+
)
70+
diffs = [diff.decode(errors="backslashreplace") for diff in diffs]
8671

8772
for diff in diffs:
8873
sys.stdout.write(to_string(diff))
@@ -230,14 +215,8 @@ def compareDirTrees(flags, dir_trees, base_paths=["", ""]):
230215

231216
def main(argv):
232217
if sys.platform == "win32":
233-
if hasattr(sys.stdout, "buffer"):
234-
# python 3
235-
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, newline="\n")
236-
else:
237-
# python 2.7
238-
import msvcrt
218+
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, newline="\n")
239219

240-
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
241220
args = argv[1:]
242221
try:
243222
opts, args = getopt.gnu_getopt(args, "wbuI:U:r", ["strip-trailing-cr"])

0 commit comments

Comments
 (0)