<s> import __builtin__ <EOL> import os . path <EOL> import _ast <EOL> from pyflakes import messages <EOL> try : <EOL> import ast <EOL> iter_child_nodes = ast . iter_child_nodes <EOL> except ( ImportError , AttributeError ) : <EOL> def iter_child_nodes ( node , astcls = _ast . AST ) : <EOL> """<STR_LIT>""" <EOL> for name in node . _fields : <EOL> field = getattr ( node , name , None ) <EOL> if isinstance ( field , astcls ) : <EOL> yield field <EOL> elif isinstance ( field , list ) : <EOL> for item in field : <EOL> yield item <EOL> class Binding ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , source ) : <EOL> self . name = name <EOL> self . source = source <EOL> self . used = False <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> self . name , <EOL> self . source . lineno , <EOL> id ( self ) ) <EOL> class UnBinding ( Binding ) : <EOL> '''<STR_LIT>''' <EOL> class Importation ( Binding ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , source ) : <EOL> self . fullName = name <EOL> name = name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> super ( Importation , self ) . __init__ ( name , source ) <EOL> class Argument ( Binding ) : <EOL> """<STR_LIT>""" <EOL> class Assignment ( Binding ) : <EOL> """<STR_LIT>""" <EOL> class FunctionDefinition ( Binding ) : <EOL> _property_decorator = False <EOL> class ExportBinding ( Binding ) : <EOL> """<STR_LIT>""" <EOL> def names ( self ) : <EOL> """<STR_LIT>""" <EOL> names = [ ] <EOL> if isinstance ( self . source , _ast . List ) : <EOL> for node in self . source . elts : <EOL> if isinstance ( node , _ast . Str ) : <EOL> names . append ( node . s ) <EOL> return names <EOL> class Scope ( dict ) : <EOL> importStarred = False <EOL> usesLocals = False <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , id ( self ) , dict . __repr__ ( self ) ) <EOL> def __init__ ( self ) : <EOL> super ( Scope , self ) . __init__ ( ) <EOL> class ClassScope ( Scope ) : <EOL> pass <EOL> class FunctionScope ( Scope ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( FunctionScope , self ) . __init__ ( ) <EOL> self . globals = { } <EOL> class ModuleScope ( Scope ) : <EOL> pass <EOL> _MAGIC_GLOBALS = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Checker ( object ) : <EOL> """<STR_LIT>""" <EOL> nodeDepth = <NUM_LIT:0> <EOL> traceTree = False <EOL> def __init__ ( self , tree , filename = None ) : <EOL> if filename is None : <EOL> filename = '<STR_LIT>' <EOL> self . _deferredFunctions = [ ] <EOL> self . _deferredAssignments = [ ] <EOL> self . dead_scopes = [ ] <EOL> self . messages = [ ] <EOL> self . filename = filename <EOL> self . scopeStack = [ ModuleScope ( ) ] <EOL> self . futuresAllowed = True <EOL> self . root = tree <EOL> self . handleChildren ( tree ) <EOL> self . _runDeferred ( self . _deferredFunctions ) <EOL> self . _deferredFunctions = None <EOL> self . _runDeferred ( self . _deferredAssignments ) <EOL> self . _deferredAssignments = None <EOL> del self . scopeStack [ <NUM_LIT:1> : ] <EOL> self . popScope ( ) <EOL> self . check_dead_scopes ( ) <EOL> def deferFunction ( self , callable ) : <EOL> '''<STR_LIT>''' <EOL> self . _deferredFunctions . append ( ( callable , self . scopeStack [ : ] ) ) <EOL> def deferAssignment ( self , callable ) : <EOL> """<STR_LIT>""" <EOL> self . _deferredAssignments . append ( ( callable , self . scopeStack [ : ] ) ) <EOL> def _runDeferred ( self , deferred ) : <EOL> """<STR_LIT>""" <EOL> for handler , scope in deferred : <EOL> self . scopeStack = scope <EOL> handler ( ) <EOL> def scope ( self ) : <EOL> return self . scopeStack [ - <NUM_LIT:1> ] <EOL> scope = property ( scope ) <EOL> def popScope ( self ) : <EOL> self . dead_scopes . append ( self . scopeStack . pop ( ) ) <EOL> def check_dead_scopes ( self ) : <EOL> """<STR_LIT>""" <EOL> for scope in self . dead_scopes : <EOL> export = isinstance ( scope . get ( '<STR_LIT>' ) , ExportBinding ) <EOL> if export : <EOL> all = scope [ '<STR_LIT>' ] . names ( ) <EOL> if os . path . split ( self . filename ) [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> undefined = set ( all ) - set ( scope ) <EOL> for name in undefined : <EOL> self . report ( <EOL> messages . UndefinedExport , <EOL> scope [ '<STR_LIT>' ] . source , <EOL> name ) <EOL> else : <EOL> all = [ ] <EOL> for importation in scope . itervalues ( ) : <EOL> if isinstance ( importation , Importation ) : <EOL> if not importation . used and importation . name not in all : <EOL> self . report ( <EOL> messages . UnusedImport , <EOL> importation . source , <EOL> importation . name ) <EOL> def pushFunctionScope ( self ) : <EOL> self . scopeStack . append ( FunctionScope ( ) ) <EOL> def pushClassScope ( self ) : <EOL> self . scopeStack . append ( ClassScope ( ) ) <EOL> def report ( self , messageClass , * args , ** kwargs ) : <EOL> self . messages . append ( messageClass ( self . filename , * args , ** kwargs ) ) <EOL> def lowestCommonAncestor ( self , lnode , rnode , stop = None ) : <EOL> if not stop : <EOL> stop = self . root <EOL> if lnode is stop : <EOL> return lnode <EOL> if rnode is stop : <EOL> return rnode <EOL> if lnode is rnode : <EOL> return lnode <EOL> if ( lnode . level > rnode . level ) : <EOL> return self . lowestCommonAncestor ( lnode . parent , rnode , stop ) <EOL> if ( rnode . level > lnode . level ) : <EOL> return self . lowestCommonAncestor ( lnode , rnode . parent , stop ) <EOL> if lnode . parent is rnode . parent : <EOL> return lnode . parent <EOL> else : <EOL> return self . lowestCommonAncestor ( lnode . parent , rnode . parent , stop ) <EOL> def descendantOf ( self , node , ancestors , stop = None ) : <EOL> for a in ancestors : <EOL> try : <EOL> p = self . lowestCommonAncestor ( node , a , stop ) <EOL> if not p is stop : <EOL> return True <EOL> except AttributeError : <EOL> pass <EOL> return False <EOL> def onFork ( self , parent , lnode , rnode , items ) : <EOL> return int ( self . descendantOf ( lnode , items , parent ) ) + int ( self . descendantOf ( rnode , items , parent ) ) <EOL> def differentForks ( self , lnode , rnode ) : <EOL> "<STR_LIT>" <EOL> ancestor = self . lowestCommonAncestor ( lnode , rnode ) <EOL> if isinstance ( ancestor , _ast . If ) : <EOL> for fork in ( ancestor . body , ancestor . orelse ) : <EOL> if self . onFork ( ancestor , lnode , rnode , fork ) == <NUM_LIT:1> : <EOL> return True <EOL> if isinstance ( ancestor , _ast . TryExcept ) : <EOL> for fork in ( ancestor . body , ancestor . handlers , ancestor . orelse ) : <EOL> if self . onFork ( ancestor , lnode , rnode , fork ) == <NUM_LIT:1> : <EOL> return True <EOL> return False <EOL> def handleChildren ( self , tree ) : <EOL> for node in iter_child_nodes ( tree ) : <EOL> self . handleNode ( node , tree ) <EOL> def isDocstring ( self , node ) : <EOL> """<STR_LIT>""" <EOL> return isinstance ( node , _ast . Str ) or ( isinstance ( node , _ast . Expr ) and <EOL> isinstance ( node . value , _ast . Str ) ) <EOL> def handleNode ( self , node , parent ) : <EOL> node . parent = parent <EOL> if self . traceTree : <EOL> print '<STR_LIT:U+0020>' * self . nodeDepth + node . __class__ . __name__ <EOL> self . nodeDepth += <NUM_LIT:1> <EOL> if self . futuresAllowed and not ( isinstance ( node , _ast . ImportFrom ) or self . isDocstring ( node ) ) : <EOL> self . futuresAllowed = False <EOL> nodeType = node . __class__ . __name__ . upper ( ) <EOL> node . level = self . nodeDepth <EOL> try : <EOL> handler = getattr ( self , nodeType ) <EOL> handler ( node ) <EOL> finally : <EOL> self . nodeDepth -= <NUM_LIT:1> <EOL> if self . traceTree : <EOL> print '<STR_LIT:U+0020>' * self . nodeDepth + '<STR_LIT>' + node . __class__ . __name__ <EOL> def ignore ( self , node ) : <EOL> pass <EOL> RETURN = DELETE = PRINT = WHILE = IF = WITH = RAISE = TRYEXCEPT = TRYFINALLY = ASSERT = EXEC = EXPR = handleChildren <EOL> CONTINUE = BREAK = PASS = ignore <EOL> BOOLOP = BINOP = UNARYOP = IFEXP = DICT = SET = YIELD = COMPARE = CALL = REPR = ATTRIBUTE = SUBSCRIPT = LIST = TUPLE = handleChildren <EOL> NUM = STR = ELLIPSIS = ignore <EOL> SLICE = EXTSLICE = INDEX = handleChildren <EOL> LOAD = STORE = DEL = AUGLOAD = AUGSTORE = PARAM = ignore <EOL> AND = OR = ADD = SUB = MULT = DIV = MOD = POW = LSHIFT = RSHIFT = BITOR = BITXOR = BITAND = FLOORDIV = INVERT = NOT = UADD = USUB = EQ = NOTEQ = LT = LTE = GT = GTE = IS = ISNOT = IN = NOTIN = ignore <EOL> COMPREHENSION = EXCEPTHANDLER = KEYWORD = handleChildren <EOL> def addBinding ( self , loc , value , reportRedef = True ) : <EOL> '''<STR_LIT>''' <EOL> if ( isinstance ( self . scope . get ( value . name ) , FunctionDefinition ) <EOL> and isinstance ( value , FunctionDefinition ) ) : <EOL> if not value . _property_decorator : <EOL> if not self . differentForks ( loc , self . scope [ value . name ] . source ) : <EOL> self . report ( messages . RedefinedFunction , <EOL> loc , value . name , self . scope [ value . name ] . source ) <EOL> if not isinstance ( self . scope , ClassScope ) : <EOL> for scope in self . scopeStack [ : : - <NUM_LIT:1> ] : <EOL> existing = scope . get ( value . name ) <EOL> if ( isinstance ( existing , Importation ) <EOL> and not existing . used <EOL> and ( not isinstance ( value , Importation ) or value . fullName == existing . fullName ) <EOL> and reportRedef ) : <EOL> if not self . differentForks ( loc , existing . source ) : <EOL> self . report ( messages . RedefinedWhileUnused , <EOL> loc , value . name , existing . source ) <EOL> if isinstance ( value , UnBinding ) : <EOL> try : <EOL> del self . scope [ value . name ] <EOL> except KeyError : <EOL> self . report ( messages . UndefinedName , loc , value . name ) <EOL> else : <EOL> self . scope [ value . name ] = value <EOL> def GLOBAL ( self , node ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( self . scope , FunctionScope ) : <EOL> self . scope . globals . update ( dict . fromkeys ( node . names ) ) <EOL> def LISTCOMP ( self , node ) : <EOL> for gen in node . generators : <EOL> self . handleNode ( gen , node ) <EOL> self . handleNode ( node . elt , node ) <EOL> GENERATOREXP = SETCOMP = LISTCOMP <EOL> def DICTCOMP ( self , node ) : <EOL> for gen in node . generators : <EOL> self . handleNode ( gen , node ) <EOL> self . handleNode ( node . key , node ) <EOL> self . handleNode ( node . value , node ) <EOL> def FOR ( self , node ) : <EOL> """<STR_LIT>""" <EOL> vars = [ ] <EOL> def collectLoopVars ( n ) : <EOL> if isinstance ( n , _ast . Name ) : <EOL> vars . append ( n . id ) <EOL> elif isinstance ( n , _ast . expr_context ) : <EOL> return <EOL> else : <EOL> for c in iter_child_nodes ( n ) : <EOL> collectLoopVars ( c ) <EOL> collectLoopVars ( node . target ) <EOL> for varn in vars : <EOL> if ( isinstance ( self . scope . get ( varn ) , Importation ) <EOL> and self . scope [ varn ] . used ) : <EOL> self . report ( messages . ImportShadowedByLoopVar , <EOL> node , varn , self . scope [ varn ] . source ) <EOL> self . handleChildren ( node ) <EOL> def NAME ( self , node ) : <EOL> """<STR_LIT>""" <EOL> if node . id == '<STR_LIT>' and isinstance ( node . parent , _ast . Call ) : <EOL> self . scope . usesLocals = True <EOL> if isinstance ( node . ctx , ( _ast . Load , _ast . AugLoad ) ) : <EOL> importStarred = self . scope . importStarred <EOL> try : <EOL> self . scope [ node . id ] . used = ( self . scope , node ) <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> return <EOL> for scope in self . scopeStack [ - <NUM_LIT:2> : <NUM_LIT:0> : - <NUM_LIT:1> ] : <EOL> importStarred = importStarred or scope . importStarred <EOL> if not isinstance ( scope , FunctionScope ) : <EOL> continue <EOL> try : <EOL> scope [ node . id ] . used = ( self . scope , node ) <EOL> except KeyError : <EOL> pass <EOL> else : <EOL> return <EOL> importStarred = importStarred or self . scopeStack [ <NUM_LIT:0> ] . importStarred <EOL> try : <EOL> self . scopeStack [ <NUM_LIT:0> ] [ node . id ] . used = ( self . scope , node ) <EOL> except KeyError : <EOL> if ( ( not hasattr ( __builtin__ , node . id ) ) <EOL> and node . id not in _MAGIC_GLOBALS <EOL> and not importStarred ) : <EOL> if ( os . path . basename ( self . filename ) == '<STR_LIT>' and <EOL> node . id == '<STR_LIT>' ) : <EOL> pass <EOL> else : <EOL> self . report ( messages . UndefinedName , node , node . id ) <EOL> elif isinstance ( node . ctx , ( _ast . Store , _ast . AugStore ) ) : <EOL> if isinstance ( self . scope , FunctionScope ) and node . id not in self . scope : <EOL> for scope in self . scopeStack [ : - <NUM_LIT:1> ] : <EOL> if not isinstance ( scope , ( FunctionScope , ModuleScope ) ) : <EOL> continue <EOL> if ( node . id in scope <EOL> and scope [ node . id ] . used <EOL> and scope [ node . id ] . used [ <NUM_LIT:0> ] is self . scope <EOL> and node . id not in self . scope . globals ) : <EOL> self . report ( messages . UndefinedLocal , <EOL> scope [ node . id ] . used [ <NUM_LIT:1> ] , <EOL> node . id , <EOL> scope [ node . id ] . source ) <EOL> break <EOL> if isinstance ( node . parent , <EOL> ( _ast . For , _ast . comprehension , _ast . Tuple , _ast . List ) ) : <EOL> binding = Binding ( node . id , node ) <EOL> elif ( node . id == '<STR_LIT>' and <EOL> isinstance ( self . scope , ModuleScope ) ) : <EOL> binding = ExportBinding ( node . id , node . parent . value ) <EOL> else : <EOL> binding = Assignment ( node . id , node ) <EOL> if node . id in self . scope : <EOL> binding . used = self . scope [ node . id ] . used <EOL> self . addBinding ( node , binding ) <EOL> elif isinstance ( node . ctx , _ast . Del ) : <EOL> if isinstance ( self . scope , FunctionScope ) and node . id in self . scope . globals : <EOL> del self . scope . globals [ node . id ] <EOL> else : <EOL> self . addBinding ( node , UnBinding ( node . id , node ) ) <EOL> else : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" % ( node . ctx , ) ) <EOL> def FUNCTIONDEF ( self , node ) : <EOL> if hasattr ( node , '<STR_LIT>' ) : <EOL> for deco in node . decorators : <EOL> self . handleNode ( deco , node ) <EOL> else : <EOL> for deco in node . decorator_list : <EOL> self . handleNode ( deco , node ) <EOL> func_def = FunctionDefinition ( node . name , node ) <EOL> if hasattr ( node , '<STR_LIT>' ) : <EOL> for decorator in node . decorators : <EOL> if getattr ( decorator , '<STR_LIT>' , None ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> func_def . _property_decorator = True <EOL> else : <EOL> for decorator in node . decorator_list : <EOL> if getattr ( decorator , '<STR_LIT>' , None ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> func_def . _property_decorator = True <EOL> self . addBinding ( node , func_def ) <EOL> self . LAMBDA ( node ) <EOL> def LAMBDA ( self , node ) : <EOL> for default in node . args . defaults : <EOL> self . handleNode ( default , node ) <EOL> def runFunction ( ) : <EOL> args = [ ] <EOL> def addArgs ( arglist ) : <EOL> for arg in arglist : <EOL> if isinstance ( arg , _ast . Tuple ) : <EOL> addArgs ( arg . elts ) <EOL> else : <EOL> if arg . id in args : <EOL> self . report ( messages . DuplicateArgument , <EOL> node , arg . id ) <EOL> args . append ( arg . id ) <EOL> self . pushFunctionScope ( ) <EOL> addArgs ( node . args . args ) <EOL> if node . args . vararg : <EOL> args . append ( node . args . vararg ) <EOL> if node . args . kwarg : <EOL> args . append ( node . args . kwarg ) <EOL> for name in args : <EOL> self . addBinding ( node , Argument ( name , node ) , reportRedef = False ) <EOL> if isinstance ( node . body , list ) : <EOL> for stmt in node . body : <EOL> self . handleNode ( stmt , node ) <EOL> else : <EOL> self . handleNode ( node . body , node ) <EOL> def checkUnusedAssignments ( ) : <EOL> """<STR_LIT>""" <EOL> for name , binding in self . scope . iteritems ( ) : <EOL> if ( not binding . used and not name in self . scope . globals <EOL> and not self . scope . usesLocals <EOL> and isinstance ( binding , Assignment ) ) : <EOL> self . report ( messages . UnusedVariable , <EOL> binding . source , name ) <EOL> self . deferAssignment ( checkUnusedAssignments ) <EOL> self . popScope ( ) <EOL> self . deferFunction ( runFunction ) <EOL> def CLASSDEF ( self , node ) : <EOL> """<STR_LIT>""" <EOL> for deco in getattr ( node , '<STR_LIT>' , [ ] ) : <EOL> self . handleNode ( deco , node ) <EOL> for baseNode in node . bases : <EOL> self . handleNode ( baseNode , node ) <EOL> self . pushClassScope ( ) <EOL> for stmt in node . body : <EOL> self . handleNode ( stmt , node ) <EOL> self . popScope ( ) <EOL> self . addBinding ( node , Binding ( node . name , node ) ) <EOL> def ASSIGN ( self , node ) : <EOL> self . handleNode ( node . value , node ) <EOL> for target in node . targets : <EOL> self . handleNode ( target , node ) <EOL> def AUGASSIGN ( self , node ) : <EOL> node . target . ctx = _ast . AugLoad ( ) <EOL> self . handleNode ( node . target , node ) <EOL> self . handleNode ( node . value , node ) <EOL> node . target . ctx = _ast . AugStore ( ) <EOL> self . handleNode ( node . target , node ) <EOL> def IMPORT ( self , node ) : <EOL> for alias in node . names : <EOL> name = alias . asname or alias . name <EOL> importation = Importation ( name , node ) <EOL> self . addBinding ( node , importation ) <EOL> def IMPORTFROM ( self , node ) : <EOL> if node . module == '<STR_LIT>' : <EOL> if not self . futuresAllowed : <EOL> self . report ( messages . LateFutureImport , node , <EOL> [ n . name for n in node . names ] ) <EOL> else : <EOL> self . futuresAllowed = False <EOL> for alias in node . names : <EOL> if alias . name == '<STR_LIT:*>' : <EOL> self . scope . importStarred = True <EOL> self . report ( messages . ImportStarUsed , node , node . module ) <EOL> continue <EOL> name = alias . asname or alias . name <EOL> importation = Importation ( name , node ) <EOL> if node . module == '<STR_LIT>' : <EOL> importation . used = ( self . scope , node ) <EOL> self . addBinding ( node , importation ) </s>
