forked from jasder/antlr
Python StdinStream/FileStream -> UTF-8
This commit is contained in:
parent
0901851719
commit
99ed4b6de6
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
super(type(self), self).__init__(data)
|
||||
data = codecs.decode(bytes, encoding, errors)
|
||||
super(type(self), self).__init__(data)
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue