Skip to content

Commit b292711

Browse files
committed
Use iterators instead of allocating extra lists
1 parent 13dc706 commit b292711

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

bpython/curtsiesfrontend/preprocess.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
etc)"""
33

44
from ..lazyre import LazyReCompile
5+
from itertools import tee, islice, chain
56

67
# TODO specifically catch IndentationErrors instead of any syntax errors
78

8-
99
indent_empty_lines_re = LazyReCompile(r"\s*")
1010
tabs_to_spaces_re = LazyReCompile(r"^\t+")
1111

@@ -21,7 +21,11 @@ def indent_empty_lines(s, compiler):
2121
lines.pop()
2222
result_lines = []
2323

24-
for p_line, line, n_line in zip([""] + lines[:-1], lines, lines[1:] + [""]):
24+
prevs, lines, nexts = tee(lines, 3)
25+
prevs = chain(("",), prevs)
26+
nexts = chain(islice(nexts, 1, None), ("",))
27+
28+
for p_line, line, n_line in zip(prevs, lines, nexts):
2529
if len(line) == 0:
2630
p_indent = indent_empty_lines_re.match(p_line).group()
2731
n_indent = indent_empty_lines_re.match(n_line).group()

0 commit comments

Comments
 (0)