Browse Source

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
Joshua Harlow 4 years ago
parent
commit
674cbbcd59
1 changed files with 9 additions and 6 deletions
  1. 9
    6
      doc8/parser.py

+ 9
- 6
doc8/parser.py View File

@@ -35,6 +35,7 @@ class ParsedFile(object):
35 35
         self._encoding = encoding
36 36
         self._doc = None
37 37
         self._errors = None
38
+        self._lines = None
38 39
         self._extension = os.path.splitext(filename)[1]
39 40
 
40 41
     @property
@@ -72,12 +73,14 @@ class ParsedFile(object):
72 73
         return self._doc
73 74
 
74 75
     def lines_iter(self, remove_trailing_newline=True):
75
-        with open(self.filename, 'rb') as fh:
76
-            for line in fh:
77
-                line = six.text_type(line, encoding=self.encoding)
78
-                if remove_trailing_newline and line.endswith("\n"):
79
-                    line = line[0:-1]
80
-                yield line
76
+        if self._lines is None:
77
+            with open(self.filename, 'rb') as fh:
78
+                self._lines = list(fh)
79
+        for line in self._lines:
80
+            line = six.text_type(line, encoding=self.encoding)
81
+            if remove_trailing_newline and line.endswith("\n"):
82
+                line = line[0:-1]
83
+            yield line
81 84
 
82 85
     @property
83 86
     def extension(self):

Loading…
Cancel
Save