From 4c65368056170eaf5d696738232e154455f27db0 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 27 Mar 2019 01:02:27 +0100 Subject: [PATCH] Some minor changes to file_io --- parso/file_io.py | 10 ++++++++-- parso/grammar.py | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/parso/file_io.py b/parso/file_io.py index dda48bb..b9a24f4 100644 --- a/parso/file_io.py +++ b/parso/file_io.py @@ -5,8 +5,11 @@ class FileIO: def __init__(self, path): self.path = path - def read(self): - with open(self.path) as f: + def read(self): # Returns bytes/str + # We would like to read unicode here, but we cannot, because we are not + # sure if it is a valid unicode file. Therefore just read whatever is + # here. + with open(self.path, 'rb') as f: return f.read() def get_last_modified(self): @@ -16,6 +19,9 @@ class FileIO: """ return os.path.getmtime(self.path) + def __repr__(self): + return '%s(%s)' % (self.__class__.__name__, self.path) + class KnownContentFileIO(FileIO): def __init__(self, path, content): diff --git a/parso/grammar.py b/parso/grammar.py index 938439d..68a944a 100644 --- a/parso/grammar.py +++ b/parso/grammar.py @@ -105,7 +105,8 @@ class Grammar(object): if module_node is not None: return module_node - code = file_io.read() + if code is None: + code = file_io.read() code = python_bytes_to_unicode(code) lines = split_lines(code, keepends=True)