def test_struct_fields(self): longsize = 4 if IS_32_BIT else 8 POINT = lltype.Struct('POINT', ('x', rffi.LONG), ('y', rffi.SHORT), ('z', rffi.VOIDP), ) y_ofs = longsize z_ofs = longsize*2 p = lltype.malloc(POINT, flavor='raw') p.x = 42 p.y = rffi.cast(rffi.SHORT, -1) p.z = rffi.cast(rffi.VOIDP, 0x1234) addr = rffi.cast(rffi.VOIDP, p) assert struct_getfield_int(types.slong, addr, 0) == 42 assert struct_getfield_int(types.sshort, addr, y_ofs) == -1 assert struct_getfield_int(types.pointer, addr, z_ofs) == 0x1234 # struct_setfield_int(types.slong, addr, 0, 43) struct_setfield_int(types.sshort, addr, y_ofs, 0x1234FFFE) # 0x1234 is masked out struct_setfield_int(types.pointer, addr, z_ofs, 0x4321) assert p.x == 43 assert p.y == -2 assert rffi.cast(rffi.LONG, p.z) == 0x4321 # lltype.free(p, flavor='raw')
def test_struct_fields(self): longsize = 4 if IS_32_BIT else 8 POINT = lltype.Struct( 'POINT', ('x', rffi.LONG), ('y', rffi.SHORT), ('z', rffi.VOIDP), ) y_ofs = longsize z_ofs = longsize * 2 p = lltype.malloc(POINT, flavor='raw') p.x = 42 p.y = rffi.cast(rffi.SHORT, -1) p.z = rffi.cast(rffi.VOIDP, 0x1234) addr = rffi.cast(rffi.VOIDP, p) assert struct_getfield_int(types.slong, addr, 0) == 42 assert struct_getfield_int(types.sshort, addr, y_ofs) == -1 assert struct_getfield_int(types.pointer, addr, z_ofs) == 0x1234 # struct_setfield_int(types.slong, addr, 0, 43) struct_setfield_int(types.sshort, addr, y_ofs, 0x1234FFFE) # 0x1234 is masked out struct_setfield_int(types.pointer, addr, z_ofs, 0x4321) assert p.x == 43 assert p.y == -2 assert rffi.cast(rffi.LONG, p.z) == 0x4321 # lltype.free(p, flavor='raw')
def get_unichar(self, w_ffitype): intval = libffi.struct_getfield_int(w_ffitype.get_ffitype(), self.rawmem, self.offset) return rffi.cast(rffi.WCHAR_T, intval)
def get_unsigned(self, w_ffitype): value = libffi.struct_getfield_int(w_ffitype.get_ffitype(), self.rawmem, self.offset) return r_uint(value)
def get_signed(self, w_ffitype): return libffi.struct_getfield_int(w_ffitype.get_ffitype(), self.rawmem, self.offset)