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

View File

@ -2,6 +2,7 @@
from __future__ import print_function
import io
import os
import sys
import subprocess
@ -72,17 +73,11 @@ else:
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):
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()]
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()]
shortenedFilenameA = fileA.rsplit(os.sep, 1)[-1]
@ -146,8 +141,8 @@ def approve(baseName, args):
subprocess.call(args, stdout=f, stderr=f)
f.close()
rawFile = openFile(rawResultsPath, 'r')
filteredFile = openFile(filteredResultsPath, 'w')
rawFile = io.open(rawResultsPath, 'r', encoding='utf-8', errors='surrogateescape')
filteredFile = io.open(filteredResultsPath, 'w', encoding='utf-8', errors='surrogateescape')
for line in rawFile:
filteredFile.write(filterLine(line).rstrip() + "\n")
filteredFile.close()