2525 'Skip Montanaro, Raymond Hettinger, Trent Nelson, '
2626 'Michael Foord' )
2727import builtins
28- import re
29- import sys
30- from token import *
3128from codecs import lookup , BOM_UTF8
3229import collections
3330from io import TextIOWrapper
31+ from itertools import chain
32+ import re
33+ import sys
34+ from token import *
35+
3436cookie_re = re .compile (r'^[ \t\f]*#.*coding[:=][ \t]*([-\w.]+)' , re .ASCII )
3537blank_re = re .compile (br'^[ \t\f]*(?:[#\r\n]|$)' , re .ASCII )
3638
@@ -237,9 +239,10 @@ def add_whitespace(self, start):
237239 self .tokens .append (" " * col_offset )
238240
239241 def untokenize (self , iterable ):
240- for t in iterable :
242+ it = iter (iterable )
243+ for t in it :
241244 if len (t ) == 2 :
242- self .compat (t , iterable )
245+ self .compat (t , it )
243246 break
244247 tok_type , token , start , end , line = t
245248 if tok_type == ENCODING :
@@ -254,17 +257,12 @@ def untokenize(self, iterable):
254257 return "" .join (self .tokens )
255258
256259 def compat (self , token , iterable ):
257- startline = False
258260 indents = []
259261 toks_append = self .tokens .append
260- toknum , tokval = token
261-
262- if toknum in (NAME , NUMBER ):
263- tokval += ' '
264- if toknum in (NEWLINE , NL ):
265- startline = True
262+ startline = token [0 ] in (NEWLINE , NL )
266263 prevstring = False
267- for tok in iterable :
264+
265+ for tok in chain ([token ], iterable ):
268266 toknum , tokval = tok [:2 ]
269267 if toknum == ENCODING :
270268 self .encoding = tokval
0 commit comments