<s> import Nodes <EOL> import ExprNodes <EOL> import PyrexTypes <EOL> import Visitor <EOL> def unwrap_node ( node ) : <EOL> while isinstance ( node , ExprNodes . PersistentNode ) : <EOL> node = node . arg <EOL> return node <EOL> def is_common_value ( a , b ) : <EOL> a = unwrap_node ( a ) <EOL> b = unwrap_node ( b ) <EOL> if isinstance ( a , ExprNodes . NameNode ) and isinstance ( b , ExprNodes . NameNode ) : <EOL> return a . name == b . name <EOL> if isinstance ( a , ExprNodes . AttributeNode ) and isinstance ( b , ExprNodes . AttributeNode ) : <EOL> return not a . is_py_attr and is_common_value ( a . obj , b . obj ) and a . attribute == b . attribute <EOL> return False <EOL> class SwitchTransform ( Visitor . VisitorTransform ) : <EOL> """<STR_LIT>""" <EOL> def extract_conditions ( self , cond ) : <EOL> if isinstance ( cond , ExprNodes . CoerceToTempNode ) : <EOL> cond = cond . arg <EOL> if isinstance ( cond , ExprNodes . TypecastNode ) : <EOL> cond = cond . operand <EOL> if ( isinstance ( cond , ExprNodes . PrimaryCmpNode ) <EOL> and cond . cascade is None <EOL> and cond . operator == '<STR_LIT>' <EOL> and not cond . is_python_comparison ( ) ) : <EOL> if is_common_value ( cond . operand1 , cond . operand1 ) : <EOL> if isinstance ( cond . operand2 , ExprNodes . ConstNode ) : <EOL> return cond . operand1 , [ cond . operand2 ] <EOL> elif hasattr ( cond . operand2 , '<STR_LIT>' ) and cond . operand2 . entry and cond . operand2 . entry . is_const : <EOL> return cond . operand1 , [ cond . operand2 ] <EOL> if is_common_value ( cond . operand2 , cond . operand2 ) : <EOL> if isinstance ( cond . operand1 , ExprNodes . ConstNode ) : <EOL> return cond . operand2 , [ cond . operand1 ] <EOL> elif hasattr ( cond . operand1 , '<STR_LIT>' ) and cond . operand1 . entry and cond . operand1 . entry . is_const : <EOL> return cond . operand2 , [ cond . operand1 ] <EOL> elif ( isinstance ( cond , ExprNodes . BoolBinopNode ) <EOL> and cond . operator == '<STR_LIT>' ) : <EOL> t1 , c1 = self . extract_conditions ( cond . operand1 ) <EOL> t2 , c2 = self . extract_conditions ( cond . operand2 ) <EOL> if is_common_value ( t1 , t2 ) : <EOL> return t1 , c1 + c2 <EOL> return None , None <EOL> def visit_IfStatNode ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> common_var = None <EOL> case_count = <NUM_LIT:0> <EOL> cases = [ ] <EOL> for if_clause in node . if_clauses : <EOL> var , conditions = self . extract_conditions ( if_clause . condition ) <EOL> if var is None : <EOL> return node <EOL> elif common_var is not None and not is_common_value ( var , common_var ) : <EOL> return node <EOL> elif not var . type . is_int or sum ( [ not cond . type . is_int for cond in conditions ] ) : <EOL> return node <EOL> else : <EOL> common_var = var <EOL> case_count += len ( conditions ) <EOL> cases . append ( Nodes . SwitchCaseNode ( pos = if_clause . pos , <EOL> conditions = conditions , <EOL> body = if_clause . body ) ) <EOL> if case_count < <NUM_LIT:2> : <EOL> return node <EOL> common_var = unwrap_node ( common_var ) <EOL> return Nodes . SwitchStatNode ( pos = node . pos , <EOL> test = common_var , <EOL> cases = cases , <EOL> else_clause = node . else_clause ) <EOL> def visit_Node ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> class FlattenInListTransform ( Visitor . VisitorTransform ) : <EOL> """<STR_LIT>""" <EOL> def visit_PrimaryCmpNode ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> if node . cascade is not None : <EOL> return node <EOL> elif node . operator == '<STR_LIT>' : <EOL> conjunction = '<STR_LIT>' <EOL> eq_or_neq = '<STR_LIT>' <EOL> elif node . operator == '<STR_LIT>' : <EOL> conjunction = '<STR_LIT>' <EOL> eq_or_neq = '<STR_LIT>' <EOL> else : <EOL> return node <EOL> if isinstance ( node . operand2 , ExprNodes . TupleNode ) or isinstance ( node . operand2 , ExprNodes . ListNode ) : <EOL> args = node . operand2 . args <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> return ExprNodes . BoolNode ( pos = node . pos , value = node . operator == '<STR_LIT>' ) <EOL> else : <EOL> lhs = ExprNodes . PersistentNode ( node . operand1 , len ( args ) ) <EOL> conds = [ ] <EOL> for arg in args : <EOL> cond = ExprNodes . PrimaryCmpNode ( <EOL> pos = node . pos , <EOL> operand1 = lhs , <EOL> operator = eq_or_neq , <EOL> operand2 = arg , <EOL> cascade = None ) <EOL> conds . append ( ExprNodes . TypecastNode ( <EOL> pos = node . pos , <EOL> operand = cond , <EOL> type = PyrexTypes . c_bint_type ) ) <EOL> def concat ( left , right ) : <EOL> return ExprNodes . BoolBinopNode ( <EOL> pos = node . pos , <EOL> operator = conjunction , <EOL> operand1 = left , <EOL> operand2 = right ) <EOL> return reduce ( concat , conds ) <EOL> else : <EOL> return node <EOL> def visit_Node ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> return node <EOL> class FinalOptimizePhase ( Visitor . CythonTransform ) : <EOL> """<STR_LIT>""" <EOL> def visit_SingleAssignmentNode ( self , node ) : <EOL> if node . first : <EOL> lhs = node . lhs <EOL> lhs . lhs_of_first_assignment = True <EOL> if isinstance ( lhs , ExprNodes . NameNode ) and lhs . entry . type . is_pyobject : <EOL> lhs . entry . init_to_none = False <EOL> lhs . entry . init = <NUM_LIT:0> <EOL> return node <EOL> def visit_SimpleCallNode ( self , node ) : <EOL> self . visitchildren ( node ) <EOL> if node . function . type . is_cfunction and isinstance ( node . function , ExprNodes . NameNode ) : <EOL> if node . function . name == '<STR_LIT>' : <EOL> type_arg = node . args [ <NUM_LIT:1> ] <EOL> if type_arg . type . is_builtin_type and type_arg . type . name == '<STR_LIT:type>' : <EOL> object_module = self . context . find_module ( '<STR_LIT>' ) <EOL> node . function . entry = object_module . lookup ( '<STR_LIT>' ) <EOL> if node . function . entry is None : <EOL> return node <EOL> node . function . type = node . function . entry . type <EOL> PyTypeObjectPtr = PyrexTypes . CPtrType ( object_module . lookup ( '<STR_LIT>' ) . type ) <EOL> node . args [ <NUM_LIT:1> ] = ExprNodes . CastNode ( node . args [ <NUM_LIT:1> ] , PyTypeObjectPtr ) <EOL> return node </s>
