PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/runtime/Python2/src/antlr4/FileStream.py

https://github.com/antlr/antlr4
Python | 32 lines | 14 code | 8 blank | 10 comment | 1 complexity | 0cd7fea2f03a2ed46ed0ce67789e9d25 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #
  2. # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  3. # Use of this file is governed by the BSD 3-clause license that
  4. # can be found in the LICENSE.txt file in the project root.
  5. #
  6. #
  7. # This is an InputStream that is loaded from a file all at once
  8. # when you construct the object.
  9. #
  10. import codecs
  11. import unittest
  12. from antlr4.InputStream import InputStream
  13. class FileStream(InputStream):
  14. def __init__(self, fileName, encoding='ascii', errors='strict'):
  15. self.fileName = fileName
  16. # read binary to avoid line ending conversion
  17. with open(fileName, 'rb') as file:
  18. bytes = file.read()
  19. data = codecs.decode(bytes, encoding, errors)
  20. super(type(self), self).__init__(data)
  21. class TestFileStream(unittest.TestCase):
  22. def testStream(self):
  23. stream = FileStream("FileStream.py")
  24. self.assertTrue(stream.size>0)