Python StdinStream/FileStream -> UTF-8

This commit is contained in:
Ben Hamilton 2017-01-25 16:33:14 -08:00
parent 0901851719
commit 99ed4b6de6
3 changed files with 9 additions and 9 deletions

View File

@ -16,12 +16,12 @@ from antlr4.InputStream import InputStream
class FileStream(InputStream):
def __init__(self, fileName, encoding='ascii'):
def __init__(self, fileName, encoding='utf-8', errors='replace'):
self.fileName = fileName
# read binary to avoid line ending conversion
with open(fileName, 'rb') as file:
bytes = file.read()
data = codecs.decode(bytes, encoding)
data = codecs.decode(bytes, encoding, errors)
super(type(self), self).__init__(data)

View File

@ -15,7 +15,7 @@ from antlr4.InputStream import InputStream
class StdinStream(InputStream):
def __init__(self, encoding='ascii'):
def __init__(self, encoding='utf-8', errors='replace'):
bytes = sys.stdin.read()
data = codecs.decode(bytes, encoding)
data = codecs.decode(bytes, encoding, errors)
super(type(self), self).__init__(data)

View File

@ -16,15 +16,15 @@ from antlr4.InputStream import InputStream
class FileStream(InputStream):
def __init__(self, fileName:str, encoding:str='ascii'):
super().__init__(self.readDataFrom(fileName, encoding))
def __init__(self, fileName:str, encoding:str='utf-8', errors:str='replace'):
super().__init__(self.readDataFrom(fileName, encoding, errors))
self.fileName = fileName
def readDataFrom(self, fileName:str, encoding:str):
def readDataFrom(self, fileName:str, encoding:str, errors:str='replace'):
# read binary to avoid line ending conversion
with open(fileName, 'rb') as file:
bytes = file.read()
return codecs.decode(bytes, encoding)
return codecs.decode(bytes, encoding, errors)
class TestFileStream(unittest.TestCase):