]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #20387: Backport fix from Python 3.4
authorJason R. Coombs <jaraco@jaraco.com>
Sun, 28 Jun 2015 17:05:19 +0000 (13:05 -0400)
committerJason R. Coombs <jaraco@jaraco.com>
Sun, 28 Jun 2015 17:05:19 +0000 (13:05 -0400)
Lib/tokenize.py
Misc/NEWS

index 661ddeb8e6b5cd167013a34fbc211b402d4b1b90..d426cd2df52a04fb6bafd2b15d7c50a75f5776ce 100644 (file)
@@ -198,6 +198,8 @@ class Untokenizer:
 
     def untokenize(self, iterable):
         it = iter(iterable)
+        indents = []
+        startline = False
         for t in it:
             if len(t) == 2:
                 self.compat(t, it)
@@ -205,6 +207,21 @@ class Untokenizer:
             tok_type, token, start, end, line = t
             if tok_type == ENDMARKER:
                 break
+            if tok_type == INDENT:
+                indents.append(token)
+                continue
+            elif tok_type == DEDENT:
+                indents.pop()
+                self.prev_row, self.prev_col = end
+                continue
+            elif tok_type in (NEWLINE, NL):
+                startline = True
+            elif startline and indents:
+                indent = indents[-1]
+                if start[1] >= len(indent):
+                    self.tokens.append(indent)
+                    self.prev_col = len(indent)
+                startline = False
             self.add_whitespace(start)
             self.tokens.append(token)
             self.prev_row, self.prev_col = end
index 9418ad257dabd5bb0a13e258c50953e061aeea8f..92e08a9219bbd07901ac6f722b4351d5be9d19e1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -30,6 +30,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #20387: Restore semantic round-trip correctness in tokenize/untokenize
+  for tab-indented blocks.
+
 - Issue #24456: Fixed possible buffer over-read in adpcm2lin() and lin2adpcm()
   functions of the audioop module.  Fixed SystemError when the state is not a
   tuple.  Fixed possible memory leak.