<s> """<STR_LIT>""" <EOL> import types <EOL> import unittest <EOL> from test import test_support <EOL> class Old : <EOL> pass <EOL> class New ( object ) : <EOL> pass <EOL> old = Old ( ) <EOL> new = New ( ) <EOL> class TestDescrTestCase ( unittest . TestCase ) : <EOL> def test_class_dict_is_copy ( self ) : <EOL> class FooMeta ( type ) : <EOL> def __new__ ( meta , name , bases , class_dict ) : <EOL> cls = type . __new__ ( meta , name , bases , class_dict ) <EOL> self . assert_ ( '<STR_LIT:foo>' not in class_dict ) <EOL> cls . foo = '<STR_LIT:bar>' <EOL> self . assert_ ( '<STR_LIT:foo>' not in class_dict ) <EOL> return cls <EOL> class Foo ( object ) : <EOL> __metaclass__ = FooMeta <EOL> def test_descr___get__ ( self ) : <EOL> class Foo ( object ) : <EOL> __slots__ = '<STR_LIT:bar>' <EOL> def hello ( self ) : <EOL> pass <EOL> def hi ( self ) : <EOL> pass <EOL> hi = staticmethod ( hi ) <EOL> foo = Foo ( ) <EOL> foo . bar = '<STR_LIT>' <EOL> self . assertEqual ( Foo . bar . __get__ ( foo ) , '<STR_LIT>' ) <EOL> self . assertEqual ( Foo . bar . __get__ ( None , Foo ) , Foo . bar ) <EOL> bound = Foo . hello . __get__ ( foo ) <EOL> self . assert_ ( isinstance ( bound , types . MethodType ) ) <EOL> self . assert_ ( bound . im_self is foo ) <EOL> self . assertEqual ( Foo . hello . __get__ ( None , Foo ) , Foo . hello ) <EOL> bound = Foo . hi . __get__ ( foo ) <EOL> self . assert_ ( isinstance ( bound , types . MethodType ) ) <EOL> self . assert_ ( bound . im_self is foo ) <EOL> unbound = Foo . hi . __get__ ( None , foo ) <EOL> self . assert_ ( isinstance ( unbound , types . MethodType ) ) <EOL> self . assert_ ( unbound . im_self is None ) <EOL> def test_ints ( self ) : <EOL> class C ( int ) : <EOL> pass <EOL> try : <EOL> foo = int ( None ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . assert_ ( False , "<STR_LIT>" ) <EOL> try : <EOL> foo = C ( None ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . assert_ ( False , "<STR_LIT>" ) <EOL> def test_raising_custom_attribute_error ( self ) : <EOL> class RaisesCustomMsg ( object ) : <EOL> def __get__ ( self , instance , type ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> class CustomAttributeError ( AttributeError ) : pass <EOL> class RaisesCustomErr ( object ) : <EOL> def __get__ ( self , instance , type ) : <EOL> raise CustomAttributeError <EOL> class Foo ( object ) : <EOL> custom_msg = RaisesCustomMsg ( ) <EOL> custom_err = RaisesCustomErr ( ) <EOL> self . assertRaises ( CustomAttributeError , lambda : Foo ( ) . custom_err ) <EOL> try : <EOL> Foo ( ) . custom_msg <EOL> self . assert_ ( False ) <EOL> except AttributeError , e : <EOL> self . assertEquals ( "<STR_LIT>" , str ( e ) ) <EOL> def test_set_without_get ( self ) : <EOL> class Descr ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def __set__ ( self , obj , value ) : <EOL> obj . __dict__ [ self . name ] = value <EOL> descr = Descr ( "<STR_LIT:a>" ) <EOL> class X ( object ) : <EOL> a = descr <EOL> x = X ( ) <EOL> self . assertTrue ( x . a is descr ) <EOL> x . a = <NUM_LIT> <EOL> self . assertEqual ( x . a , <NUM_LIT> ) <EOL> class SubclassDescrTestCase ( unittest . TestCase ) : <EOL> def test_subclass_cmp_right_op ( self ) : <EOL> class B ( int ) : <EOL> def __ge__ ( self , other ) : <EOL> return "<STR_LIT>" <EOL> def __le__ ( self , other ) : <EOL> return "<STR_LIT>" <EOL> self . assertEqual ( B ( <NUM_LIT:1> ) >= <NUM_LIT:1> , "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT:1> >= B ( <NUM_LIT:1> ) , "<STR_LIT>" ) <EOL> class C ( object ) : <EOL> def __ge__ ( self , other ) : <EOL> return "<STR_LIT>" <EOL> def __le__ ( self , other ) : <EOL> return "<STR_LIT>" <EOL> self . assertEqual ( C ( ) >= <NUM_LIT:1> , "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT:1> >= C ( ) , "<STR_LIT>" ) <EOL> class D ( C ) : <EOL> def __ge__ ( self , other ) : <EOL> return "<STR_LIT>" <EOL> def __le__ ( self , other ) : <EOL> return "<STR_LIT>" <EOL> self . assertEqual ( D ( ) >= C ( ) , "<STR_LIT>" ) <EOL> self . assertEqual ( C ( ) >= D ( ) , "<STR_LIT>" ) <EOL> class E ( C ) : <EOL> pass <EOL> self . assertEqual ( E . __le__ , C . __le__ ) <EOL> self . assertEqual ( E ( ) >= <NUM_LIT:1> , "<STR_LIT>" ) <EOL> self . assertEqual ( <NUM_LIT:1> >= E ( ) , "<STR_LIT>" ) <EOL> self . assertEqual ( E ( ) >= C ( ) , "<STR_LIT>" ) <EOL> self . assertEqual ( C ( ) >= E ( ) , "<STR_LIT>" ) <EOL> def test_subclass_binop ( self ) : <EOL> def raises ( exc , expected , callable , * args ) : <EOL> try : <EOL> callable ( * args ) <EOL> except exc , msg : <EOL> if str ( msg ) != expected : <EOL> self . assert_ ( False , "<STR_LIT>" % ( str ( msg ) , <EOL> expected ) ) <EOL> else : <EOL> self . assert_ ( False , "<STR_LIT>" % exc ) <EOL> class B ( object ) : <EOL> pass <EOL> class C ( object ) : <EOL> def __radd__ ( self , o ) : <EOL> return '<STR_LIT>' % ( o , ) <EOL> def __rmul__ ( self , o ) : <EOL> return '<STR_LIT>' % ( o , ) <EOL> mapping = [ ] <EOL> mapping . append ( ( lambda o : '<STR_LIT:foo>' + o , <EOL> TypeError , "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) <EOL> if test_support . is_jython : <EOL> mapping . append ( ( lambda o : u'<STR_LIT:foo>' + o , <EOL> TypeError , "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) <EOL> else : <EOL> mapping . append ( ( lambda o : u'<STR_LIT:foo>' + o , <EOL> TypeError , <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" ) ) <EOL> mapping . append ( ( lambda o : [ <NUM_LIT:1> , <NUM_LIT:2> ] + o , <EOL> TypeError , '<STR_LIT>' , <EOL> '<STR_LIT>' ) ) <EOL> mapping . append ( ( lambda o : ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) + o , <EOL> TypeError , '<STR_LIT>' , <EOL> "<STR_LIT>" ) ) <EOL> mapping . append ( ( lambda o : '<STR_LIT:foo>' * o , <EOL> TypeError , "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) <EOL> mapping . append ( ( lambda o : u'<STR_LIT:foo>' * o , <EOL> TypeError , "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) <EOL> mapping . append ( ( lambda o : [ <NUM_LIT:1> , <NUM_LIT:2> ] * o , <EOL> TypeError , "<STR_LIT>" , <EOL> '<STR_LIT>' ) ) <EOL> mapping . append ( ( lambda o : ( '<STR_LIT:foo>' , '<STR_LIT:bar>' ) * o , <EOL> TypeError , "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) <EOL> for func , bexc , bexc_msg , cresult in mapping : <EOL> raises ( bexc , bexc_msg , lambda : func ( B ( ) ) ) <EOL> self . assertEqual ( func ( C ( ) ) , cresult ) <EOL> def test_overriding_base_binop ( self ) : <EOL> class MulBase ( object ) : <EOL> def __init__ ( self , value ) : <EOL> self . value = value <EOL> def __mul__ ( self , other ) : <EOL> return self . value * other . value <EOL> def __rmul__ ( self , other ) : <EOL> return other . value * self . value <EOL> class DoublerBase ( MulBase ) : <EOL> def __mul__ ( self , other ) : <EOL> return <NUM_LIT:2> * ( self . value * other . value ) <EOL> class AnotherDoubler ( DoublerBase ) : <EOL> pass <EOL> self . assertEquals ( DoublerBase ( <NUM_LIT:2> ) * AnotherDoubler ( <NUM_LIT:3> ) , <NUM_LIT:12> ) <EOL> def test_oldstyle_binop_notimplemented ( self ) : <EOL> class Foo : <EOL> pass <EOL> class Bar ( object ) : <EOL> def __radd__ ( self , other ) : <EOL> return <NUM_LIT:3> <EOL> self . assertEqual ( Foo ( ) + Bar ( ) , <NUM_LIT:3> ) <EOL> def test_int_mul ( self ) : <EOL> class Foo ( tuple ) : <EOL> def __rmul__ ( self , other ) : <EOL> return '<STR_LIT:foo>' <EOL> foo = Foo ( ) <EOL> self . assertEqual ( <NUM_LIT> * foo , '<STR_LIT:foo>' ) <EOL> self . assertEqual ( <NUM_LIT:4> * foo , '<STR_LIT:foo>' ) <EOL> class InPlaceTestCase ( unittest . TestCase ) : <EOL> def test_iadd ( self ) : <EOL> class Foo ( object ) : <EOL> def __add__ ( self , other ) : <EOL> return <NUM_LIT:1> <EOL> def __radd__ ( self , other ) : <EOL> return <NUM_LIT:2> <EOL> class Bar ( object ) : <EOL> pass <EOL> class Baz ( object ) : <EOL> def __iadd__ ( self , other ) : <EOL> return NotImplemented <EOL> foo = Foo ( ) <EOL> foo += Bar ( ) <EOL> self . assertEqual ( foo , <NUM_LIT:1> ) <EOL> bar = Bar ( ) <EOL> bar += Foo ( ) <EOL> self . assertEqual ( bar , <NUM_LIT:2> ) <EOL> baz = Baz ( ) <EOL> baz += Foo ( ) <EOL> self . assertEqual ( baz , <NUM_LIT:2> ) <EOL> def test_imul ( self ) : <EOL> class FooInplace ( list ) : <EOL> def __imul__ ( self , other ) : <EOL> return [ <NUM_LIT:1> ] <EOL> class Bar ( FooInplace ) : <EOL> def __mul__ ( self , other ) : <EOL> return [ <NUM_LIT:2> ] <EOL> foo = FooInplace ( ) <EOL> foo *= <NUM_LIT:3> <EOL> self . assertEqual ( foo , [ <NUM_LIT:1> ] ) <EOL> foo = Bar ( [ <NUM_LIT:3> ] ) <EOL> foo *= <NUM_LIT:3> <EOL> self . assertEqual ( foo , [ <NUM_LIT:1> ] ) <EOL> class Baz ( FooInplace ) : <EOL> def __mul__ ( self , other ) : <EOL> return [ <NUM_LIT:3> ] <EOL> baz = Baz ( ) <EOL> baz *= <NUM_LIT:3> <EOL> self . assertEqual ( baz , [ <NUM_LIT:1> ] ) <EOL> def test_list ( self ) : <EOL> class Foo ( list ) : <EOL> def __mul__ ( self , other ) : <EOL> return [ <NUM_LIT:1> ] <EOL> foo = Foo ( [ <NUM_LIT:2> ] ) <EOL> foo *= <NUM_LIT:3> <EOL> if test_support . is_jython : <EOL> self . assertEqual ( foo , [ <NUM_LIT:2> , <NUM_LIT:2> , <NUM_LIT:2> ] ) <EOL> else : <EOL> self . assertEqual ( foo , [ <NUM_LIT:1> ] ) <EOL> class Bar ( object ) : <EOL> def __radd__ ( self , other ) : <EOL> return <NUM_LIT:1> <EOL> def __rmul__ ( self , other ) : <EOL> return <NUM_LIT:2> <EOL> l = [ ] <EOL> l += Bar ( ) <EOL> self . assertEqual ( l , <NUM_LIT:1> ) <EOL> l = [ ] <EOL> l *= Bar ( ) <EOL> self . assertEqual ( l , <NUM_LIT:2> ) <EOL> def test_iand ( self ) : <EOL> class Foo ( set ) : <EOL> def __and__ ( self , other ) : <EOL> return set ( [ <NUM_LIT:1> ] ) <EOL> foo = Foo ( ) <EOL> foo &= <NUM_LIT:3> <EOL> self . assertEqual ( foo , set ( [ <NUM_LIT:1> ] ) ) <EOL> class DescrExceptionsTestCase ( unittest . TestCase ) : <EOL> def test_hex ( self ) : <EOL> self . _test ( hex ) <EOL> def test_oct ( self ) : <EOL> self . _test ( oct ) <EOL> def test_other ( self ) : <EOL> for op in '<STR_LIT:->' , '<STR_LIT:+>' , '<STR_LIT>' : <EOL> try : <EOL> eval ( '<STR_LIT>' % op ) <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> self . _assert ( False , '<STR_LIT>' % op ) <EOL> try : <EOL> eval ( '<STR_LIT>' % op ) <EOL> except TypeError : <EOL> pass <EOL> else : <EOL> self . _assert ( False , '<STR_LIT>' % op ) <EOL> def _test ( self , func ) : <EOL> self . assertRaises ( AttributeError , func , old ) <EOL> self . assertRaises ( TypeError , func , new ) <EOL> def test_eq ( self ) : <EOL> class A ( object ) : <EOL> def __eq__ ( self , other ) : <EOL> return self . value == other . value <EOL> self . assertRaises ( AttributeError , lambda : A ( ) == A ( ) ) <EOL> class GetAttrTestCase ( unittest . TestCase ) : <EOL> def test_raising_custom_attribute_error ( self ) : <EOL> class BarAttributeError ( AttributeError ) : pass <EOL> class Bar ( object ) : <EOL> def __getattr__ ( self , name ) : <EOL> raise BarAttributeError <EOL> class BarClassic : <EOL> def __getattr__ ( self , name ) : <EOL> raise BarAttributeError <EOL> class Foo ( object ) : <EOL> def __getattr__ ( self , name ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> class FooClassic : <EOL> def __getattr__ ( self , name ) : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> self . assertRaises ( BarAttributeError , lambda : Bar ( ) . x ) <EOL> self . assertRaises ( BarAttributeError , lambda : BarClassic ( ) . x ) <EOL> try : <EOL> Foo ( ) . x <EOL> self . assert_ ( False ) <EOL> except AttributeError , e : <EOL> self . assertEquals ( "<STR_LIT>" , str ( e ) ) <EOL> try : <EOL> FooClassic ( ) . x <EOL> self . assert_ ( False ) <EOL> except AttributeError , e : <EOL> self . assertEquals ( "<STR_LIT>" , str ( e ) ) <EOL> class Base ( object ) : <EOL> def __init__ ( self , name ) : <EOL> self . name = name <EOL> def lookup_where ( obj , name ) : <EOL> mro = type ( obj ) . __mro__ <EOL> for t in mro : <EOL> if name in t . __dict__ : <EOL> return t . __dict__ [ name ] , t <EOL> return None , None <EOL> def refop ( x , y , opname , ropname ) : <EOL> t1 = type ( x ) <EOL> t2 = type ( y ) <EOL> op , where1 = lookup_where ( x , opname ) <EOL> rop , where2 = lookup_where ( y , ropname ) <EOL> if op is None and rop is not None : <EOL> return rop ( y , x ) <EOL> if rop and where1 is not where2 : <EOL> if ( issubclass ( t2 , t1 ) and not issubclass ( where1 , where2 ) and <EOL> not issubclass ( t1 , where2 ) ) : <EOL> return rop ( y , x ) <EOL> if op is None : <EOL> return "<STR_LIT>" <EOL> return op ( x , y ) <EOL> def do_test ( X , Y , name , impl ) : <EOL> x = X ( '<STR_LIT:x>' ) <EOL> y = Y ( '<STR_LIT:y>' ) <EOL> opname = '<STR_LIT>' % name <EOL> ropname = '<STR_LIT>' % name <EOL> count = [ <NUM_LIT:0> ] <EOL> fail = [ ] <EOL> def check ( z1 , z2 ) : <EOL> ref = refop ( z1 , z2 , opname , ropname ) <EOL> try : <EOL> v = impl ( z1 , z2 ) <EOL> except TypeError : <EOL> v = "<STR_LIT>" <EOL> if v != ref : <EOL> fail . append ( count [ <NUM_LIT:0> ] ) <EOL> def override_in_hier ( n = <NUM_LIT:6> ) : <EOL> if n == <NUM_LIT:0> : <EOL> count [ <NUM_LIT:0> ] += <NUM_LIT:1> <EOL> check ( x , y ) <EOL> check ( y , x ) <EOL> return <EOL> f = lambda self , other : ( n , self . name , other . name ) <EOL> if n % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> name = opname <EOL> else : <EOL> name = ropname <EOL> for C in Y . __mro__ : <EOL> if name in C . __dict__ : <EOL> continue <EOL> if C is not object : <EOL> setattr ( C , name , f ) <EOL> override_in_hier ( n - <NUM_LIT:1> ) <EOL> if C is not object : <EOL> delattr ( C , name ) <EOL> override_in_hier ( ) <EOL> return fail <EOL> class BinopCombinationsTestCase ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_binop_combinations_mul ( self ) : <EOL> class X ( Base ) : <EOL> pass <EOL> class Y ( X ) : <EOL> pass <EOL> fail = do_test ( X , Y , '<STR_LIT>' , lambda x , y : x * y ) <EOL> self . assert_ ( not fail ) <EOL> def test_binop_combinations_sub ( self ) : <EOL> class X ( Base ) : <EOL> pass <EOL> class Y ( X ) : <EOL> pass <EOL> fail = do_test ( X , Y , '<STR_LIT>' , lambda x , y : x - y ) <EOL> self . assert_ ( not fail ) <EOL> def test_binop_combinations_pow ( self ) : <EOL> class X ( Base ) : <EOL> pass <EOL> class Y ( X ) : <EOL> pass <EOL> fail = do_test ( X , Y , '<STR_LIT>' , lambda x , y : x ** y ) <EOL> self . assert_ ( not fail ) <EOL> def test_binop_combinations_more_exhaustive ( self ) : <EOL> class X ( Base ) : <EOL> pass <EOL> class B1 ( object ) : <EOL> pass <EOL> class B2 ( object ) : <EOL> pass <EOL> class X1 ( B1 , X , B2 ) : <EOL> pass <EOL> class C1 ( object ) : <EOL> pass <EOL> class C2 ( object ) : <EOL> pass <EOL> class Y ( C1 , X1 , C2 ) : <EOL> pass <EOL> fail = do_test ( X , Y , '<STR_LIT>' , lambda x , y : x - y ) <EOL> self . assert_ ( not fail ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( TestDescrTestCase , <EOL> SubclassDescrTestCase , <EOL> InPlaceTestCase , <EOL> DescrExceptionsTestCase , <EOL> GetAttrTestCase , <EOL> BinopCombinationsTestCase ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> test_main ( ) </s>
