<s> import operator <EOL> from pypy . interpreter . error import OperationError <EOL> from pypy . interpreter . baseobjspace import ObjSpace <EOL> from pypy . interpreter . function import Function , Method <EOL> from pypy . interpreter . argument import Arguments <EOL> from pypy . interpreter . typedef import default_identity_hash <EOL> from pypy . tool . sourcetools import compile2 , func_with_new_name <EOL> def object_getattribute ( space ) : <EOL> "<STR_LIT>" <EOL> w_src , w_getattribute = space . lookup_in_type_where ( space . w_object , <EOL> '<STR_LIT>' ) <EOL> return w_getattribute <EOL> object_getattribute . _annspecialcase_ = '<STR_LIT>' <EOL> def object_setattr ( space ) : <EOL> "<STR_LIT>" <EOL> w_src , w_setattr = space . lookup_in_type_where ( space . w_object , <EOL> '<STR_LIT>' ) <EOL> return w_setattr <EOL> object_setattr . _annspecialcase_ = '<STR_LIT>' <EOL> def raiseattrerror ( space , w_obj , name , w_descr = None ) : <EOL> w_type = space . type ( w_obj ) <EOL> typename = w_type . getname ( space , '<STR_LIT:?>' ) <EOL> if w_descr is None : <EOL> msg = "<STR_LIT>" % ( typename , name ) <EOL> else : <EOL> msg = "<STR_LIT>" % ( typename , name ) <EOL> raise OperationError ( space . w_AttributeError , space . wrap ( msg ) ) <EOL> class Object : <EOL> def descr__getattribute__ ( space , w_obj , w_name ) : <EOL> name = space . str_w ( w_name ) <EOL> w_descr = space . lookup ( w_obj , name ) <EOL> if w_descr is not None : <EOL> if space . is_data_descr ( w_descr ) : <EOL> return space . get ( w_descr , w_obj ) <EOL> w_value = w_obj . getdictvalue_attr_is_in_class ( space , w_name ) <EOL> else : <EOL> w_value = w_obj . getdictvalue ( space , w_name ) <EOL> if w_value is not None : <EOL> return w_value <EOL> if w_descr is not None : <EOL> return space . get ( w_descr , w_obj ) <EOL> raiseattrerror ( space , w_obj , name ) <EOL> def descr__setattr__ ( space , w_obj , w_name , w_value ) : <EOL> name = space . str_w ( w_name ) <EOL> w_descr = space . lookup ( w_obj , name ) <EOL> shadows_type = False <EOL> if w_descr is not None : <EOL> if space . is_data_descr ( w_descr ) : <EOL> space . set ( w_descr , w_obj , w_value ) <EOL> return <EOL> shadows_type = True <EOL> if w_obj . setdictvalue ( space , w_name , w_value , shadows_type ) : <EOL> return <EOL> raiseattrerror ( space , w_obj , name , w_descr ) <EOL> def descr__delattr__ ( space , w_obj , w_name ) : <EOL> name = space . str_w ( w_name ) <EOL> w_descr = space . lookup ( w_obj , name ) <EOL> if w_descr is not None : <EOL> if space . is_data_descr ( w_descr ) : <EOL> space . delete ( w_descr , w_obj ) <EOL> return <EOL> if w_obj . deldictvalue ( space , w_name ) : <EOL> return <EOL> raiseattrerror ( space , w_obj , name , w_descr ) <EOL> def descr__init__ ( space , w_obj , __args__ ) : <EOL> pass <EOL> class DescrOperation : <EOL> _mixin_ = True <EOL> def is_data_descr ( space , w_obj ) : <EOL> return space . lookup ( w_obj , '<STR_LIT>' ) is not None <EOL> def get_and_call_args ( space , w_descr , w_obj , args ) : <EOL> descr = space . interpclass_w ( w_descr ) <EOL> if type ( descr ) is Function : <EOL> return descr . call_obj_args ( w_obj , args ) <EOL> else : <EOL> w_impl = space . get ( w_descr , w_obj ) <EOL> return space . call_args ( w_impl , args ) <EOL> def get_and_call_function ( space , w_descr , w_obj , * args_w ) : <EOL> descr = space . interpclass_w ( w_descr ) <EOL> if type ( descr ) is Function : <EOL> return descr . funccall ( w_obj , * args_w ) <EOL> else : <EOL> args = Arguments ( space , list ( args_w ) ) <EOL> w_impl = space . get ( w_descr , w_obj ) <EOL> return space . call_args ( w_impl , args ) <EOL> def call_args ( space , w_obj , args ) : <EOL> if isinstance ( w_obj , Function ) : <EOL> return w_obj . call_args ( args ) <EOL> if isinstance ( w_obj , Method ) : <EOL> return w_obj . call_args ( args ) <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( <EOL> space . w_TypeError , <EOL> space . mod ( space . wrap ( '<STR_LIT>' ) , <EOL> space . newtuple ( [ w_obj ] ) ) ) <EOL> return space . get_and_call_args ( w_descr , w_obj , args ) <EOL> def get ( space , w_descr , w_obj , w_type = None ) : <EOL> w_get = space . lookup ( w_descr , '<STR_LIT>' ) <EOL> if w_get is None : <EOL> return w_descr <EOL> if w_type is None : <EOL> w_type = space . type ( w_obj ) <EOL> return space . get_and_call_function ( w_get , w_descr , w_obj , w_type ) <EOL> def set ( space , w_descr , w_obj , w_val ) : <EOL> w_set = space . lookup ( w_descr , '<STR_LIT>' ) <EOL> if w_set is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_set , w_descr , w_obj , w_val ) <EOL> def delete ( space , w_descr , w_obj ) : <EOL> w_delete = space . lookup ( w_descr , '<STR_LIT>' ) <EOL> if w_delete is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_delete , w_descr , w_obj ) <EOL> def getattr ( space , w_obj , w_name ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> return space . _handle_getattribute ( w_descr , w_obj , w_name ) <EOL> def _handle_getattribute ( space , w_descr , w_obj , w_name ) : <EOL> try : <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_AttributeError , space . w_None ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_name ) <EOL> except OperationError , e : <EOL> if not e . match ( space , space . w_AttributeError ) : <EOL> raise <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise <EOL> return space . get_and_call_function ( w_descr , w_obj , w_name ) <EOL> def setattr ( space , w_obj , w_name , w_val ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_AttributeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_name , w_val ) <EOL> def delattr ( space , w_obj , w_name ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_AttributeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_name ) <EOL> def is_true ( space , w_obj ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> return True <EOL> w_res = space . get_and_call_function ( w_descr , w_obj ) <EOL> if w_res is space . w_False : <EOL> return False <EOL> if w_res is space . w_True : <EOL> return True <EOL> w_restype = space . type ( w_res ) <EOL> if ( space . is_w ( w_restype , space . w_bool ) or <EOL> space . is_w ( w_restype , space . w_int ) ) : <EOL> return space . int_w ( w_res ) != <NUM_LIT:0> <EOL> else : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> def nonzero ( self , w_obj ) : <EOL> if self . is_true ( w_obj ) : <EOL> return self . w_True <EOL> else : <EOL> return self . w_False <EOL> def iter ( space , w_obj ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . newseqiter ( w_obj ) <EOL> return space . get_and_call_function ( w_descr , w_obj ) <EOL> def next ( space , w_obj ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_descr , w_obj ) <EOL> def getitem ( space , w_obj , w_key ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_key ) <EOL> def setitem ( space , w_obj , w_key , w_val ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_key , w_val ) <EOL> def delitem ( space , w_obj , w_key ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_key ) <EOL> def getslice ( space , w_obj , w_start , w_stop ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> w_slice = space . newslice ( w_start , w_stop , space . w_None ) <EOL> return space . getitem ( w_obj , w_slice ) <EOL> w_start , w_stop = old_slice_range ( space , w_obj , w_start , w_stop ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_start , w_stop ) <EOL> def setslice ( space , w_obj , w_start , w_stop , w_sequence ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> w_slice = space . newslice ( w_start , w_stop , space . w_None ) <EOL> return space . setitem ( w_obj , w_slice , w_sequence ) <EOL> w_start , w_stop = old_slice_range ( space , w_obj , w_start , w_stop ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_start , w_stop , w_sequence ) <EOL> def delslice ( space , w_obj , w_start , w_stop ) : <EOL> w_descr = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_descr is None : <EOL> w_slice = space . newslice ( w_start , w_stop , space . w_None ) <EOL> return space . delitem ( w_obj , w_slice ) <EOL> w_start , w_stop = old_slice_range ( space , w_obj , w_start , w_stop ) <EOL> return space . get_and_call_function ( w_descr , w_obj , w_start , w_stop ) <EOL> def pow ( space , w_obj1 , w_obj2 , w_obj3 ) : <EOL> w_typ1 = space . type ( w_obj1 ) <EOL> w_typ2 = space . type ( w_obj2 ) <EOL> w_left_src , w_left_impl = space . lookup_in_type_where ( w_typ1 , '<STR_LIT>' ) <EOL> if space . is_w ( w_typ1 , w_typ2 ) : <EOL> w_right_impl = None <EOL> else : <EOL> w_right_src , w_right_impl = space . lookup_in_type_where ( w_typ2 , '<STR_LIT>' ) <EOL> if ( w_left_src is not w_right_src <EOL> and space . is_true ( space . issubtype ( w_typ2 , w_typ1 ) ) ) : <EOL> if ( w_left_src and w_right_src and <EOL> not space . abstract_issubclass_w ( w_left_src , w_right_src ) and <EOL> not space . abstract_issubclass_w ( w_typ1 , w_right_src ) ) : <EOL> w_obj1 , w_obj2 = w_obj2 , w_obj1 <EOL> w_left_impl , w_right_impl = w_right_impl , w_left_impl <EOL> if w_left_impl is not None : <EOL> if space . is_w ( w_obj3 , space . w_None ) : <EOL> w_res = space . get_and_call_function ( w_left_impl , w_obj1 , w_obj2 ) <EOL> else : <EOL> w_res = space . get_and_call_function ( w_left_impl , w_obj1 , w_obj2 , w_obj3 ) <EOL> if _check_notimplemented ( space , w_res ) : <EOL> return w_res <EOL> if w_right_impl is not None : <EOL> if space . is_w ( w_obj3 , space . w_None ) : <EOL> w_res = space . get_and_call_function ( w_right_impl , w_obj2 , w_obj1 ) <EOL> else : <EOL> w_res = space . get_and_call_function ( w_right_impl , w_obj2 , w_obj1 , <EOL> w_obj3 ) <EOL> if _check_notimplemented ( space , w_res ) : <EOL> return w_res <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> def inplace_pow ( space , w_lhs , w_rhs ) : <EOL> w_impl = space . lookup ( w_lhs , '<STR_LIT>' ) <EOL> if w_impl is not None : <EOL> w_res = space . get_and_call_function ( w_impl , w_lhs , w_rhs ) <EOL> if _check_notimplemented ( space , w_res ) : <EOL> return w_res <EOL> return space . pow ( w_lhs , w_rhs , space . w_None ) <EOL> def contains ( space , w_container , w_item ) : <EOL> w_descr = space . lookup ( w_container , '<STR_LIT>' ) <EOL> if w_descr is not None : <EOL> return space . get_and_call_function ( w_descr , w_container , w_item ) <EOL> w_iter = space . iter ( w_container ) <EOL> while <NUM_LIT:1> : <EOL> try : <EOL> w_next = space . next ( w_iter ) <EOL> except OperationError , e : <EOL> if not e . match ( space , space . w_StopIteration ) : <EOL> raise <EOL> return space . w_False <EOL> if space . eq_w ( w_next , w_item ) : <EOL> return space . w_True <EOL> def hash ( space , w_obj ) : <EOL> w_hash = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_hash is None : <EOL> return default_identity_hash ( space , w_obj ) <EOL> w_result = space . get_and_call_function ( w_hash , w_obj ) <EOL> w_resulttype = space . type ( w_result ) <EOL> if space . is_w ( w_resulttype , space . w_int ) : <EOL> return w_result <EOL> elif space . is_w ( w_resulttype , space . w_long ) : <EOL> return space . hash ( w_result ) <EOL> elif space . is_true ( space . isinstance ( w_result , space . w_int ) ) : <EOL> return space . wrap ( space . int_w ( w_result ) ) <EOL> elif space . is_true ( space . isinstance ( w_result , space . w_long ) ) : <EOL> bigint = space . bigint_w ( w_result ) <EOL> return space . wrap ( bigint . hash ( ) ) <EOL> else : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> def userdel ( space , w_obj ) : <EOL> w_del = space . lookup ( w_obj , '<STR_LIT>' ) <EOL> if w_del is not None : <EOL> space . get_and_call_function ( w_del , w_obj ) <EOL> def cmp ( space , w_v , w_w ) : <EOL> if space . is_w ( w_v , w_w ) : <EOL> return space . wrap ( <NUM_LIT:0> ) <EOL> if space . is_w ( space . type ( w_v ) , space . type ( w_w ) ) : <EOL> w_cmp = space . lookup ( w_v , '<STR_LIT>' ) <EOL> w_res = _invoke_binop ( space , w_cmp , w_v , w_w ) <EOL> if w_res is not None : <EOL> return w_res <EOL> if space . eq_w ( w_v , w_w ) : <EOL> return space . wrap ( <NUM_LIT:0> ) <EOL> elif space . is_true ( space . lt ( w_v , w_w ) ) : <EOL> return space . wrap ( - <NUM_LIT:1> ) <EOL> return space . wrap ( <NUM_LIT:1> ) <EOL> def coerce ( space , w_obj1 , w_obj2 ) : <EOL> w_typ1 = space . type ( w_obj1 ) <EOL> w_typ2 = space . type ( w_obj2 ) <EOL> w_left_src , w_left_impl = space . lookup_in_type_where ( w_typ1 , '<STR_LIT>' ) <EOL> if space . is_w ( w_typ1 , w_typ2 ) : <EOL> w_right_impl = None <EOL> else : <EOL> w_right_src , w_right_impl = space . lookup_in_type_where ( w_typ2 , '<STR_LIT>' ) <EOL> if ( w_left_src is not w_right_src <EOL> and space . is_true ( space . issubtype ( w_typ2 , w_typ1 ) ) ) : <EOL> w_obj1 , w_obj2 = w_obj2 , w_obj1 <EOL> w_left_impl , w_right_impl = w_right_impl , w_left_impl <EOL> w_res = _invoke_binop ( space , w_left_impl , w_obj1 , w_obj2 ) <EOL> if w_res is None or space . is_w ( w_res , space . w_None ) : <EOL> w_res = _invoke_binop ( space , w_right_impl , w_obj2 , w_obj1 ) <EOL> if w_res is None or space . is_w ( w_res , space . w_None ) : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> if ( not space . is_true ( space . isinstance ( w_res , space . w_tuple ) ) or <EOL> space . int_w ( space . len ( w_res ) ) != <NUM_LIT:2> ) : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> w_res = space . newtuple ( [ space . getitem ( w_res , space . wrap ( <NUM_LIT:1> ) ) , space . getitem ( w_res , space . wrap ( <NUM_LIT:0> ) ) ] ) <EOL> elif ( not space . is_true ( space . isinstance ( w_res , space . w_tuple ) ) or <EOL> space . int_w ( space . len ( w_res ) ) != <NUM_LIT:2> ) : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" ) ) <EOL> return w_res <EOL> def _check_notimplemented ( space , w_obj ) : <EOL> return not space . is_w ( w_obj , space . w_NotImplemented ) <EOL> def _invoke_binop ( space , w_impl , w_obj1 , w_obj2 ) : <EOL> if w_impl is not None : <EOL> w_res = space . get_and_call_function ( w_impl , w_obj1 , w_obj2 ) <EOL> if _check_notimplemented ( space , w_res ) : <EOL> return w_res <EOL> return None <EOL> def _conditional_neg ( space , w_obj , flag ) : <EOL> if flag : <EOL> return space . neg ( w_obj ) <EOL> else : <EOL> return w_obj <EOL> def _cmp ( space , w_obj1 , w_obj2 ) : <EOL> w_typ1 = space . type ( w_obj1 ) <EOL> w_typ2 = space . type ( w_obj2 ) <EOL> w_left_src , w_left_impl = space . lookup_in_type_where ( w_typ1 , '<STR_LIT>' ) <EOL> do_neg1 = False <EOL> do_neg2 = True <EOL> if space . is_w ( w_typ1 , w_typ2 ) : <EOL> w_right_impl = None <EOL> else : <EOL> w_right_src , w_right_impl = space . lookup_in_type_where ( w_typ2 , '<STR_LIT>' ) <EOL> if ( w_left_src is not w_right_src <EOL> and space . is_true ( space . issubtype ( w_typ2 , w_typ1 ) ) ) : <EOL> w_obj1 , w_obj2 = w_obj2 , w_obj1 <EOL> w_left_impl , w_right_impl = w_right_impl , w_left_impl <EOL> do_neg1 , do_neg2 = do_neg2 , do_neg1 <EOL> w_res = _invoke_binop ( space , w_left_impl , w_obj1 , w_obj2 ) <EOL> if w_res is not None : <EOL> return _conditional_neg ( space , w_res , do_neg1 ) <EOL> w_res = _invoke_binop ( space , w_right_impl , w_obj2 , w_obj1 ) <EOL> if w_res is not None : <EOL> return _conditional_neg ( space , w_res , do_neg2 ) <EOL> if space . is_w ( w_obj1 , w_obj2 ) : <EOL> return space . wrap ( <NUM_LIT:0> ) <EOL> if space . is_w ( w_obj1 , space . w_None ) : <EOL> return space . wrap ( - <NUM_LIT:1> ) <EOL> if space . is_w ( w_obj2 , space . w_None ) : <EOL> return space . wrap ( <NUM_LIT:1> ) <EOL> if space . is_w ( w_typ1 , w_typ2 ) : <EOL> w_id1 = space . id ( w_obj1 ) <EOL> w_id2 = space . id ( w_obj2 ) <EOL> lt = space . is_true ( space . lt ( w_id1 , w_id2 ) ) <EOL> else : <EOL> num1 = number_check ( space , w_obj1 ) <EOL> num2 = number_check ( space , w_obj2 ) <EOL> if num1 != num2 : <EOL> lt = num1 <EOL> else : <EOL> name1 = w_typ1 . getname ( space , "<STR_LIT>" ) <EOL> name2 = w_typ2 . getname ( space , "<STR_LIT>" ) <EOL> if name1 != name2 : <EOL> lt = name1 < name2 <EOL> else : <EOL> w_id1 = space . id ( w_typ1 ) <EOL> w_id2 = space . id ( w_typ2 ) <EOL> lt = space . is_true ( space . lt ( w_id1 , w_id2 ) ) <EOL> if lt : <EOL> return space . wrap ( - <NUM_LIT:1> ) <EOL> else : <EOL> return space . wrap ( <NUM_LIT:1> ) <EOL> def number_check ( space , w_obj ) : <EOL> return ( space . lookup ( w_obj , '<STR_LIT>' ) is not None or <EOL> space . lookup ( w_obj , '<STR_LIT>' ) is not None ) <EOL> class Temp : <EOL> def __getslice__ ( self , i , j ) : <EOL> return j <EOL> slice_max = Temp ( ) [ : ] <EOL> del Temp <EOL> def old_slice_range ( space , w_obj , w_start , w_stop ) : <EOL> """<STR_LIT>""" <EOL> if space . is_w ( w_start , space . w_None ) : <EOL> w_start = space . wrap ( <NUM_LIT:0> ) <EOL> else : <EOL> w_start = space . wrap ( space . getindex_w ( w_start , None ) ) <EOL> if space . is_true ( space . lt ( w_start , space . wrap ( <NUM_LIT:0> ) ) ) : <EOL> try : <EOL> w_start = space . add ( w_start , space . len ( w_obj ) ) <EOL> except OperationError , e : <EOL> if not ( ( e . match ( space , space . w_AttributeError ) or <EOL> e . match ( space , space . w_TypeError ) ) ) : <EOL> raise <EOL> if space . is_w ( w_stop , space . w_None ) : <EOL> w_stop = space . wrap ( slice_max ) <EOL> else : <EOL> w_stop = space . wrap ( space . getindex_w ( w_stop , None ) ) <EOL> if space . is_true ( space . lt ( w_stop , space . wrap ( <NUM_LIT:0> ) ) ) : <EOL> w_stop = space . add ( w_stop , space . len ( w_obj ) ) <EOL> return w_start , w_stop <EOL> def _make_binop_impl ( symbol , specialnames ) : <EOL> left , right = specialnames <EOL> def binop_impl ( space , w_obj1 , w_obj2 ) : <EOL> w_typ1 = space . type ( w_obj1 ) <EOL> w_typ2 = space . type ( w_obj2 ) <EOL> w_left_src , w_left_impl = space . lookup_in_type_where ( w_typ1 , left ) <EOL> if space . is_w ( w_typ1 , w_typ2 ) : <EOL> w_right_impl = None <EOL> else : <EOL> w_right_src , w_right_impl = space . lookup_in_type_where ( w_typ2 , right ) <EOL> if w_left_src is not w_right_src : <EOL> if symbol == '<STR_LIT:+>' and space . is_w ( w_typ1 , space . w_unicode ) : <EOL> w_typ1 = space . w_basestring <EOL> if space . is_true ( space . issubtype ( w_typ2 , w_typ1 ) ) : <EOL> if ( w_left_src and w_right_src and <EOL> not space . abstract_issubclass_w ( w_left_src , w_right_src ) and <EOL> not space . abstract_issubclass_w ( w_typ1 , w_right_src ) ) : <EOL> w_obj1 , w_obj2 = w_obj2 , w_obj1 <EOL> w_left_impl , w_right_impl = w_right_impl , w_left_impl <EOL> w_res = _invoke_binop ( space , w_left_impl , w_obj1 , w_obj2 ) <EOL> if w_res is not None : <EOL> return w_res <EOL> w_res = _invoke_binop ( space , w_right_impl , w_obj2 , w_obj1 ) <EOL> if w_res is not None : <EOL> return w_res <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" % symbol ) ) <EOL> return func_with_new_name ( binop_impl , "<STR_LIT>" % left . strip ( '<STR_LIT:_>' ) ) <EOL> def _make_comparison_impl ( symbol , specialnames ) : <EOL> left , right = specialnames <EOL> op = getattr ( operator , left ) <EOL> def comparison_impl ( space , w_obj1 , w_obj2 ) : <EOL> w_typ1 = space . type ( w_obj1 ) <EOL> w_typ2 = space . type ( w_obj2 ) <EOL> w_left_src , w_left_impl = space . lookup_in_type_where ( w_typ1 , left ) <EOL> w_first = w_obj1 <EOL> w_second = w_obj2 <EOL> if space . is_w ( w_typ1 , w_typ2 ) : <EOL> w_right_impl = None <EOL> else : <EOL> w_right_src , w_right_impl = space . lookup_in_type_where ( w_typ2 , right ) <EOL> if space . is_true ( space . issubtype ( w_typ2 , w_typ1 ) ) : <EOL> w_obj1 , w_obj2 = w_obj2 , w_obj1 <EOL> w_left_impl , w_right_impl = w_right_impl , w_left_impl <EOL> w_res = _invoke_binop ( space , w_left_impl , w_obj1 , w_obj2 ) <EOL> if w_res is not None : <EOL> return w_res <EOL> w_res = _invoke_binop ( space , w_right_impl , w_obj2 , w_obj1 ) <EOL> if w_res is not None : <EOL> return w_res <EOL> w_res = _cmp ( space , w_first , w_second ) <EOL> res = space . int_w ( w_res ) <EOL> return space . wrap ( op ( res , <NUM_LIT:0> ) ) <EOL> return func_with_new_name ( comparison_impl , '<STR_LIT>' % left . strip ( '<STR_LIT:_>' ) ) <EOL> def _make_inplace_impl ( symbol , specialnames ) : <EOL> specialname , = specialnames <EOL> assert specialname . startswith ( '<STR_LIT>' ) and specialname . endswith ( '<STR_LIT>' ) <EOL> noninplacespacemethod = specialname [ <NUM_LIT:3> : - <NUM_LIT:2> ] <EOL> if noninplacespacemethod in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> noninplacespacemethod += '<STR_LIT:_>' <EOL> def inplace_impl ( space , w_lhs , w_rhs ) : <EOL> w_impl = space . lookup ( w_lhs , specialname ) <EOL> if w_impl is not None : <EOL> w_res = space . get_and_call_function ( w_impl , w_lhs , w_rhs ) <EOL> if _check_notimplemented ( space , w_res ) : <EOL> return w_res <EOL> return getattr ( space , noninplacespacemethod ) ( w_lhs , w_rhs ) <EOL> return func_with_new_name ( inplace_impl , '<STR_LIT>' % specialname . strip ( '<STR_LIT:_>' ) ) <EOL> def _make_unaryop_impl ( symbol , specialnames ) : <EOL> specialname , = specialnames <EOL> def unaryop_impl ( space , w_obj ) : <EOL> w_impl = space . lookup ( w_obj , specialname ) <EOL> if w_impl is None : <EOL> raise OperationError ( space . w_TypeError , <EOL> space . wrap ( "<STR_LIT>" % symbol ) ) <EOL> return space . get_and_call_function ( w_impl , w_obj ) <EOL> return func_with_new_name ( unaryop_impl , '<STR_LIT>' % specialname . strip ( '<STR_LIT:_>' ) ) <EOL> for targetname , specialname , checkerspec in [ <EOL> ( '<STR_LIT:int>' , '<STR_LIT>' , ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> ( '<STR_LIT:index>' , '<STR_LIT>' , ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , ( "<STR_LIT>" , "<STR_LIT>" ) ) , <EOL> ( '<STR_LIT:float>' , '<STR_LIT>' , ( "<STR_LIT>" , ) ) ] : <EOL> l = [ "<STR_LIT>" % x <EOL> for x in checkerspec ] <EOL> checker = "<STR_LIT>" . join ( l ) <EOL> source = """<STR_LIT>""" % locals ( ) <EOL> exec compile2 ( source ) <EOL> for targetname , specialname in [ <EOL> ( '<STR_LIT:str>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ] : <EOL> source = """<STR_LIT>""" % locals ( ) <EOL> exec compile2 ( source ) <EOL> for _name , _symbol , _arity , _specialnames in ObjSpace . MethodTable : <EOL> if not hasattr ( DescrOperation , _name ) : <EOL> _impl_maker = None <EOL> if _arity == <NUM_LIT:2> and _name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> _impl_maker = _make_comparison_impl <EOL> elif _arity == <NUM_LIT:2> and _name . startswith ( '<STR_LIT>' ) : <EOL> _impl_maker = _make_inplace_impl <EOL> elif _arity == <NUM_LIT:2> and len ( _specialnames ) == <NUM_LIT:2> : <EOL> _impl_maker = _make_binop_impl <EOL> elif _arity == <NUM_LIT:1> and len ( _specialnames ) == <NUM_LIT:1> : <EOL> _impl_maker = _make_unaryop_impl <EOL> if _impl_maker : <EOL> setattr ( DescrOperation , _name , _impl_maker ( _symbol , _specialnames ) ) <EOL> elif _name not in [ '<STR_LIT>' , '<STR_LIT:id>' , '<STR_LIT:type>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> raise Exception , "<STR_LIT>" % _name </s>
