Use io.open in approvalTests.py regardless of Python version

Both Python 2.7 and 3.x support full-featured io.open, so we
can avoid using a polyfill over this.
This commit is contained in:
Martin Hořeňovský 2018-04-19 22:02:31 +02:00
parent dc6b83bec9
commit 375f2052bd
1 changed files with 5 additions and 10 deletions

View File

@ -2,6 +2,7 @@
from __future__ import print_function from __future__ import print_function
import io
import os import os
import sys import sys
import subprocess import subprocess
@ -72,17 +73,11 @@ else:
overallResult = 0 overallResult = 0
def openFile(file, mode):
try:
return open(file, mode, encoding='utf-8', errors='surrogateescape')
except TypeError:
import io
return io.open(file, mode, encoding='utf-8', errors='surrogateescape')
def diffFiles(fileA, fileB): def diffFiles(fileA, fileB):
with openFile(fileA, 'r') as file: with io.open(fileA, 'r', encoding='utf-8', errors='surrogateescape') as file:
aLines = [line.rstrip() for line in file.readlines()] aLines = [line.rstrip() for line in file.readlines()]
with openFile(fileB, 'r') as file: with io.open(fileB, 'r', encoding='utf-8', errors='surrogateescape') as file:
bLines = [line.rstrip() for line in file.readlines()] bLines = [line.rstrip() for line in file.readlines()]
shortenedFilenameA = fileA.rsplit(os.sep, 1)[-1] shortenedFilenameA = fileA.rsplit(os.sep, 1)[-1]
@ -146,8 +141,8 @@ def approve(baseName, args):
subprocess.call(args, stdout=f, stderr=f) subprocess.call(args, stdout=f, stderr=f)
f.close() f.close()
rawFile = openFile(rawResultsPath, 'r') rawFile = io.open(rawResultsPath, 'r', encoding='utf-8', errors='surrogateescape')
filteredFile = openFile(filteredResultsPath, 'w') filteredFile = io.open(filteredResultsPath, 'w', encoding='utf-8', errors='surrogateescape')
for line in rawFile: for line in rawFile:
filteredFile.write(filterLine(line).rstrip() + "\n") filteredFile.write(filterLine(line).rstrip() + "\n")
filteredFile.close() filteredFile.close()