summaryrefslogtreecommitdiffstats
path: root/llvm/utils/lit
diff options
context:
space:
mode:
authorAaron Smith <aaron.smith@microsoft.com>2018-04-02 22:08:56 +0000
committerAaron Smith <aaron.smith@microsoft.com>2018-04-02 22:08:56 +0000
commit98d31d688013e12bd8e5185a1971b593cb3fe15e (patch)
tree44ae7a5e6111e35fa3c9a606cb314058007f1c80 /llvm/utils/lit
parent98315aad60537bdc83ca38c8e27ed9ad10555776 (diff)
downloadbcm5719-llvm-98d31d688013e12bd8e5185a1971b593cb3fe15e.tar.gz
bcm5719-llvm-98d31d688013e12bd8e5185a1971b593cb3fe15e.zip
[lit] Fix problem in how Python versions open files with different encodings
Reapply D43165 which was reverted because of different versions of python failing. The one line fix for the different python versions was commited at the same time that D43165 was reverted. If this change is giving you issues then get in touch with your python version and we will fix it. llvm-svn: 329022
Diffstat (limited to 'llvm/utils/lit')
-rw-r--r--llvm/utils/lit/lit/TestRunner.py42
1 files changed, 41 insertions, 1 deletions
diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py
index 757d01ac47d..02745e62dcf 100644
--- a/llvm/utils/lit/lit/TestRunner.py
+++ b/llvm/utils/lit/lit/TestRunner.py
@@ -388,7 +388,47 @@ def executeBuiltinDiff(cmd, cmd_shenv):
def compareTwoFiles(filepaths):
filelines = []
for file in filepaths:
- with io.open(file, 'r') as f:
+ try:
+ with open(file, 'r') as f:
+ filelines.append(f.readlines())
+ except UnicodeDecodeError:
+ try:
+ with open(file, 'r', encoding="utf-8") as f:
+ filelines.append(f.readlines())
+ encoding = "utf-8"
+ except:
+ compare_bytes = True
+
+ if compare_bytes:
+ return compareTwoBinaryFiles(filepaths)
+ else:
+ return compareTwoTextFiles(filepaths, encoding)
+
+ def compareTwoBinaryFiles(filepaths):
+ filelines = []
+ for file in filepaths:
+ with open(file, 'rb') as f:
+ filelines.append(f.readlines())
+
+ exitCode = 0
+ if hasattr(difflib, 'diff_bytes'):
+ # python 3.5 or newer
+ diffs = difflib.diff_bytes(difflib.unified_diff, filelines[0], filelines[1], filepaths[0].encode(), filepaths[1].encode())
+ diffs = [diff.decode() for diff in diffs]
+ else:
+ # python 2.7
+ func = difflib.unified_diff if unified_diff else difflib.context_diff
+ diffs = func(filelines[0], filelines[1], filepaths[0], filepaths[1])
+
+ for diff in diffs:
+ stdout.write(diff)
+ exitCode = 1
+ return exitCode
+
+ def compareTwoTextFiles(filepaths, encoding):
+ filelines = []
+ for file in filepaths:
+ with io.open(file, 'r', encoding=encoding) as f:
filelines.append(f.readlines())
exitCode = 0
OpenPOWER on IntegriCloud