From 674cbbcd597d732bca5ac0e465d1a7162e416023 Mon Sep 17 00:00:00 2001 From: Joshua Harlow Date: Sat, 6 Sep 2014 17:21:12 -0700 Subject: [PATCH] Cache the lines that are read instead of re-reading With-in the parsed file we cache the contents of the file before it is converted to unicode and the contents of the file after it is converted to unicode so it seems better to follow this pattern and to also cache the raw lines (instead of re-reading) as well. Change-Id: Ic320a6c5517e513c88bdb4014d509a5791d85524 --- doc8/parser.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/doc8/parser.py b/doc8/parser.py index aeffc2d..7a88520 100644 --- a/doc8/parser.py +++ b/doc8/parser.py @@ -35,6 +35,7 @@ class ParsedFile(object): self._encoding = encoding self._doc = None self._errors = None + self._lines = None self._extension = os.path.splitext(filename)[1] @property @@ -72,12 +73,14 @@ class ParsedFile(object): return self._doc def lines_iter(self, remove_trailing_newline=True): - with open(self.filename, 'rb') as fh: - for line in fh: - line = six.text_type(line, encoding=self.encoding) - if remove_trailing_newline and line.endswith("\n"): - line = line[0:-1] - yield line + if self._lines is None: + with open(self.filename, 'rb') as fh: + self._lines = list(fh) + for line in self._lines: + line = six.text_type(line, encoding=self.encoding) + if remove_trailing_newline and line.endswith("\n"): + line = line[0:-1] + yield line @property def extension(self):