<s> from __future__ import absolute_import , print_function <EOL> import sys <EOL> import inspect <EOL> from . import TypeSlots <EOL> from . import Builtin <EOL> from . import Nodes <EOL> from . import ExprNodes <EOL> from . import Errors <EOL> from . import DebugFlags <EOL> from . import Future <EOL> import cython <EOL> cython . declare ( _PRINTABLE = tuple ) <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> _PRINTABLE = ( bytes , str , int , float ) <EOL> else : <EOL> _PRINTABLE = ( str , unicode , long , int , float ) <EOL> class TreeVisitor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( TreeVisitor , self ) . __init__ ( ) <EOL> self . dispatch_table = { } <EOL> self . access_path = [ ] <EOL> def dump_node ( self , node , indent = <NUM_LIT:0> ) : <EOL> ignored = list ( node . child_attrs or [ ] ) + [ <EOL> u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' , u'<STR_LIT>' ] <EOL> values = [ ] <EOL> pos = getattr ( node , '<STR_LIT>' , None ) <EOL> if pos : <EOL> source = pos [ <NUM_LIT:0> ] <EOL> if source : <EOL> import os . path <EOL> source = os . path . basename ( source . get_description ( ) ) <EOL> values . append ( u'<STR_LIT>' % ( source , pos [ <NUM_LIT:1> ] , pos [ <NUM_LIT:2> ] ) ) <EOL> attribute_names = dir ( node ) <EOL> attribute_names . sort ( ) <EOL> for attr in attribute_names : <EOL> if attr in ignored : <EOL> continue <EOL> if attr . startswith ( '<STR_LIT:_>' ) or attr . endswith ( '<STR_LIT:_>' ) : <EOL> continue <EOL> try : <EOL> value = getattr ( node , attr ) <EOL> except AttributeError : <EOL> continue <EOL> if value is None or value == <NUM_LIT:0> : <EOL> continue <EOL> elif isinstance ( value , list ) : <EOL> value = u'<STR_LIT>' % len ( value ) <EOL> elif not isinstance ( value , _PRINTABLE ) : <EOL> continue <EOL> else : <EOL> value = repr ( value ) <EOL> values . append ( u'<STR_LIT>' % ( attr , value ) ) <EOL> return u'<STR_LIT>' % ( node . __class__ . __name__ , u'<STR_LIT>' . join ( values ) ) <EOL> def _find_node_path ( self , stacktrace ) : <EOL> import os . path <EOL> last_traceback = stacktrace <EOL> nodes = [ ] <EOL> while hasattr ( stacktrace , '<STR_LIT>' ) : <EOL> frame = stacktrace . tb_frame <EOL> node = frame . f_locals . get ( u'<STR_LIT>' ) <EOL> if isinstance ( node , Nodes . Node ) : <EOL> code = frame . f_code <EOL> method_name = code . co_name <EOL> pos = ( os . path . basename ( code . co_filename ) , <EOL> frame . f_lineno ) <EOL> nodes . append ( ( node , method_name , pos ) ) <EOL> last_traceback = stacktrace <EOL> stacktrace = stacktrace . tb_next <EOL> return ( last_traceback , nodes ) <EOL> def _raise_compiler_error ( self , child , e ) : <EOL> trace = [ '<STR_LIT>' ] <EOL> for parent , attribute , index in self . access_path : <EOL> node = getattr ( parent , attribute ) <EOL> if index is None : <EOL> index = '<STR_LIT>' <EOL> else : <EOL> node = node [ index ] <EOL> index = u'<STR_LIT>' % index <EOL> trace . append ( u'<STR_LIT>' % ( <EOL> parent . __class__ . __name__ , attribute , index , <EOL> self . dump_node ( node ) ) ) <EOL> stacktrace , called_nodes = self . _find_node_path ( sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> last_node = child <EOL> for node , method_name , pos in called_nodes : <EOL> last_node = node <EOL> trace . append ( u"<STR_LIT>" % ( <EOL> pos [ <NUM_LIT:0> ] , pos [ <NUM_LIT:1> ] , method_name , self . dump_node ( node ) ) ) <EOL> raise Errors . CompilerCrash ( <EOL> getattr ( last_node , '<STR_LIT>' , None ) , self . __class__ . __name__ , <EOL> u'<STR_LIT:\n>' . join ( trace ) , e , stacktrace ) <EOL> @ cython . final <EOL> def find_handler ( self , obj ) : <EOL> cls = type ( obj ) <EOL> pattern = "<STR_LIT>" <EOL> mro = inspect . getmro ( cls ) <EOL> handler_method = None <EOL> for mro_cls in mro : <EOL> handler_method = getattr ( self , pattern % mro_cls . __name__ , None ) <EOL> if handler_method is not None : <EOL> return handler_method <EOL> print ( type ( self ) , cls ) <EOL> if self . access_path : <EOL> print ( self . access_path ) <EOL> print ( self . access_path [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] . pos ) <EOL> print ( self . access_path [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] . __dict__ ) <EOL> raise RuntimeError ( "<STR_LIT>" % ( self , obj ) ) <EOL> def visit ( self , obj ) : <EOL> return self . _visit ( obj ) <EOL> @ cython . final <EOL> def _visit ( self , obj ) : <EOL> try : <EOL> try : <EOL> handler_method = self . dispatch_table [ type ( obj ) ] <EOL> except KeyError : <EOL> handler_method = self . find_handler ( obj ) <EOL> self . dispatch_table [ type ( obj ) ] = handler_method <EOL> return handler_method ( obj ) <EOL> except Errors . CompileError : <EOL> raise <EOL> except Errors . AbortError : <EOL> raise <EOL> except Exception as e : <EOL> if DebugFlags . debug_no_exception_intercept : <EOL> raise <EOL> self . _raise_compiler_error ( obj , e ) <EOL> @ cython . final <EOL> def _visitchild ( self , child , parent , attrname , idx ) : <EOL> self . access_path . append ( ( parent , attrname , idx ) ) <EOL> result = self . _visit ( child ) <EOL> self . access_path . pop ( ) <EOL> return result <EOL> def visitchildren ( self , parent , attrs = None ) : <EOL> return self . _visitchildren ( parent , attrs ) <EOL> @ cython . final <EOL> @ cython . locals ( idx = int ) <EOL> def _visitchildren ( self , parent , attrs ) : <EOL> """<STR_LIT>""" <EOL> if parent is None : return None <EOL> result = { } <EOL> for attr in parent . child_attrs : <EOL> if attrs is not None and attr not in attrs : continue <EOL> child = getattr ( parent , attr ) <EOL> if child is not None : <EOL> if type ( child ) is list : <EOL> childretval = [ self . _visitchild ( x , parent , attr , idx ) for idx , x in enumerate ( child ) ] <EOL> else : <EOL> childretval = self . _visitchild ( child , parent , attr , None ) <EOL> assert not isinstance ( childretval , list ) , '<STR_LIT>' % ( attr , parent ) <EOL> result [ attr ] = childretval <EOL> return result <EOL> class VisitorTransform ( TreeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def visitchildren ( self , parent , attrs = None ) : <EOL> result = self . _visitchildren ( parent , attrs ) <EOL> for attr , newnode in result . items ( ) : <EOL> if type ( newnode ) is not list : <EOL> setattr ( parent , attr , newnode ) <EOL> else : <EOL> newlist = [ ] <EOL> for x in newnode : <EOL> if x is not None : <EOL> if type ( x ) is list : <EOL> newlist += x <EOL> else : <EOL> newlist . append ( x ) <EOL> setattr ( parent , attr , newlist ) <EOL> return result <EOL> def recurse_to_children ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> def __call__ ( self , root ) : <EOL> return self . _visit ( root ) <EOL> class CythonTransform ( VisitorTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context ) : <EOL> super ( CythonTransform , self ) . __init__ ( ) <EOL> self . context = context <EOL> def __call__ ( self , node ) : <EOL> from . import ModuleNode <EOL> if isinstance ( node , ModuleNode . ModuleNode ) : <EOL> self . current_directives = node . directives <EOL> return super ( CythonTransform , self ) . __call__ ( node ) <EOL> def visit_CompilerDirectivesNode ( self , node ) : <EOL> old = self . current_directives <EOL> self . current_directives = node . directives <EOL> self . visitchildren ( node ) <EOL> self . current_directives = old <EOL> return node <EOL> def visit_Node ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> class ScopeTrackingTransform ( CythonTransform ) : <EOL> def visit_ModuleNode ( self , node ) : <EOL> self . scope_type = '<STR_LIT>' <EOL> self . scope_node = node <EOL> self . visitchildren ( node ) <EOL> return node <EOL> def visit_scope ( self , node , scope_type ) : <EOL> prev = self . scope_type , self . scope_node <EOL> self . scope_type = scope_type <EOL> self . scope_node = node <EOL> self . visitchildren ( node ) <EOL> self . scope_type , self . scope_node = prev <EOL> return node <EOL> def visit_CClassDefNode ( self , node ) : <EOL> return self . visit_scope ( node , '<STR_LIT>' ) <EOL> def visit_PyClassDefNode ( self , node ) : <EOL> return self . visit_scope ( node , '<STR_LIT>' ) <EOL> def visit_FuncDefNode ( self , node ) : <EOL> return self . visit_scope ( node , '<STR_LIT>' ) <EOL> def visit_CStructOrUnionDefNode ( self , node ) : <EOL> return self . visit_scope ( node , '<STR_LIT>' ) <EOL> class EnvTransform ( CythonTransform ) : <EOL> """<STR_LIT>""" <EOL> def __call__ ( self , root ) : <EOL> self . env_stack = [ ] <EOL> self . enter_scope ( root , root . scope ) <EOL> return super ( EnvTransform , self ) . __call__ ( root ) <EOL> def current_env ( self ) : <EOL> return self . env_stack [ - <NUM_LIT:1> ] [ <NUM_LIT:1> ] <EOL> def current_scope_node ( self ) : <EOL> return self . env_stack [ - <NUM_LIT:1> ] [ <NUM_LIT:0> ] <EOL> def global_scope ( self ) : <EOL> return self . current_env ( ) . global_scope ( ) <EOL> def enter_scope ( self , node , scope ) : <EOL> self . env_stack . append ( ( node , scope ) ) <EOL> def exit_scope ( self ) : <EOL> self . env_stack . pop ( ) <EOL> def visit_FuncDefNode ( self , node ) : <EOL> self . enter_scope ( node , node . local_scope ) <EOL> self . visitchildren ( node ) <EOL> self . exit_scope ( ) <EOL> return node <EOL> def visit_GeneratorBodyDefNode ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> def visit_ClassDefNode ( self , node ) : <EOL> self . enter_scope ( node , node . scope ) <EOL> self . visitchildren ( node ) <EOL> self . exit_scope ( ) <EOL> return node <EOL> def visit_CStructOrUnionDefNode ( self , node ) : <EOL> self . enter_scope ( node , node . scope ) <EOL> self . visitchildren ( node ) <EOL> self . exit_scope ( ) <EOL> return node <EOL> def visit_ScopedExprNode ( self , node ) : <EOL> if node . expr_scope : <EOL> self . enter_scope ( node , node . expr_scope ) <EOL> self . visitchildren ( node ) <EOL> self . exit_scope ( ) <EOL> else : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> def visit_CArgDeclNode ( self , node ) : <EOL> if node . default : <EOL> attrs = [ attr for attr in node . child_attrs if attr != '<STR_LIT:default>' ] <EOL> self . visitchildren ( node , attrs ) <EOL> self . enter_scope ( node , self . current_env ( ) . outer_scope ) <EOL> self . visitchildren ( node , ( '<STR_LIT:default>' , ) ) <EOL> self . exit_scope ( ) <EOL> else : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> class NodeRefCleanupMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> super ( NodeRefCleanupMixin , self ) . __init__ ( * args ) <EOL> self . _replacements = { } <EOL> def visit_CloneNode ( self , node ) : <EOL> arg = node . arg <EOL> if arg not in self . _replacements : <EOL> self . visitchildren ( arg ) <EOL> node . arg = self . _replacements . get ( arg , arg ) <EOL> return node <EOL> def visit_ResultRefNode ( self , node ) : <EOL> expr = node . expression <EOL> if expr is None or expr not in self . _replacements : <EOL> self . visitchildren ( node ) <EOL> expr = node . expression <EOL> if expr is not None : <EOL> node . expression = self . _replacements . get ( expr , expr ) <EOL> return node <EOL> def replace ( self , node , replacement ) : <EOL> self . _replacements [ node ] = replacement <EOL> return replacement <EOL> find_special_method_for_binary_operator = { <EOL> '<STR_LIT:<>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:>>' : '<STR_LIT>' , <EOL> '<STR_LIT:+>' : '<STR_LIT>' , <EOL> '<STR_LIT:&>' : '<STR_LIT>' , <EOL> '<STR_LIT:/>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:%>' : '<STR_LIT>' , <EOL> '<STR_LIT:*>' : '<STR_LIT>' , <EOL> '<STR_LIT:|>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:->' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } . get <EOL> find_special_method_for_unary_operator = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:->' : '<STR_LIT>' , <EOL> '<STR_LIT:+>' : '<STR_LIT>' , <EOL> } . get <EOL> class MethodDispatcherTransform ( EnvTransform ) : <EOL> """<STR_LIT>""" <EOL> def visit_GeneralCallNode ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> function = node . function <EOL> if not function . type . is_pyobject : <EOL> return node <EOL> arg_tuple = node . positional_args <EOL> if not isinstance ( arg_tuple , ExprNodes . TupleNode ) : <EOL> return node <EOL> keyword_args = node . keyword_args <EOL> if keyword_args and not isinstance ( keyword_args , ExprNodes . DictNode ) : <EOL> return node <EOL> args = arg_tuple . args <EOL> return self . _dispatch_to_handler ( node , function , args , keyword_args ) <EOL> def visit_SimpleCallNode ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> function = node . function <EOL> if function . type . is_pyobject : <EOL> arg_tuple = node . arg_tuple <EOL> if not isinstance ( arg_tuple , ExprNodes . TupleNode ) : <EOL> return node <EOL> args = arg_tuple . args <EOL> else : <EOL> args = node . args <EOL> return self . _dispatch_to_handler ( node , function , args , None ) <EOL> def visit_PrimaryCmpNode ( self , node ) : <EOL> if node . cascade : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> return self . _visit_binop_node ( node ) <EOL> def visit_BinopNode ( self , node ) : <EOL> return self . _visit_binop_node ( node ) <EOL> def _visit_binop_node ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> special_method_name = find_special_method_for_binary_operator ( node . operator ) <EOL> if special_method_name : <EOL> operand1 , operand2 = node . operand1 , node . operand2 <EOL> if special_method_name == '<STR_LIT>' : <EOL> operand1 , operand2 = operand2 , operand1 <EOL> elif special_method_name == '<STR_LIT>' : <EOL> if Future . division in self . current_env ( ) . global_scope ( ) . context . future_directives : <EOL> special_method_name = '<STR_LIT>' <EOL> obj_type = operand1 . type <EOL> if obj_type . is_builtin_type : <EOL> type_name = obj_type . name <EOL> else : <EOL> type_name = "<STR_LIT:object>" <EOL> node = self . _dispatch_to_method_handler ( <EOL> special_method_name , None , False , type_name , <EOL> node , None , [ operand1 , operand2 ] , None ) <EOL> return node <EOL> def visit_UnopNode ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> special_method_name = find_special_method_for_unary_operator ( node . operator ) <EOL> if special_method_name : <EOL> operand = node . operand <EOL> obj_type = operand . type <EOL> if obj_type . is_builtin_type : <EOL> type_name = obj_type . name <EOL> else : <EOL> type_name = "<STR_LIT:object>" <EOL> node = self . _dispatch_to_method_handler ( <EOL> special_method_name , None , False , type_name , <EOL> node , None , [ operand ] , None ) <EOL> return node <EOL> def _find_handler ( self , match_name , has_kwargs ) : <EOL> call_type = has_kwargs and '<STR_LIT>' or '<STR_LIT>' <EOL> handler = getattr ( self , '<STR_LIT>' % ( call_type , match_name ) , None ) <EOL> if handler is None : <EOL> handler = getattr ( self , '<STR_LIT>' % match_name , None ) <EOL> return handler <EOL> def _delegate_to_assigned_value ( self , node , function , arg_list , kwargs ) : <EOL> assignment = function . cf_state [ <NUM_LIT:0> ] <EOL> value = assignment . rhs <EOL> if value . is_name : <EOL> if not value . entry or len ( value . entry . cf_assignments ) > <NUM_LIT:1> : <EOL> return node <EOL> elif value . is_attribute and value . obj . is_name : <EOL> if not value . obj . entry or len ( value . obj . entry . cf_assignments ) > <NUM_LIT:1> : <EOL> return node <EOL> else : <EOL> return node <EOL> return self . _dispatch_to_handler ( <EOL> node , value , arg_list , kwargs ) <EOL> def _dispatch_to_handler ( self , node , function , arg_list , kwargs ) : <EOL> if function . is_name : <EOL> if not function . entry : <EOL> return node <EOL> is_builtin = ( <EOL> function . entry . is_builtin or <EOL> function . entry is self . current_env ( ) . builtin_scope ( ) . lookup_here ( function . name ) ) <EOL> if not is_builtin : <EOL> if function . cf_state and function . cf_state . is_single : <EOL> return self . _delegate_to_assigned_value ( <EOL> node , function , arg_list , kwargs ) <EOL> return node <EOL> function_handler = self . _find_handler ( <EOL> "<STR_LIT>" % function . name , kwargs ) <EOL> if function_handler is None : <EOL> return self . _handle_function ( node , function . name , function , arg_list , kwargs ) <EOL> if kwargs : <EOL> return function_handler ( node , function , arg_list , kwargs ) <EOL> else : <EOL> return function_handler ( node , function , arg_list ) <EOL> elif function . is_attribute : <EOL> attr_name = function . attribute <EOL> if function . type . is_pyobject : <EOL> self_arg = function . obj <EOL> elif node . self and function . entry : <EOL> entry = function . entry . as_variable <EOL> if not entry or not entry . is_builtin : <EOL> return node <EOL> self_arg = node . self <EOL> arg_list = arg_list [ <NUM_LIT:1> : ] <EOL> else : <EOL> return node <EOL> obj_type = self_arg . type <EOL> is_unbound_method = False <EOL> if obj_type . is_builtin_type : <EOL> if ( obj_type is Builtin . type_type and self_arg . is_name and <EOL> arg_list and arg_list [ <NUM_LIT:0> ] . type . is_pyobject ) : <EOL> type_name = self_arg . name <EOL> self_arg = None <EOL> is_unbound_method = True <EOL> else : <EOL> type_name = obj_type . name <EOL> else : <EOL> type_name = "<STR_LIT:object>" <EOL> return self . _dispatch_to_method_handler ( <EOL> attr_name , self_arg , is_unbound_method , type_name , <EOL> node , function , arg_list , kwargs ) <EOL> else : <EOL> return node <EOL> def _dispatch_to_method_handler ( self , attr_name , self_arg , <EOL> is_unbound_method , type_name , <EOL> node , function , arg_list , kwargs ) : <EOL> method_handler = self . _find_handler ( <EOL> "<STR_LIT>" % ( type_name , attr_name ) , kwargs ) <EOL> if method_handler is None : <EOL> if ( attr_name in TypeSlots . method_name_to_slot <EOL> or attr_name == '<STR_LIT>' ) : <EOL> method_handler = self . _find_handler ( <EOL> "<STR_LIT>" % attr_name , kwargs ) <EOL> if method_handler is None : <EOL> return self . _handle_method ( <EOL> node , type_name , attr_name , function , <EOL> arg_list , is_unbound_method , kwargs ) <EOL> if self_arg is not None : <EOL> arg_list = [ self_arg ] + list ( arg_list ) <EOL> if kwargs : <EOL> result = method_handler ( <EOL> node , function , arg_list , is_unbound_method , kwargs ) <EOL> else : <EOL> result = method_handler ( <EOL> node , function , arg_list , is_unbound_method ) <EOL> return result <EOL> def _handle_function ( self , node , function_name , function , arg_list , kwargs ) : <EOL> """<STR_LIT>""" <EOL> return node <EOL> def _handle_method ( self , node , type_name , attr_name , function , <EOL> arg_list , is_unbound_method , kwargs ) : <EOL> """<STR_LIT>""" <EOL> return node <EOL> class RecursiveNodeReplacer ( VisitorTransform ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , orig_node , new_node ) : <EOL> super ( RecursiveNodeReplacer , self ) . __init__ ( ) <EOL> self . orig_node , self . new_node = orig_node , new_node <EOL> def visit_CloneNode ( self , node ) : <EOL> if node is self . orig_node : <EOL> return self . new_node <EOL> if node . arg is self . orig_node : <EOL> node . arg = self . new_node <EOL> return node <EOL> def visit_Node ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> if node is self . orig_node : <EOL> return self . new_node <EOL> else : <EOL> return node <EOL> def recursively_replace_node ( tree , old_node , new_node ) : <EOL> replace_in = RecursiveNodeReplacer ( old_node , new_node ) <EOL> replace_in ( tree ) <EOL> class NodeFinder ( TreeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , node ) : <EOL> super ( NodeFinder , self ) . __init__ ( ) <EOL> self . node = node <EOL> self . found = False <EOL> def visit_Node ( self , node ) : <EOL> if self . found : <EOL> pass <EOL> elif node is self . node : <EOL> self . found = True <EOL> else : <EOL> self . _visitchildren ( node , None ) <EOL> def tree_contains ( tree , node ) : <EOL> finder = NodeFinder ( node ) <EOL> finder . visit ( tree ) <EOL> return finder . found <EOL> def replace_node ( ptr , value ) : <EOL> """<STR_LIT>""" <EOL> parent , attrname , listidx = ptr <EOL> if listidx is None : <EOL> setattr ( parent , attrname , value ) <EOL> else : <EOL> getattr ( parent , attrname ) [ listidx ] = value <EOL> class PrintTree ( TreeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , start = None , end = None ) : <EOL> TreeVisitor . __init__ ( self ) <EOL> self . _indent = "<STR_LIT>" <EOL> if start is not None or end is not None : <EOL> self . _line_range = ( start or <NUM_LIT:0> , end or <NUM_LIT:2> ** <NUM_LIT:30> ) <EOL> else : <EOL> self . _line_range = None <EOL> def indent ( self ) : <EOL> self . _indent += "<STR_LIT:U+0020>" <EOL> def unindent ( self ) : <EOL> self . _indent = self . _indent [ : - <NUM_LIT:2> ] <EOL> def __call__ ( self , tree , phase = None ) : <EOL> print ( "<STR_LIT>" % phase ) <EOL> self . visit ( tree ) <EOL> return tree <EOL> def visit_Node ( self , node ) : <EOL> self . _print_node ( node ) <EOL> self . indent ( ) <EOL> self . visitchildren ( node ) <EOL> self . unindent ( ) <EOL> return node <EOL> def visit_CloneNode ( self , node ) : <EOL> self . _print_node ( node ) <EOL> self . indent ( ) <EOL> line = node . pos [ <NUM_LIT:1> ] <EOL> if self . _line_range is None or self . _line_range [ <NUM_LIT:0> ] <= line <= self . _line_range [ <NUM_LIT:1> ] : <EOL> print ( "<STR_LIT>" % ( self . _indent , '<STR_LIT>' , self . repr_of ( node . arg ) ) ) <EOL> self . indent ( ) <EOL> self . visitchildren ( node . arg ) <EOL> self . unindent ( ) <EOL> self . unindent ( ) <EOL> return node <EOL> def _print_node ( self , node ) : <EOL> line = node . pos [ <NUM_LIT:1> ] <EOL> if self . _line_range is None or self . _line_range [ <NUM_LIT:0> ] <= line <= self . _line_range [ <NUM_LIT:1> ] : <EOL> if len ( self . access_path ) == <NUM_LIT:0> : <EOL> name = "<STR_LIT>" <EOL> else : <EOL> parent , attr , idx = self . access_path [ - <NUM_LIT:1> ] <EOL> if idx is not None : <EOL> name = "<STR_LIT>" % ( attr , idx ) <EOL> else : <EOL> name = attr <EOL> print ( "<STR_LIT>" % ( self . _indent , name , self . repr_of ( node ) ) ) <EOL> def repr_of ( self , node ) : <EOL> if node is None : <EOL> return "<STR_LIT>" <EOL> else : <EOL> result = node . __class__ . __name__ <EOL> if isinstance ( node , ExprNodes . NameNode ) : <EOL> result += "<STR_LIT>" % ( repr ( node . type ) , node . name ) <EOL> elif isinstance ( node , Nodes . DefNode ) : <EOL> result += "<STR_LIT>" % node . name <EOL> elif isinstance ( node , ExprNodes . ExprNode ) : <EOL> t = node . type <EOL> result += "<STR_LIT>" % repr ( t ) <EOL> elif node . pos : <EOL> pos = node . pos <EOL> path = pos [ <NUM_LIT:0> ] . get_description ( ) <EOL> if '<STR_LIT:/>' in path : <EOL> path = path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if '<STR_LIT:\\>' in path : <EOL> path = path . split ( '<STR_LIT:\\>' ) [ - <NUM_LIT:1> ] <EOL> result += "<STR_LIT>" % ( path , pos [ <NUM_LIT:1> ] , pos [ <NUM_LIT:2> ] ) <EOL> return result <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import doctest <EOL> doctest . testmod ( ) </s>
