@@ -77,9 +77,8 @@ def check(file):
7777 if verbose > 1 :
7878 print "checking" , `file` , "..."
7979
80- reset_globals ()
8180 try :
82- tokenize .tokenize (f .readline , tokeneater )
81+ process_tokens ( tokenize .generate_tokens (f .readline ) )
8382
8483 except tokenize .TokenError , msg :
8584 errprint ("%s: Token Error: %s" % (`file` , str (msg )))
@@ -244,28 +243,19 @@ def format_witnesses(w):
244243 prefix = prefix + "s"
245244 return prefix + " " + string .join (firsts , ', ' )
246245
247- # The collection of globals, the reset_globals() function, and the
248- # tokeneater() function, depend on which version of tokenize is
249- # in use.
246+ # Need Guido's enhancement
247+ assert hasattr (tokenize , 'NL' ), "tokenize module too old"
250248
251- if hasattr (tokenize , 'NL' ):
252- # take advantage of Guido's patch!
253-
254- indents = []
255- check_equal = 0
256-
257- def reset_globals ():
258- global indents , check_equal
259- check_equal = 0
260- indents = [Whitespace ("" )]
261-
262- def tokeneater (type , token , start , end , line ,
249+ def process_tokens (tokens ,
263250 INDENT = tokenize .INDENT ,
264251 DEDENT = tokenize .DEDENT ,
265252 NEWLINE = tokenize .NEWLINE ,
266- JUNK = (tokenize .COMMENT , tokenize .NL ) ):
267- global indents , check_equal
253+ JUNK = (tokenize .COMMENT , tokenize .NL )):
268254
255+ indents = [Whitespace ("" )]
256+ check_equal = 0
257+
258+ for (type , token , start , end , line ) in tokens :
269259 if type == NEWLINE :
270260 # a program statement, or ENDMARKER, will eventually follow,
271261 # after some (possibly empty) run of tokens of the form
@@ -311,62 +301,6 @@ def tokeneater(type, token, start, end, line,
311301 msg = "indent not equal e.g. " + format_witnesses (witness )
312302 raise NannyNag (start [0 ], msg , line )
313303
314- else :
315- # unpatched version of tokenize
316-
317- nesting_level = 0
318- indents = []
319- check_equal = 0
320-
321- def reset_globals ():
322- global nesting_level , indents , check_equal
323- nesting_level = check_equal = 0
324- indents = [Whitespace ("" )]
325-
326- def tokeneater (type , token , start , end , line ,
327- INDENT = tokenize .INDENT ,
328- DEDENT = tokenize .DEDENT ,
329- NEWLINE = tokenize .NEWLINE ,
330- COMMENT = tokenize .COMMENT ,
331- OP = tokenize .OP ):
332- global nesting_level , indents , check_equal
333-
334- if type == INDENT :
335- check_equal = 0
336- thisguy = Whitespace (token )
337- if not indents [- 1 ].less (thisguy ):
338- witness = indents [- 1 ].not_less_witness (thisguy )
339- msg = "indent not greater e.g. " + format_witnesses (witness )
340- raise NannyNag (start [0 ], msg , line )
341- indents .append (thisguy )
342-
343- elif type == DEDENT :
344- del indents [- 1 ]
345-
346- elif type == NEWLINE :
347- if nesting_level == 0 :
348- check_equal = 1
349-
350- elif type == COMMENT :
351- pass
352-
353- elif check_equal :
354- check_equal = 0
355- thisguy = Whitespace (line )
356- if not indents [- 1 ].equal (thisguy ):
357- witness = indents [- 1 ].not_equal_witness (thisguy )
358- msg = "indent not equal e.g. " + format_witnesses (witness )
359- raise NannyNag (start [0 ], msg , line )
360-
361- if type == OP and token in ('{' , '[' , '(' ):
362- nesting_level = nesting_level + 1
363-
364- elif type == OP and token in ('}' , ']' , ')' ):
365- if nesting_level == 0 :
366- raise NannyNag (start [0 ],
367- "unbalanced bracket '" + token + "'" ,
368- line )
369- nesting_level = nesting_level - 1
370304
371305if __name__ == '__main__' :
372306 main ()
0 commit comments