<s> from pypy . rpython . lltypesystem . lltype import * <EOL> def isweak ( p , T ) : <EOL> try : <EOL> typeOf ( p ) <EOL> except TypeError : <EOL> return True <EOL> return False <EOL> def test_basics ( ) : <EOL> S0 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) , ( '<STR_LIT:b>' , Signed ) ) <EOL> assert S0 . a == Signed <EOL> assert S0 . b == Signed <EOL> s0 = malloc ( S0 ) <EOL> print s0 <EOL> assert typeOf ( s0 ) == Ptr ( S0 ) <EOL> py . test . raises ( UninitializedMemoryAccess , "<STR_LIT>" ) <EOL> s0 . a = <NUM_LIT:1> <EOL> s0 . b = s0 . a <EOL> assert s0 . a == <NUM_LIT:1> <EOL> assert s0 . b == <NUM_LIT:1> <EOL> assert typeOf ( s0 . a ) == Signed <EOL> Ar = GcArray ( ( '<STR_LIT:v>' , Signed ) ) <EOL> x = malloc ( Ar , <NUM_LIT:0> ) <EOL> print x <EOL> assert len ( x ) == <NUM_LIT:0> <EOL> x = malloc ( Ar , <NUM_LIT:3> ) <EOL> print x <EOL> assert typeOf ( x ) == Ptr ( Ar ) <EOL> assert isweak ( x [ <NUM_LIT:0> ] , Ar . OF ) <EOL> x [ <NUM_LIT:0> ] . v = <NUM_LIT:1> <EOL> x [ <NUM_LIT:1> ] . v = <NUM_LIT:2> <EOL> x [ <NUM_LIT:2> ] . v = <NUM_LIT:3> <EOL> assert typeOf ( x [ <NUM_LIT:0> ] . v ) == Signed <EOL> assert [ x [ z ] . v for z in range ( <NUM_LIT:3> ) ] == [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] <EOL> def define_list ( T ) : <EOL> List_typ = GcStruct ( "<STR_LIT:list>" , <EOL> ( "<STR_LIT>" , Ptr ( GcArray ( ( '<STR_LIT>' , T ) ) ) ) ) <EOL> def newlist ( ) : <EOL> l = malloc ( List_typ ) <EOL> items = malloc ( List_typ . items . TO , <NUM_LIT:0> ) <EOL> l . items = items <EOL> return l <EOL> def append ( l , newitem ) : <EOL> length = len ( l . items ) <EOL> newitems = malloc ( List_typ . items . TO , length + <NUM_LIT:1> ) <EOL> i = <NUM_LIT:0> <EOL> while i < length : <EOL> newitems [ i ] . item = l . items [ i ] . item <EOL> i += <NUM_LIT:1> <EOL> newitems [ length ] . item = newitem <EOL> l . items = newitems <EOL> def item ( l , i ) : <EOL> return l . items [ i ] . item <EOL> return List_typ , newlist , append , item <EOL> List_typ , inewlist , iappend , iitem = define_list ( Signed ) <EOL> l = inewlist ( ) <EOL> assert typeOf ( l ) == Ptr ( List_typ ) <EOL> iappend ( l , <NUM_LIT:2> ) <EOL> iappend ( l , <NUM_LIT:3> ) <EOL> assert len ( l . items ) == <NUM_LIT:2> <EOL> assert iitem ( l , <NUM_LIT:0> ) == <NUM_LIT:2> <EOL> assert iitem ( l , <NUM_LIT:1> ) == <NUM_LIT:3> <EOL> IWrap = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:v>' , Signed ) ) <EOL> List_typ , iwnewlist , iwappend , iwitem = define_list ( Ptr ( IWrap ) ) <EOL> l = iwnewlist ( ) <EOL> assert typeOf ( l ) == Ptr ( List_typ ) <EOL> iw2 = malloc ( IWrap ) <EOL> iw3 = malloc ( IWrap ) <EOL> iw2 . v = <NUM_LIT:2> <EOL> iw3 . v = <NUM_LIT:3> <EOL> assert iw3 . v == <NUM_LIT:3> <EOL> iwappend ( l , iw2 ) <EOL> iwappend ( l , iw3 ) <EOL> assert len ( l . items ) == <NUM_LIT:2> <EOL> assert iwitem ( l , <NUM_LIT:0> ) . v == <NUM_LIT:2> <EOL> assert iwitem ( l , <NUM_LIT:1> ) . v == <NUM_LIT:3> <EOL> S = Struct ( "<STR_LIT:s>" , ( '<STR_LIT:v>' , Signed ) ) <EOL> List_typ , iwnewlistzzz , iwappendzzz , iwitemzzz = define_list ( S ) <EOL> l = iwnewlistzzz ( ) <EOL> S1 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:s>' , S ) ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> def test_varsizestruct ( ) : <EOL> S1 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) , ( '<STR_LIT>' , Array ( ( '<STR_LIT:v>' , Signed ) ) ) ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> s1 = malloc ( S1 , <NUM_LIT:4> ) <EOL> s1 . a = <NUM_LIT:0> <EOL> assert s1 . a == <NUM_LIT:0> <EOL> assert isweak ( s1 . rest , S1 . rest ) <EOL> assert len ( s1 . rest ) == <NUM_LIT:4> <EOL> assert isweak ( s1 . rest [ <NUM_LIT:0> ] , S1 . rest . OF ) <EOL> s1 . rest [ <NUM_LIT:0> ] . v = <NUM_LIT:0> <EOL> assert typeOf ( s1 . rest [ <NUM_LIT:0> ] . v ) == Signed <EOL> assert s1 . rest [ <NUM_LIT:0> ] . v == <NUM_LIT:0> <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> s1 . a = <NUM_LIT> <EOL> s1 . rest [ <NUM_LIT:3> ] . v = <NUM_LIT:5> <EOL> assert s1 . a == <NUM_LIT> <EOL> assert s1 . rest [ <NUM_LIT:3> ] . v == <NUM_LIT:5> <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> def test_substructure_ptr ( ) : <EOL> S3 = Struct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) ) <EOL> S2 = Struct ( "<STR_LIT>" , ( '<STR_LIT>' , S3 ) ) <EOL> S1 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S2 ) , ( '<STR_LIT>' , S2 ) ) <EOL> p1 = malloc ( S1 ) <EOL> assert isweak ( p1 . sub1 , S2 ) <EOL> assert isweak ( p1 . sub2 , S2 ) <EOL> assert isweak ( p1 . sub1 . s3 , S3 ) <EOL> p2 = p1 . sub1 <EOL> assert isweak ( p2 . s3 , S3 ) <EOL> def test_gc_substructure_ptr ( ) : <EOL> S1 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) ) <EOL> S2 = Struct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) ) <EOL> S0 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S1 ) , ( '<STR_LIT>' , S2 ) ) <EOL> p1 = malloc ( S0 ) <EOL> assert typeOf ( p1 . sub1 ) == Ptr ( S1 ) <EOL> assert isweak ( p1 . sub2 , S2 ) <EOL> def test_cast_simple_widening ( ) : <EOL> S2 = Struct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) ) <EOL> S1 = Struct ( "<STR_LIT>" , ( '<STR_LIT>' , S2 ) , ( '<STR_LIT>' , S2 ) ) <EOL> p1 = malloc ( S1 , immortal = True ) <EOL> p2 = p1 . sub1 <EOL> p3 = p2 <EOL> assert typeOf ( p3 ) == Ptr ( S2 ) <EOL> p4 = cast_pointer ( Ptr ( S1 ) , p3 ) <EOL> assert typeOf ( p4 ) == Ptr ( S1 ) <EOL> assert p4 == p1 <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> SUnrelated = Struct ( "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> S1bis = Struct ( "<STR_LIT>" , ( '<STR_LIT>' , S2 ) ) <EOL> p1b = malloc ( S1bis , immortal = True ) <EOL> p2 = p1b . sub1 <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> def test_cast_simple_widening2 ( ) : <EOL> S2 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) ) <EOL> S1 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S2 ) ) <EOL> p1 = malloc ( S1 ) <EOL> p2 = p1 . sub1 <EOL> assert typeOf ( p2 ) == Ptr ( S2 ) <EOL> p3 = cast_pointer ( Ptr ( S1 ) , p2 ) <EOL> assert p3 == p1 <EOL> p2 = malloc ( S2 ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> def test_cast_pointer ( ) : <EOL> S3 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) ) <EOL> S2 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S3 ) ) <EOL> S1 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S2 ) ) <EOL> p1 = malloc ( S1 ) <EOL> p2 = p1 . sub <EOL> p3 = p2 . sub <EOL> assert typeOf ( p3 ) == Ptr ( S3 ) <EOL> assert typeOf ( p2 ) == Ptr ( S2 ) <EOL> p12 = cast_pointer ( Ptr ( S1 ) , p2 ) <EOL> assert p12 == p1 <EOL> p13 = cast_pointer ( Ptr ( S1 ) , p3 ) <EOL> assert p13 == p1 <EOL> p21 = cast_pointer ( Ptr ( S2 ) , p1 ) <EOL> assert p21 == p2 <EOL> p23 = cast_pointer ( Ptr ( S2 ) , p3 ) <EOL> assert p23 == p2 <EOL> p31 = cast_pointer ( Ptr ( S3 ) , p1 ) <EOL> assert p31 == p3 <EOL> p32 = cast_pointer ( Ptr ( S3 ) , p2 ) <EOL> assert p32 == p3 <EOL> p3 = malloc ( S3 ) <EOL> p2 = malloc ( S2 ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> S0 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S1 ) ) <EOL> p0 = malloc ( S0 ) <EOL> assert p0 == cast_pointer ( Ptr ( S0 ) , p0 ) <EOL> p3 = cast_pointer ( Ptr ( S3 ) , p0 ) <EOL> p03 = cast_pointer ( Ptr ( S0 ) , p3 ) <EOL> assert p0 == p03 <EOL> S1bis = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S2 ) ) <EOL> assert S1bis != S1 <EOL> p1b = malloc ( S1bis ) <EOL> p3 = p1b . sub . sub <EOL> assert typeOf ( p3 ) == Ptr ( S3 ) <EOL> assert p1b == cast_pointer ( Ptr ( S1bis ) , p3 ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> def test_best_effort_gced_parent_detection ( ) : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> S2 = Struct ( "<STR_LIT>" , ( '<STR_LIT:a>' , Signed ) ) <EOL> S1 = GcStruct ( "<STR_LIT>" , ( '<STR_LIT>' , S2 ) , ( '<STR_LIT>' , S2 ) , ( '<STR_LIT>' , Array ( ( '<STR_LIT:e>' , Signed ) ) ) ) <EOL> p1 = malloc ( S1 , <NUM_LIT:1> ) <EOL> p2 = p1 . sub2 <EOL> p3 = p1 . tail <EOL> p3 [ <NUM_LIT:0> ] . e = <NUM_LIT:1> <EOL> assert p3 [ <NUM_LIT:0> ] . e == <NUM_LIT:1> <EOL> del p1 <EOL> import gc <EOL> gc . collect ( ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> def test_best_effort_gced_parent_for_arrays ( ) : <EOL> py . test . skip ( "<STR_LIT>" ) <EOL> A1 = GcArray ( ( '<STR_LIT:v>' , Signed ) ) <EOL> p1 = malloc ( A1 , <NUM_LIT:10> ) <EOL> p1 [ <NUM_LIT:5> ] . v = <NUM_LIT:3> <EOL> assert p1 [ <NUM_LIT:5> ] . v == <NUM_LIT:3> <EOL> p1_5 = p1 [ <NUM_LIT:5> ] <EOL> del p1 <EOL> import gc <EOL> gc . collect ( ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> def test_examples ( ) : <EOL> A1 = GcArray ( ( '<STR_LIT:v>' , Signed ) ) <EOL> S = GcStruct ( "<STR_LIT:s>" , ( '<STR_LIT:v>' , Signed ) ) <EOL> St = GcStruct ( "<STR_LIT>" , ( '<STR_LIT:v>' , Signed ) , ( '<STR_LIT>' , Array ( ( '<STR_LIT:v>' , Signed ) ) ) ) <EOL> PA1 = Ptr ( A1 ) <EOL> PS = Ptr ( S ) <EOL> PSt = Ptr ( St ) <EOL> ex_pa1 = PA1 . _example ( ) <EOL> ex_ps = PS . _example ( ) <EOL> ex_pst = PSt . _example ( ) <EOL> assert typeOf ( ex_pa1 ) == PA1 <EOL> assert typeOf ( ex_ps ) == PS <EOL> assert typeOf ( ex_pst ) == PSt <EOL> assert ex_pa1 [ <NUM_LIT:0> ] . v == <NUM_LIT:0> <EOL> assert ex_ps . v == <NUM_LIT:0> <EOL> assert ex_pst . v == <NUM_LIT:0> <EOL> assert ex_pst . trail [ <NUM_LIT:0> ] . v == <NUM_LIT:0> <EOL> def test_functions ( ) : <EOL> F = FuncType ( ( Signed , ) , Signed ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> PF = Ptr ( F ) <EOL> pf = PF . _example ( ) <EOL> assert pf ( <NUM_LIT:0> ) == <NUM_LIT:0> <EOL> py . test . raises ( TypeError , pf , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> py . test . raises ( TypeError , pf , '<STR_LIT:a>' ) <EOL> def test_truargs ( ) : <EOL> F = FuncType ( ( Void , Signed , Void , Unsigned ) , Float ) <EOL> assert Void not in F . _trueargs ( ) <EOL> def test_inconsistent_gc_containers ( ) : <EOL> A = GcArray ( ( '<STR_LIT:y>' , Signed ) ) <EOL> S = GcStruct ( '<STR_LIT:b>' , ( '<STR_LIT:y>' , Signed ) ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> def test_forward_reference ( ) : <EOL> F = GcForwardReference ( ) <EOL> S = GcStruct ( '<STR_LIT:abc>' , ( '<STR_LIT:x>' , Ptr ( F ) ) ) <EOL> F . become ( S ) <EOL> assert S . x == Ptr ( S ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> ForwardReference ( ) . become ( Struct ( '<STR_LIT:abc>' ) ) <EOL> hash ( S ) <EOL> def test_nullptr ( ) : <EOL> S = Struct ( '<STR_LIT:s>' ) <EOL> p0 = nullptr ( S ) <EOL> assert not p0 <EOL> assert typeOf ( p0 ) == Ptr ( S ) <EOL> def test_nullptr_cast ( ) : <EOL> S = Struct ( '<STR_LIT:s>' ) <EOL> p0 = nullptr ( S ) <EOL> assert not p0 <EOL> S1 = Struct ( "<STR_LIT>" , ( '<STR_LIT:s>' , S ) ) <EOL> p10 = cast_pointer ( Ptr ( S1 ) , p0 ) <EOL> assert typeOf ( p10 ) == Ptr ( S1 ) <EOL> assert not p10 <EOL> def test_nullptr_opaque_cast ( ) : <EOL> S = Struct ( '<STR_LIT:S>' ) <EOL> p0 = nullptr ( S ) <EOL> O1 = OpaqueType ( '<STR_LIT>' ) <EOL> O2 = OpaqueType ( '<STR_LIT>' ) <EOL> p1 = cast_opaque_ptr ( Ptr ( O1 ) , p0 ) <EOL> assert not p1 <EOL> p2 = cast_opaque_ptr ( Ptr ( O2 ) , p1 ) <EOL> assert not p2 <EOL> p3 = cast_opaque_ptr ( Ptr ( S ) , p2 ) <EOL> assert not p3 <EOL> def test_hash ( ) : <EOL> S = ForwardReference ( ) <EOL> S . become ( Struct ( '<STR_LIT:S>' , ( '<STR_LIT:p>' , Ptr ( S ) ) ) ) <EOL> assert S == S <EOL> hash ( S ) <EOL> S1 = Struct ( '<STR_LIT:S>' , ( '<STR_LIT:p>' , Ptr ( S ) ) ) <EOL> assert S1 == S <EOL> assert S == S1 <EOL> assert hash ( S1 ) == hash ( S ) <EOL> def test_array_with_non_container_elements ( ) : <EOL> As = GcArray ( Signed ) <EOL> a = malloc ( As , <NUM_LIT:3> ) <EOL> assert typeOf ( a ) == Ptr ( As ) <EOL> py . test . raises ( UninitializedMemoryAccess , "<STR_LIT>" ) <EOL> a [ <NUM_LIT:1> ] = <NUM_LIT:3> <EOL> assert a [ <NUM_LIT:1> ] == <NUM_LIT:3> <EOL> S = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> s = malloc ( S ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> S = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> S = Struct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> A = GcArray ( S ) <EOL> a = malloc ( A , <NUM_LIT:2> ) <EOL> s = S . _container_example ( ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> S = Struct ( '<STR_LIT:s>' , ( '<STR_LIT>' , Array ( S ) ) ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> def test_immortal_parent ( ) : <EOL> S1 = GcStruct ( '<STR_LIT>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> S = GcStruct ( '<STR_LIT>' , ( '<STR_LIT>' , S1 ) ) <EOL> p = malloc ( S , immortal = True ) <EOL> p1 = p . s1 <EOL> p1 . x = <NUM_LIT:5> <EOL> del p <EOL> p = cast_pointer ( Ptr ( S ) , p1 ) <EOL> assert p . s1 . x == <NUM_LIT:5> <EOL> def test_getRuntimeTypeInfo ( ) : <EOL> S = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> py . test . raises ( ValueError , "<STR_LIT>" ) <EOL> pinf0 = attachRuntimeTypeInfo ( S ) <EOL> assert pinf0 . _obj . about == S <EOL> pinf = getRuntimeTypeInfo ( S ) <EOL> assert pinf == pinf0 <EOL> pinf1 = getRuntimeTypeInfo ( S ) <EOL> assert pinf == pinf1 <EOL> Z = GcStruct ( '<STR_LIT:z>' , ( '<STR_LIT:x>' , Unsigned ) ) <EOL> attachRuntimeTypeInfo ( Z ) <EOL> assert getRuntimeTypeInfo ( Z ) != pinf0 <EOL> Sbis = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> attachRuntimeTypeInfo ( Sbis ) <EOL> assert getRuntimeTypeInfo ( Sbis ) != pinf0 <EOL> assert Sbis != S <EOL> def test_getRuntimeTypeInfo_destrpointer ( ) : <EOL> S = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> def f ( s ) : <EOL> s . x = <NUM_LIT:1> <EOL> def type_info_S ( p ) : <EOL> return getRuntimeTypeInfo ( S ) <EOL> qp = functionptr ( FuncType ( [ Ptr ( S ) ] , Ptr ( RuntimeTypeInfo ) ) , <EOL> "<STR_LIT>" , <EOL> _callable = type_info_S ) <EOL> dp = functionptr ( FuncType ( [ Ptr ( S ) ] , Void ) , <EOL> "<STR_LIT>" , <EOL> _callable = f ) <EOL> pinf0 = attachRuntimeTypeInfo ( S , qp , destrptr = dp ) <EOL> assert pinf0 . _obj . about == S <EOL> pinf = getRuntimeTypeInfo ( S ) <EOL> assert pinf == pinf0 <EOL> pinf1 = getRuntimeTypeInfo ( S ) <EOL> assert pinf == pinf1 <EOL> assert pinf . _obj . destructor_funcptr == dp <EOL> assert pinf . _obj . query_funcptr == qp <EOL> def test_runtime_type_info ( ) : <EOL> S = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> attachRuntimeTypeInfo ( S ) <EOL> s = malloc ( S ) <EOL> s . x = <NUM_LIT:0> <EOL> assert runtime_type_info ( s ) == getRuntimeTypeInfo ( S ) <EOL> S1 = GcStruct ( '<STR_LIT>' , ( '<STR_LIT>' , S ) , ( '<STR_LIT:x>' , Signed ) ) <EOL> attachRuntimeTypeInfo ( S1 ) <EOL> s1 = malloc ( S1 ) <EOL> s1 . sub . x = <NUM_LIT:0> <EOL> s1 . x = <NUM_LIT:0> <EOL> assert runtime_type_info ( s1 ) == getRuntimeTypeInfo ( S1 ) <EOL> assert runtime_type_info ( s1 . sub ) == getRuntimeTypeInfo ( S1 ) <EOL> assert runtime_type_info ( cast_pointer ( Ptr ( S ) , s1 ) ) == getRuntimeTypeInfo ( S1 ) <EOL> def dynamic_type_info_S ( p ) : <EOL> if p . x == <NUM_LIT:0> : <EOL> return getRuntimeTypeInfo ( S ) <EOL> else : <EOL> return getRuntimeTypeInfo ( S1 ) <EOL> fp = functionptr ( FuncType ( [ Ptr ( S ) ] , Ptr ( RuntimeTypeInfo ) ) , <EOL> "<STR_LIT>" , <EOL> _callable = dynamic_type_info_S ) <EOL> attachRuntimeTypeInfo ( S , fp ) <EOL> assert s . x == <NUM_LIT:0> <EOL> assert runtime_type_info ( s ) == getRuntimeTypeInfo ( S ) <EOL> s . x = <NUM_LIT:1> <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> assert s1 . sub . x == <NUM_LIT:0> <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> s1 . sub . x = <NUM_LIT:1> <EOL> assert runtime_type_info ( s1 . sub ) == getRuntimeTypeInfo ( S1 ) <EOL> def test_flavor_malloc ( ) : <EOL> def isweak ( p , T ) : <EOL> return p . _weak and typeOf ( p ) . TO == T <EOL> S = Struct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> py . test . raises ( TypeError , malloc , S ) <EOL> p = malloc ( S , flavor = "<STR_LIT>" ) <EOL> assert typeOf ( p ) . TO == S <EOL> assert not isweak ( p , S ) <EOL> p . x = <NUM_LIT:2> <EOL> free ( p , flavor = "<STR_LIT>" ) <EOL> py . test . raises ( RuntimeError , "<STR_LIT>" ) <EOL> T = GcStruct ( '<STR_LIT:T>' , ( '<STR_LIT:y>' , Signed ) ) <EOL> p = malloc ( T , flavor = "<STR_LIT>" ) <EOL> assert typeOf ( p ) . TO == T <EOL> assert not isweak ( p , T ) <EOL> def test_opaque ( ) : <EOL> O = OpaqueType ( '<STR_LIT:O>' ) <EOL> p1 = opaqueptr ( O , '<STR_LIT>' , hello = "<STR_LIT>" ) <EOL> assert typeOf ( p1 ) == Ptr ( O ) <EOL> assert p1 . _obj . hello == "<STR_LIT>" <EOL> assert parentlink ( p1 . _obj ) == ( None , None ) <EOL> S = GcStruct ( '<STR_LIT:S>' , ( '<STR_LIT>' , O ) ) <EOL> p2 = malloc ( S ) <EOL> assert typeOf ( p2 ) == Ptr ( S ) <EOL> assert typeOf ( p2 . stuff ) == Ptr ( O ) <EOL> assert parentlink ( p2 . stuff . _obj ) == ( p2 . _obj , '<STR_LIT>' ) <EOL> def test_cast_opaque_ptr ( ) : <EOL> O = GcOpaqueType ( '<STR_LIT:O>' ) <EOL> Q = GcOpaqueType ( '<STR_LIT>' ) <EOL> S = GcStruct ( '<STR_LIT:S>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> s = malloc ( S ) <EOL> o = cast_opaque_ptr ( Ptr ( O ) , s ) <EOL> assert typeOf ( o ) . TO == O <EOL> q = cast_opaque_ptr ( Ptr ( Q ) , o ) <EOL> assert typeOf ( q ) . TO == Q <EOL> p = cast_opaque_ptr ( Ptr ( S ) , q ) <EOL> assert typeOf ( p ) . TO == S <EOL> assert p == s <EOL> O1 = OpaqueType ( '<STR_LIT:O>' ) <EOL> S1 = Struct ( '<STR_LIT>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> s1 = malloc ( S1 , immortal = True ) <EOL> o1 = cast_opaque_ptr ( Ptr ( O1 ) , s1 ) <EOL> assert typeOf ( o1 ) . TO == O1 <EOL> p1 = cast_opaque_ptr ( Ptr ( S1 ) , o1 ) <EOL> assert typeOf ( p1 ) . TO == S1 <EOL> assert p1 == s1 <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> S2 = Struct ( '<STR_LIT>' , ( '<STR_LIT:z>' , Signed ) ) <EOL> py . test . raises ( InvalidCast , "<STR_LIT>" ) <EOL> BIG = GcStruct ( '<STR_LIT>' , ( '<STR_LIT:s>' , S ) ) <EOL> UNRELATED = GcStruct ( '<STR_LIT>' ) <EOL> big = malloc ( BIG ) <EOL> unrelated = malloc ( UNRELATED ) <EOL> p1 = cast_opaque_ptr ( Ptr ( O ) , big ) <EOL> p2 = cast_opaque_ptr ( Ptr ( O ) , big ) <EOL> assert p1 == p2 <EOL> p3 = cast_opaque_ptr ( Ptr ( O ) , big . s ) <EOL> assert p1 == p3 <EOL> p4 = cast_opaque_ptr ( Ptr ( O ) , unrelated ) <EOL> assert p1 != p4 <EOL> assert p3 != p4 <EOL> def test_is_atomic ( ) : <EOL> U = Struct ( '<STR_LIT>' , ( '<STR_LIT:z>' , Signed ) ) <EOL> A = Ptr ( RuntimeTypeInfo ) <EOL> P = Ptr ( GcStruct ( '<STR_LIT:p>' ) ) <EOL> Q = GcStruct ( '<STR_LIT:q>' , ( '<STR_LIT:i>' , Signed ) , ( '<STR_LIT:u>' , U ) , ( '<STR_LIT:p>' , P ) ) <EOL> O = OpaqueType ( '<STR_LIT:O>' ) <EOL> F = GcForwardReference ( ) <EOL> assert A . _is_atomic ( ) is True <EOL> assert P . _is_atomic ( ) is False <EOL> assert Q . i . _is_atomic ( ) is True <EOL> assert Q . u . _is_atomic ( ) is True <EOL> assert Q . p . _is_atomic ( ) is False <EOL> assert Q . _is_atomic ( ) is False <EOL> assert O . _is_atomic ( ) is False <EOL> assert F . _is_atomic ( ) is False <EOL> def test_adtmeths ( ) : <EOL> def h_newstruct ( ) : <EOL> return malloc ( S ) <EOL> S = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) , <EOL> adtmeths = { "<STR_LIT>" : h_newstruct } ) <EOL> s = S . h_newstruct ( ) <EOL> assert typeOf ( s ) == Ptr ( S ) <EOL> def h_alloc ( n ) : <EOL> return malloc ( A , n ) <EOL> def h_length ( a ) : <EOL> return len ( a ) <EOL> A = GcArray ( Signed , <EOL> adtmeths = { "<STR_LIT>" : h_alloc , <EOL> "<STR_LIT>" : h_length , <EOL> "<STR_LIT>" : <NUM_LIT:12> } ) <EOL> a = A . h_alloc ( <NUM_LIT:10> ) <EOL> assert typeOf ( a ) == Ptr ( A ) <EOL> assert len ( a ) == <NUM_LIT:10> <EOL> assert a . h_length ( ) == <NUM_LIT:10> <EOL> assert a . _lookup_adtmeth ( "<STR_LIT>" ) ( ) == <NUM_LIT:10> <EOL> assert a . stuff == <NUM_LIT:12> <EOL> assert a . _lookup_adtmeth ( "<STR_LIT>" ) == <NUM_LIT:12> <EOL> def test_adt_typemethod ( ) : <EOL> def h_newstruct ( S ) : <EOL> return malloc ( S ) <EOL> h_newstruct = typeMethod ( h_newstruct ) <EOL> S = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) , <EOL> adtmeths = { "<STR_LIT>" : h_newstruct } ) <EOL> s = S . h_newstruct ( ) <EOL> assert typeOf ( s ) == Ptr ( S ) <EOL> Sprime = GcStruct ( '<STR_LIT:s>' , ( '<STR_LIT:x>' , Signed ) , <EOL> adtmeths = { "<STR_LIT>" : h_newstruct } ) <EOL> assert S == Sprime <EOL> def test_cast_primitive ( ) : <EOL> cases = [ <EOL> ( Float , <NUM_LIT:1> , <NUM_LIT:1.0> ) , <EOL> ( Float , r_singlefloat ( <NUM_LIT> ) , float ( r_singlefloat ( <NUM_LIT> ) ) ) , <EOL> ( Signed , <NUM_LIT:1.0> , <NUM_LIT:1> ) , <EOL> ( Unsigned , <NUM_LIT:1.0> , <NUM_LIT:1> ) , <EOL> ( Signed , r_uint ( - <NUM_LIT:1> ) , - <NUM_LIT:1> ) , <EOL> ( Unsigned , - <NUM_LIT:1> , r_uint ( - <NUM_LIT:1> ) ) , <EOL> ( Char , ord ( '<STR_LIT:a>' ) , '<STR_LIT:a>' ) , <EOL> ( Char , False , chr ( <NUM_LIT:0> ) ) , <EOL> ( Signed , '<STR_LIT:x>' , ord ( '<STR_LIT:x>' ) ) , <EOL> ( Unsigned , u"<STR_LIT:x>" , ord ( u'<STR_LIT:x>' ) ) , <EOL> ] <EOL> for TGT , orig_val , expect in cases : <EOL> res = cast_primitive ( TGT , orig_val ) <EOL> assert typeOf ( res ) == TGT <EOL> assert res == expect <EOL> res = cast_primitive ( SingleFloat , <NUM_LIT> ) <EOL> assert isinstance ( res , r_singlefloat ) <EOL> assert float ( res ) == float ( r_singlefloat ( <NUM_LIT> ) ) <EOL> def test_cast_identical_array_ptr_types ( ) : <EOL> A = GcArray ( Signed ) <EOL> PA = Ptr ( A ) <EOL> a = malloc ( A , <NUM_LIT:2> ) <EOL> assert cast_pointer ( PA , a ) == a <EOL> def test_array_with_no_length ( ) : <EOL> A = GcArray ( Signed , hints = { '<STR_LIT>' : True } ) <EOL> a = malloc ( A , <NUM_LIT:10> ) <EOL> py . test . raises ( TypeError , len , a ) <EOL> def test_dissect_ll_instance ( ) : <EOL> assert list ( dissect_ll_instance ( <NUM_LIT:1> ) ) == [ ( Signed , <NUM_LIT:1> ) ] <EOL> GcS = GcStruct ( "<STR_LIT:S>" , ( '<STR_LIT:x>' , Signed ) ) <EOL> s = malloc ( GcS ) <EOL> s . x = <NUM_LIT:1> <EOL> assert list ( dissect_ll_instance ( s ) ) == [ ( Ptr ( GcS ) , s ) , ( GcS , s . _obj ) , ( Signed , <NUM_LIT:1> ) ] <EOL> A = GcArray ( ( '<STR_LIT:x>' , Signed ) ) <EOL> a = malloc ( A , <NUM_LIT:10> ) <EOL> for i in range ( <NUM_LIT:10> ) : <EOL> a [ i ] . x = i <EOL> expected = [ ( Ptr ( A ) , a ) , ( A , a . _obj ) ] <EOL> for t in [ ( ( A . OF , a . _obj . items [ i ] ) , ( Signed , i ) ) for i in range ( <NUM_LIT:10> ) ] : <EOL> expected . extend ( t ) <EOL> assert list ( dissect_ll_instance ( a ) ) == expected <EOL> R = GcStruct ( "<STR_LIT:R>" , ( '<STR_LIT:r>' , Ptr ( GcForwardReference ( ) ) ) ) <EOL> R . r . TO . become ( R ) <EOL> r = malloc ( R ) <EOL> r . r = r <EOL> r_expected = [ ( Ptr ( R ) , r ) , ( R , r . _obj ) ] <EOL> assert list ( dissect_ll_instance ( r ) ) == r_expected <EOL> B = GcArray ( Ptr ( R ) ) <EOL> b = malloc ( B , <NUM_LIT:2> ) <EOL> b [ <NUM_LIT:0> ] = b [ <NUM_LIT:1> ] = r <EOL> b_expected = [ ( Ptr ( B ) , b ) , ( B , b . _obj ) ] <EOL> assert list ( dissect_ll_instance ( b ) ) == b_expected + r_expected <EOL> memo = { } <EOL> assert list ( dissect_ll_instance ( r , None , memo ) ) == r_expected <EOL> assert list ( dissect_ll_instance ( b , None , memo ) ) == b_expected <EOL> def test_fixedsizearray ( ) : <EOL> A = FixedSizeArray ( Signed , <NUM_LIT:5> ) <EOL> assert A . OF == Signed <EOL> assert A . length == <NUM_LIT:5> <EOL> assert A . item0 == A . item1 == A . item2 == A . item3 == A . item4 == Signed <EOL> assert A . _names == ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> a = malloc ( A , immortal = True ) <EOL> a [ <NUM_LIT:0> ] = <NUM_LIT:5> <EOL> a [ <NUM_LIT:4> ] = <NUM_LIT> <EOL> assert a [ <NUM_LIT:0> ] == <NUM_LIT:5> <EOL> assert a [ <NUM_LIT:4> ] == <NUM_LIT> <EOL> assert a . item4 == <NUM_LIT> <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> assert len ( a ) == <NUM_LIT:5> <EOL> S = GcStruct ( '<STR_LIT:S>' , ( '<STR_LIT>' , Signed ) , <EOL> ( '<STR_LIT:a>' , A ) , <EOL> ( '<STR_LIT>' , Signed ) ) <EOL> s = malloc ( S ) <EOL> s . a [ <NUM_LIT:3> ] = <NUM_LIT> <EOL> assert s . a [ <NUM_LIT:3> ] == <NUM_LIT> <EOL> assert len ( s . a ) == <NUM_LIT:5> <EOL> py . test . raises ( TypeError , "<STR_LIT>" ) <EOL> def test_direct_arrayitems ( ) : <EOL> for a in [ malloc ( GcArray ( Signed ) , <NUM_LIT:5> ) , <EOL> malloc ( FixedSizeArray ( Signed , <NUM_LIT:5> ) , immortal = True ) ] : <EOL> a [ <NUM_LIT:0> ] = <NUM_LIT:0> <EOL> a [ <NUM_LIT:1> ] = <NUM_LIT:10> <EOL> a [ <NUM_LIT:2> ] = <NUM_LIT:20> <EOL> a [ <NUM_LIT:3> ] = <NUM_LIT:30> <EOL> a [ <NUM_LIT:4> ] = <NUM_LIT> <EOL> b0 = direct_arrayitems ( a ) <EOL> b1 = direct_ptradd ( b0 , <NUM_LIT:1> ) <EOL> b2 = direct_ptradd ( b1 , <NUM_LIT:1> ) <EOL> b3 = direct_ptradd ( b0 , <NUM_LIT:3> ) <EOL> assert b0 [ <NUM_LIT:0> ] == <NUM_LIT:0> <EOL> assert b0 [ <NUM_LIT:1> ] == <NUM_LIT:10> <EOL> assert b0 [ <NUM_LIT:4> ] == <NUM_LIT> <EOL> assert b1 [ <NUM_LIT:0> ] == <NUM_LIT:10> <EOL> assert b1 [ <NUM_LIT:1> ] == <NUM_LIT:20> <EOL> assert b2 [ <NUM_LIT:0> ] == <NUM_LIT:20> <EOL> assert b2 [ <NUM_LIT:1> ] == <NUM_LIT:30> <EOL> assert b3 [ - <NUM_LIT:2> ] == <NUM_LIT:10> <EOL> assert b3 [ <NUM_LIT:0> ] == <NUM_LIT:30> <EOL> assert b3 [ <NUM_LIT:1> ] == <NUM_LIT> <EOL> assert b2 [ - <NUM_LIT:2> ] == <NUM_LIT:0> <EOL> assert b1 [ <NUM_LIT:3> ] == <NUM_LIT> <EOL> b2 [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> assert a [ <NUM_LIT:2> ] == <NUM_LIT> <EOL> b1 [ <NUM_LIT:1> ] += <NUM_LIT:1> <EOL> assert a [ <NUM_LIT:2> ] == <NUM_LIT> <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> def test_direct_fieldptr ( ) : <EOL> S = GcStruct ( '<STR_LIT:S>' , ( '<STR_LIT:x>' , Signed ) , ( '<STR_LIT:y>' , Signed ) ) <EOL> s = malloc ( S ) <EOL> a = direct_fieldptr ( s , '<STR_LIT:y>' ) <EOL> a [ <NUM_LIT:0> ] = <NUM_LIT> <EOL> assert s . y == <NUM_LIT> <EOL> py . test . raises ( IndexError , "<STR_LIT>" ) <EOL> def test_odd_ints ( ) : <EOL> T = GcStruct ( '<STR_LIT:T>' ) <EOL> S = GcStruct ( '<STR_LIT:S>' , ( '<STR_LIT:t>' , T ) ) <EOL> s = cast_int_to_ptr ( Ptr ( S ) , <NUM_LIT> ) <EOL> assert typeOf ( s ) == Ptr ( S ) <EOL> assert cast_ptr_to_int ( s ) == <NUM_LIT> <EOL> t = cast_pointer ( Ptr ( T ) , s ) <EOL> assert typeOf ( t ) == Ptr ( T ) <EOL> assert cast_ptr_to_int ( t ) == <NUM_LIT> <EOL> assert s == cast_pointer ( Ptr ( S ) , t ) <EOL> def test_str_of_dead_ptr ( ) : <EOL> S = Struct ( '<STR_LIT:S>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> T = GcStruct ( '<STR_LIT:T>' , ( '<STR_LIT:s>' , S ) ) <EOL> t = malloc ( T ) <EOL> s = t . s <EOL> del t <EOL> import gc <EOL> gc . collect ( ) <EOL> repr ( s ) <EOL> def test_pyobject ( ) : <EOL> p = pyobjectptr ( { <NUM_LIT> : <NUM_LIT> } ) <EOL> assert typeOf ( p ) == Ptr ( PyObject ) <EOL> def test_name_clash ( ) : <EOL> import re <EOL> from pypy . rpython . lltypesystem import lltype <EOL> fn = lltype . __file__ <EOL> if fn . lower ( ) . endswith ( '<STR_LIT>' ) or fn . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> fn = fn [ : - <NUM_LIT:1> ] <EOL> f = open ( fn , '<STR_LIT:r>' ) <EOL> data = f . read ( ) <EOL> f . close ( ) <EOL> words = dict . fromkeys ( re . compile ( r"<STR_LIT>" ) . findall ( data ) ) <EOL> words = words . keys ( ) <EOL> S = GcStruct ( '<STR_LIT>' , * [ ( word , Signed ) for word in words ] ) <EOL> s = malloc ( S ) <EOL> for i , word in enumerate ( words ) : <EOL> setattr ( s , word , i ) <EOL> for i , word in enumerate ( words ) : <EOL> assert getattr ( s , word ) == i <EOL> def test_subarray_keeps_array_alive ( ) : <EOL> A = Array ( Signed ) <EOL> ptr = malloc ( A , <NUM_LIT:10> , immortal = True ) <EOL> ptr2 = direct_arrayitems ( ptr ) <EOL> del ptr <EOL> import gc ; gc . collect ( ) ; gc . collect ( ) <EOL> ptr2 [ <NUM_LIT:0> ] = <NUM_LIT:5> <EOL> def test_identityhash ( ) : <EOL> S = GcStruct ( '<STR_LIT:S>' , ( '<STR_LIT:x>' , Signed ) ) <EOL> S2 = GcStruct ( '<STR_LIT>' , ( '<STR_LIT>' , S ) ) <EOL> S3 = GcStruct ( '<STR_LIT>' , ( '<STR_LIT>' , S2 ) ) <EOL> py . test . raises ( AssertionError , identityhash , nullptr ( S2 ) ) <EOL> s3 = malloc ( S3 ) <EOL> hash3 = identityhash ( s3 . super ) <EOL> assert hash3 == identityhash ( s3 ) <EOL> assert hash3 == identityhash ( s3 . super ) <EOL> assert hash3 == identityhash ( s3 . super . super ) <EOL> py . test . raises ( ValueError , init_identity_hash , s3 , hash3 ^ <NUM_LIT:1> ) <EOL> py . test . raises ( ValueError , init_identity_hash , s3 . super , hash3 ^ <NUM_LIT:4> ) <EOL> py . test . raises ( ValueError , init_identity_hash , s3 . super . super , hash3 ^ <NUM_LIT:9> ) <EOL> s3 = malloc ( S3 ) <EOL> init_identity_hash ( s3 . super , - <NUM_LIT> ) <EOL> assert - <NUM_LIT> == identityhash ( s3 ) <EOL> assert - <NUM_LIT> == identityhash ( s3 . super ) <EOL> assert - <NUM_LIT> == identityhash ( s3 . super . super ) <EOL> py . test . raises ( ValueError , init_identity_hash , s3 , <NUM_LIT> ) <EOL> py . test . raises ( ValueError , init_identity_hash , s3 . super , <NUM_LIT:0> ) <EOL> py . test . raises ( ValueError , init_identity_hash , s3 . super . super , - <NUM_LIT> ) <EOL> from pypy . rpython . lltypesystem import llmemory <EOL> p3 = cast_opaque_ptr ( llmemory . GCREF , s3 ) <EOL> assert - <NUM_LIT> == identityhash ( p3 ) <EOL> A = GcArray ( Signed ) <EOL> a = malloc ( A , <NUM_LIT:3> ) <EOL> hash1 = identityhash ( a ) <EOL> assert hash1 == identityhash ( a ) <EOL> p = cast_opaque_ptr ( llmemory . GCREF , a ) <EOL> assert hash1 == identityhash ( p ) </s>
