From 061354b586bb03b9869565334be88d7997b36d95 Mon Sep 17 00:00:00 2001 From: Aaron Smith Date: Tue, 3 Apr 2018 00:22:12 +0000 Subject: [PATCH] [lit] Prefer opening files with open (Python 2) rather than io.open which requires io. Only rely on Python 3 (io.open) when necessary. This puts TestRunnyer.py closer to how it behaved before the changes introduced in D43165 and silences a few Windows build bot failures. Thanks to Stella Stamenova for the patch! llvm-svn: 329037 --- llvm/utils/lit/lit/TestRunner.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/llvm/utils/lit/lit/TestRunner.py b/llvm/utils/lit/lit/TestRunner.py index f0049526cce7..5eecb80e2f38 100644 --- a/llvm/utils/lit/lit/TestRunner.py +++ b/llvm/utils/lit/lit/TestRunner.py @@ -392,14 +392,12 @@ def executeBuiltinDiff(cmd, cmd_shenv): encoding = None filelines = [] for file in filepaths: - compare_bytes = False - encoding = None try: with open(file, 'r') as f: filelines.append(f.readlines()) except UnicodeDecodeError: try: - with open(file, 'r', encoding="utf-8") as f: + with io.open(file, 'r', encoding="utf-8") as f: filelines.append(f.readlines()) encoding = "utf-8" except: @@ -416,7 +414,7 @@ def executeBuiltinDiff(cmd, cmd_shenv): with open(file, 'rb') as f: filelines.append(f.readlines()) - exitCode = 0 + 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()) @@ -434,10 +432,14 @@ def executeBuiltinDiff(cmd, cmd_shenv): def compareTwoTextFiles(filepaths, encoding): filelines = [] for file in filepaths: - with io.open(file, 'r', encoding=encoding) as f: - filelines.append(f.readlines()) + if encoding is None: + with open(file, 'r') as f: + filelines.append(f.readlines()) + else: + with io.open(file, 'r', encoding=encoding) as f: + filelines.append(f.readlines()) - exitCode = 0 + exitCode = 0 def compose2(f, g): return lambda x: f(g(x))