Lgtm<br><div class="gmail_quote"><div dir="ltr">On Mon, Apr 2, 2018 at 12:21 PM Aaron Smith via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">asmith updated this revision to Diff 140661.<br>
asmith edited the summary of this revision.<br>
asmith added a comment.<br>
<br>
This is changed based on the reviewers suggestions. First open as usual and on failure reopen as UTF8 and when that fails reopen as binary. Verified on Windows and Linux.<br>
<br>
<br>
<a href="https://reviews.llvm.org/D43165" rel="noreferrer" target="_blank">https://reviews.llvm.org/D43165</a><br>
<br>
Files:<br>
lit/lit/TestRunner.py<br>
<br>
<br>
Index: lit/lit/TestRunner.py<br>
===================================================================<br>
--- lit/lit/TestRunner.py<br>
+++ lit/lit/TestRunner.py<br>
@@ -2,6 +2,7 @@<br>
import difflib<br>
import errno<br>
import functools<br>
+import io<br>
import itertools<br>
import getopt<br>
import os, signal, subprocess, sys<br>
@@ -385,9 +386,51 @@<br>
return path, sorted(child_trees)<br>
<br>
def compareTwoFiles(filepaths):<br>
+ compare_bytes = False<br>
+ encoding = None<br>
filelines = []<br>
for file in filepaths:<br>
- with open(file, 'r') as f:<br>
+ try:<br>
+ with open(file, 'r') as f:<br>
+ filelines.append(f.readlines())<br>
+ except UnicodeDecodeError:<br>
+ try:<br>
+ with open(file, 'r', encoding="utf-8") as f:<br>
+ filelines.append(f.readlines())<br>
+ encoding = "utf-8"<br>
+ except:<br>
+ compare_bytes = True<br>
+<br>
+ if compare_bytes:<br>
+ return compareTwoBinaryFiles(filepaths)<br>
+ else:<br>
+ return compareTwoTextFiles(filepaths, encoding)<br>
+<br>
+ def compareTwoBinaryFiles(filepaths):<br>
+ filelines = []<br>
+ for file in filepaths:<br>
+ with open(file, 'rb') as f:<br>
+ filelines.append(f.readlines())<br>
+<br>
+ exitCode = 0<br>
+ if hasattr(difflib, 'diff_bytes'):<br>
+ # python 3.5 or newer<br>
+ diffs = difflib.diff_bytes(difflib.unified_diff, filelines[0], filelines[1], filepaths[0].encode(), filepaths[1].encode())<br>
+ diffs = [diff.decode() for diff in diffs]<br>
+ else:<br>
+ # python 2.7<br>
+ func = difflib.unified_diff if unified_diff else difflib.context_diff<br>
+ diffs = func(filelines[0], filelines[1], filepaths[0], filepaths[1])<br>
+<br>
+ for diff in diffs:<br>
+ stdout.write(diff)<br>
+ exitCode = 1<br>
+ return exitCode<br>
+<br>
+ def compareTwoTextFiles(filepaths, encoding):<br>
+ filelines = []<br>
+ for file in filepaths:<br>
+ with open(file, 'r', encoding=encoding) as f:<br>
filelines.append(f.readlines())<br>
<br>
exitCode = 0<br>
<br>
<br>
</blockquote></div>