Beispiel #1
0
def test_getfield_pure():
    S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
    S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                         hints={'immutable': True})
    accessor = rclass.FieldListAccessor()
    #
    s1 = lltype.malloc(S1); s1.x = 45
    py.test.raises(TypeError, llop.getfield, lltype.Signed, s1, 'x')
    s2 = lltype.malloc(S2); s2.x = 45
    assert llop.getfield(lltype.Signed, s2, 'x') == 45
    #
    py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s1, 'x')
    assert llop.getinteriorfield(lltype.Signed, s2, 'x') == 45
    #
    for kind in [rclass.IR_MUTABLE, rclass.IR_IMMUTABLE,
                 rclass.IR_IMMUTABLE_ARRAY, rclass.IR_QUASIIMMUTABLE,
                 rclass.IR_QUASIIMMUTABLE_ARRAY]:
        #
        S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                             hints={'immutable_fields': accessor})
        accessor.initialize(S3, {'x': kind})
        s3 = lltype.malloc(S3); s3.x = 46; s3.y = 47
        if kind in [rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY]:
            assert llop.getfield(lltype.Signed, s3, 'x') == 46
            assert llop.getinteriorfield(lltype.Signed, s3, 'x') == 46
        else:
            py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'x')
            py.test.raises(TypeError, llop.getinteriorfield,
                           lltype.Signed, s3, 'x')
        py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'y')
        py.test.raises(TypeError, llop.getinteriorfield,
                       lltype.Signed, s3, 'y')
Beispiel #2
0
def test_llop_fold():
    assert llop.int_add(lltype.Signed, 10, 2) == 12
    assert llop.int_add(lltype.Signed, -6, -7) == -13
    S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
    s1 = lltype.malloc(S1)
    s1.x = 123
    assert llop.getfield(lltype.Signed, s1, 'x') == 123
    S2 = lltype.GcStruct('S2', ('x', lltype.Signed))
    s2 = lltype.malloc(S2)
    s2.x = 123
    py.test.raises(TypeError, "llop.getfield(lltype.Signed, s2, 'x')")
Beispiel #3
0
def test_llop_fold():
    assert llop.int_add(lltype.Signed, 10, 2) == 12
    assert llop.int_add(lltype.Signed, -6, -7) == -13
    S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
    s1 = lltype.malloc(S1)
    s1.x = 123
    assert llop.getfield(lltype.Signed, s1, 'x') == 123
    S2 = lltype.GcStruct('S2', ('x', lltype.Signed))
    s2 = lltype.malloc(S2)
    s2.x = 123
    py.test.raises(TypeError, "llop.getfield(lltype.Signed, s2, 'x')")
Beispiel #4
0
def test_getfield_pure():
    S1 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed))
    S2 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                         hints={'immutable': True})
    accessor = rclass.FieldListAccessor()
    #
    s1 = lltype.malloc(S1)
    s1.x = 45
    py.test.raises(TypeError, llop.getfield, lltype.Signed, s1, 'x')
    s2 = lltype.malloc(S2)
    s2.x = 45
    assert llop.getfield(lltype.Signed, s2, 'x') == 45
    #
    py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s1, 'x')
    assert llop.getinteriorfield(lltype.Signed, s2, 'x') == 45
    #
    for kind in [
            rclass.IR_MUTABLE, rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY,
            rclass.IR_QUASIIMMUTABLE, rclass.IR_QUASIIMMUTABLE_ARRAY
    ]:
        #
        S3 = lltype.GcStruct('S', ('x', lltype.Signed), ('y', lltype.Signed),
                             hints={'immutable_fields': accessor})
        accessor.initialize(S3, {'x': kind})
        s3 = lltype.malloc(S3)
        s3.x = 46
        s3.y = 47
        if kind in [rclass.IR_IMMUTABLE, rclass.IR_IMMUTABLE_ARRAY]:
            assert llop.getfield(lltype.Signed, s3, 'x') == 46
            assert llop.getinteriorfield(lltype.Signed, s3, 'x') == 46
        else:
            py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'x')
            py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s3,
                           'x')
        py.test.raises(TypeError, llop.getfield, lltype.Signed, s3, 'y')
        py.test.raises(TypeError, llop.getinteriorfield, lltype.Signed, s3,
                       'y')