bpo-33338: [tokenize] Minor code cleanup (#6573) · python/cpython@c2d384d
@@ -28,7 +28,6 @@
2828from codecs import lookup, BOM_UTF8
2929import collections
3030from io import TextIOWrapper
31-from itertools import chain
3231import itertools as _itertools
3332import re
3433import sys
@@ -278,7 +277,7 @@ def compat(self, token, iterable):
278277startline = token[0] in (NEWLINE, NL)
279278prevstring = False
280279281-for tok in chain([token], iterable):
280+for tok in _itertools.chain([token], iterable):
282281toknum, tokval = tok[:2]
283282if toknum == ENCODING:
284283self.encoding = tokval
@@ -475,13 +474,10 @@ def tokenize(readline):
475474 The first token sequence will always be an ENCODING token
476475 which tells you which encoding was used to decode the bytes stream.
477476 """
478-# This import is here to avoid problems when the itertools module is not
479-# built yet and tokenize is imported.
480-from itertools import chain, repeat
481477encoding, consumed = detect_encoding(readline)
482-rl_gen = iter(readline, b"")
483-empty = repeat(b"")
484-return _tokenize(chain(consumed, rl_gen, empty).__next__, encoding)
478+empty = _itertools.repeat(b"")
479+rl_gen = _itertools.chain(consumed, iter(readline, b""), empty)
480+return _tokenize(rl_gen.__next__, encoding)
485481486482487483def _tokenize(readline, encoding):
@@ -496,7 +492,7 @@ def _tokenize(readline, encoding):
496492# BOM will already have been stripped.
497493encoding = "utf-8"
498494yield TokenInfo(ENCODING, encoding, (0, 0), (0, 0), '')
499-while True: # loop over lines in stream
495+while True: # loop over lines in stream
500496try:
501497line = readline()
502498except StopIteration:
@@ -581,7 +577,7 @@ def _tokenize(readline, encoding):
581577continue
582578token, initial = line[start:end], line[start]
583579584-if (initial in numchars or # ordinary number
580+if (initial in numchars or # ordinary number
585581 (initial == '.' and token != '.' and token != '...')):
586582yield TokenInfo(NUMBER, token, spos, epos, line)
587583elif initial in '\r\n':
@@ -667,7 +663,8 @@ def main():
667663668664# Helper error handling routines
669665def perror(message):
670-print(message, file=sys.stderr)
666+sys.stderr.write(message)
667+sys.stderr.write('\n')
671668672669def error(message, filename=None, location=None):
673670if location: