Skip to content

Commit e411b66

Browse files
committed
Issue #20387: Restore retention of indentation during untokenize.
1 parent b6d1cdd commit e411b66

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Lib/tokenize.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ def add_whitespace(self, start):
244244

245245
def untokenize(self, iterable):
246246
it = iter(iterable)
247+
indents = []
248+
startline = False
247249
for t in it:
248250
if len(t) == 2:
249251
self.compat(t, it)
@@ -254,6 +256,21 @@ def untokenize(self, iterable):
254256
continue
255257
if tok_type == ENDMARKER:
256258
break
259+
if tok_type == INDENT:
260+
indents.append(token)
261+
continue
262+
elif tok_type == DEDENT:
263+
indents.pop()
264+
self.prev_row, self.prev_col = end
265+
continue
266+
elif tok_type in (NEWLINE, NL):
267+
startline = True
268+
elif startline and indents:
269+
indent = indents[-1]
270+
if start[1] >= len(indent):
271+
self.tokens.append(indent)
272+
self.prev_col = len(indent)
273+
startline = False
257274
self.add_whitespace(start)
258275
self.tokens.append(token)
259276
self.prev_row, self.prev_col = end

0 commit comments

Comments
 (0)