| 1 |
import sys |
| 2 |
from pypy.interpreter.gateway import interp2app, unwrap_spec |
| 3 |
from pypy.module._rawffi.alt.interp_ffitype import app_types, W_FFIType |
| 4 |
from pypy.module._rawffi.alt.interp_struct import compute_size_and_alignement, W_Field |
| 5 |
from pypy.module._rawffi.alt.test.test_funcptr import BaseAppTestFFI |
| 6 |
|
| 7 |
class TestStruct(object): |
| 8 |
|
| 9 |
class FakeSpace(object): |
| 10 |
def interp_w(self, cls, obj): |
| 11 |
return obj |
| 12 |
|
| 13 |
def compute(self, ffitypes_w): |
| 14 |
fields_w = [W_Field('<dummy>', w_ffitype) for |
| 15 |
w_ffitype in ffitypes_w] |
| 16 |
return compute_size_and_alignement(self.FakeSpace(), fields_w) |
| 17 |
|
| 18 |
def sizeof(self, ffitypes_w): |
| 19 |
size, aligned, fields_w = self.compute(ffitypes_w) |
| 20 |
return size |
| 21 |
|
| 22 |
def test_compute_size(self): |
| 23 |
T = app_types |
| 24 |
byte_size = app_types.sbyte.sizeof() |
| 25 |
long_size = app_types.slong.sizeof() |
| 26 |
llong_size = app_types.slonglong.sizeof() |
| 27 |
llong_align = app_types.slonglong.get_alignment() |
| 28 |
|
| 29 |
assert llong_align >= 4 |
| 30 |
assert self.sizeof([T.sbyte, T.slong]) == 2*long_size |
| 31 |
assert self.sizeof([T.sbyte, T.slonglong]) == llong_align + llong_size |
| 32 |
assert self.sizeof([T.sbyte, T.sbyte, T.slonglong]) == llong_align + llong_size |
| 33 |
assert self.sizeof([T.sbyte, T.sbyte, T.sbyte, T.slonglong]) == llong_align + llong_size |
| 34 |
assert self.sizeof([T.sbyte, T.sbyte, T.sbyte, T.sbyte, T.slonglong]) == llong_align + llong_size |
| 35 |
assert self.sizeof([T.slonglong, T.sbyte]) == llong_size + llong_align |
| 36 |
assert self.sizeof([T.slonglong, T.sbyte, T.sbyte]) == llong_size + llong_align |
| 37 |
assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align |
| 38 |
assert self.sizeof([T.slonglong, T.sbyte, T.sbyte, T.sbyte, T.sbyte]) == llong_size + llong_align |
| 39 |
|
| 40 |
|
| 41 |
class AppTestStruct(BaseAppTestFFI): |
| 42 |
|
| 43 |
@classmethod |
| 44 |
def read_raw_mem(cls, addr, typename, length): |
| 45 |
import ctypes |
| 46 |
addr = ctypes.cast(addr, ctypes.c_void_p) |
| 47 |
c_type = getattr(ctypes, typename) |
| 48 |
array_type = ctypes.POINTER(c_type * length) |
| 49 |
ptr_array = ctypes.cast(addr, array_type) |
| 50 |
array = ptr_array[0] |
| 51 |
lst = [array[i] for i in range(length)] |
| 52 |
return lst |
| 53 |
|
| 54 |
def setup_class(cls): |
| 55 |
BaseAppTestFFI.setup_class.im_func(cls) |
| 56 |
|
| 57 |
from rpython.rlib import clibffi |
| 58 |
from rpython.rlib.rarithmetic import r_uint |
| 59 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 60 |
|
| 61 |
if cls.runappdirect: |
| 62 |
cls.w_read_raw_mem = cls.read_raw_mem |
| 63 |
else: |
| 64 |
@unwrap_spec(addr=r_uint, typename='text', length=int) |
| 65 |
def read_raw_mem_w(space, addr, typename, length): |
| 66 |
return space.wrap(cls.read_raw_mem(addr, typename, length)) |
| 67 |
cls.w_read_raw_mem = cls.space.wrap(interp2app(read_raw_mem_w)) |
| 68 |
|
| 69 |
dummy_type = lltype.malloc(clibffi.FFI_TYPE_P.TO, flavor='raw') |
| 70 |
dummy_type.c_size = r_uint(123) |
| 71 |
dummy_type.c_alignment = rffi.cast(rffi.USHORT, 0) |
| 72 |
dummy_type.c_type = rffi.cast(rffi.USHORT, 0) |
| 73 |
cls.w_dummy_type = W_FFIType('dummy', dummy_type) |
| 74 |
cls.w_runappdirect = cls.space.wrap(cls.runappdirect) |
| 75 |
|
| 76 |
def test__StructDescr(self): |
| 77 |
from _rawffi.alt import _StructDescr, Field, types |
| 78 |
longsize = types.slong.sizeof() |
| 79 |
fields = [ |
| 80 |
Field('x', types.slong), |
| 81 |
Field('y', types.slong), |
| 82 |
] |
| 83 |
descr = _StructDescr('foo', fields) |
| 84 |
assert descr.ffitype.sizeof() == longsize*2 |
| 85 |
assert descr.ffitype.name == 'struct foo' |
| 86 |
|
| 87 |
def test_alignment(self): |
| 88 |
from _rawffi.alt import _StructDescr, Field, types |
| 89 |
longsize = types.slong.sizeof() |
| 90 |
fields = [ |
| 91 |
Field('x', types.sbyte), |
| 92 |
Field('y', types.slong), |
| 93 |
] |
| 94 |
descr = _StructDescr('foo', fields) |
| 95 |
assert descr.ffitype.sizeof() == longsize*2 |
| 96 |
assert fields[0].offset == 0 |
| 97 |
assert fields[1].offset == longsize |
| 98 |
|
| 99 |
def test_missing_field(self): |
| 100 |
from _rawffi.alt import _StructDescr, Field, types |
| 101 |
longsize = types.slong.sizeof() |
| 102 |
fields = [ |
| 103 |
Field('x', types.slong), |
| 104 |
Field('y', types.slong), |
| 105 |
] |
| 106 |
descr = _StructDescr('foo', fields) |
| 107 |
struct = descr.allocate() |
| 108 |
raises(AttributeError, "struct.getfield('missing')") |
| 109 |
raises(AttributeError, "struct.setfield('missing', 42)") |
| 110 |
|
| 111 |
def test_unknown_type(self): |
| 112 |
if self.runappdirect: |
| 113 |
skip('cannot use self.dummy_type with -A') |
| 114 |
from _rawffi.alt import _StructDescr, Field |
| 115 |
fields = [ |
| 116 |
Field('x', self.dummy_type), |
| 117 |
] |
| 118 |
descr = _StructDescr('foo', fields) |
| 119 |
struct = descr.allocate() |
| 120 |
raises(TypeError, "struct.getfield('x')") |
| 121 |
raises(TypeError, "struct.setfield('x', 42)") |
| 122 |
|
| 123 |
def test_getfield_setfield(self): |
| 124 |
from _rawffi.alt import _StructDescr, Field, types |
| 125 |
longsize = types.slong.sizeof() |
| 126 |
fields = [ |
| 127 |
Field('x', types.slong), |
| 128 |
Field('y', types.slong), |
| 129 |
] |
| 130 |
descr = _StructDescr('foo', fields) |
| 131 |
struct = descr.allocate() |
| 132 |
struct.setfield('x', 42) |
| 133 |
struct.setfield('y', 43) |
| 134 |
assert struct.getfield('x') == 42 |
| 135 |
assert struct.getfield('y') == 43 |
| 136 |
mem = self.read_raw_mem(struct.getaddr(), 'c_long', 2) |
| 137 |
assert mem == [42, 43] |
| 138 |
|
| 139 |
def test_getfield_setfield_signed_types(self): |
| 140 |
import sys |
| 141 |
from _rawffi.alt import _StructDescr, Field, types |
| 142 |
if sys.platform == 'win32': |
| 143 |
maxlong = 2 ** 31 - 1 |
| 144 |
else: |
| 145 |
maxlong = sys.maxsize |
| 146 |
fields = [ |
| 147 |
Field('sbyte', types.sbyte), |
| 148 |
Field('sshort', types.sshort), |
| 149 |
Field('sint', types.sint), |
| 150 |
Field('slong', types.slong), |
| 151 |
] |
| 152 |
descr = _StructDescr('foo', fields) |
| 153 |
struct = descr.allocate() |
| 154 |
struct.setfield('sbyte', 128) |
| 155 |
assert struct.getfield('sbyte') == -128 |
| 156 |
struct.setfield('sshort', 32768) |
| 157 |
assert struct.getfield('sshort') == -32768 |
| 158 |
struct.setfield('sint', 43) |
| 159 |
assert struct.getfield('sint') == 43 |
| 160 |
struct.setfield('slong', maxlong + 1) |
| 161 |
assert struct.getfield('slong') == -maxlong - 1 |
| 162 |
struct.setfield('slong', maxlong * 3) |
| 163 |
assert struct.getfield('slong') == maxlong - 2 |
| 164 |
|
| 165 |
def test_getfield_setfield_unsigned_types(self): |
| 166 |
import sys |
| 167 |
from _rawffi.alt import _StructDescr, Field, types |
| 168 |
longsize = types.slong.sizeof() |
| 169 |
if sys.platform == 'win32': |
| 170 |
maxlong = 2 ** 31 - 1 |
| 171 |
else: |
| 172 |
maxlong = sys.maxsize |
| 173 |
fields = [ |
| 174 |
Field('ubyte', types.ubyte), |
| 175 |
Field('ushort', types.ushort), |
| 176 |
Field('uint', types.uint), |
| 177 |
Field('ulong', types.ulong), |
| 178 |
Field('char', types.char), |
| 179 |
Field('unichar', types.unichar), |
| 180 |
Field('ptr', types.void_p), |
| 181 |
] |
| 182 |
descr = _StructDescr('foo', fields) |
| 183 |
struct = descr.allocate() |
| 184 |
struct.setfield('ubyte', -1) |
| 185 |
assert struct.getfield('ubyte') == 255 |
| 186 |
struct.setfield('ushort', -1) |
| 187 |
assert struct.getfield('ushort') == 65535 |
| 188 |
struct.setfield('uint', 43) |
| 189 |
assert struct.getfield('uint') == 43 |
| 190 |
struct.setfield('ulong', -1) |
| 191 |
assert struct.getfield('ulong') == maxlong * 2 + 1 |
| 192 |
struct.setfield('ulong', maxlong * 2 + 2) |
| 193 |
assert struct.getfield('ulong') == 0 |
| 194 |
struct.setfield('char', b'a') |
| 195 |
assert struct.getfield('char') == b'a' |
| 196 |
struct.setfield('unichar', u'\u1234') |
| 197 |
assert struct.getfield('unichar') == u'\u1234' |
| 198 |
struct.setfield('ptr', -1) |
| 199 |
assert struct.getfield('ptr') == sys.maxsize * 2 + 1 |
| 200 |
|
| 201 |
def test_getfield_setfield_longlong(self): |
| 202 |
import sys |
| 203 |
from _rawffi.alt import _StructDescr, Field, types |
| 204 |
fields = [ |
| 205 |
Field('slonglong', types.slonglong), |
| 206 |
Field('ulonglong', types.ulonglong), |
| 207 |
] |
| 208 |
descr = _StructDescr('foo', fields) |
| 209 |
struct = descr.allocate() |
| 210 |
struct.setfield('slonglong', 9223372036854775808) |
| 211 |
assert struct.getfield('slonglong') == -9223372036854775808 |
| 212 |
struct.setfield('ulonglong', -1) |
| 213 |
assert struct.getfield('ulonglong') == 18446744073709551615 |
| 214 |
mem = self.read_raw_mem(struct.getaddr(), 'c_longlong', 2) |
| 215 |
assert mem == [-9223372036854775808, -1] |
| 216 |
|
| 217 |
def test_getfield_setfield_float(self): |
| 218 |
import sys |
| 219 |
from _rawffi.alt import _StructDescr, Field, types |
| 220 |
fields = [ |
| 221 |
Field('x', types.double), |
| 222 |
] |
| 223 |
descr = _StructDescr('foo', fields) |
| 224 |
struct = descr.allocate() |
| 225 |
struct.setfield('x', 123.4) |
| 226 |
assert struct.getfield('x') == 123.4 |
| 227 |
mem = self.read_raw_mem(struct.getaddr(), 'c_double', 1) |
| 228 |
assert mem == [123.4] |
| 229 |
|
| 230 |
def test_getfield_setfield_singlefloat(self): |
| 231 |
import sys |
| 232 |
from _rawffi.alt import _StructDescr, Field, types |
| 233 |
fields = [ |
| 234 |
Field('x', types.float), |
| 235 |
] |
| 236 |
descr = _StructDescr('foo', fields) |
| 237 |
struct = descr.allocate() |
| 238 |
struct.setfield('x', 123.4) |
| 239 |
|
| 240 |
assert 0 < abs(struct.getfield('x') - 123.4) < 0.0001 |
| 241 |
|
| 242 |
struct.setfield('x', 123.5) |
| 243 |
|
| 244 |
assert struct.getfield('x') == 123.5 |
| 245 |
mem = self.read_raw_mem(struct.getaddr(), 'c_float', 1) |
| 246 |
assert mem == [123.5] |
| 247 |
|
| 248 |
def test_define_fields(self): |
| 249 |
from _rawffi.alt import _StructDescr, Field, types |
| 250 |
longsize = types.slong.sizeof() |
| 251 |
fields = [ |
| 252 |
Field('x', types.slong), |
| 253 |
Field('y', types.slong), |
| 254 |
] |
| 255 |
descr = _StructDescr('foo') |
| 256 |
assert descr.ffitype.name == 'struct foo' |
| 257 |
assert repr(descr.ffitype) == '<ffi type struct foo (incomplete)>' |
| 258 |
raises(ValueError, "descr.ffitype.sizeof()") |
| 259 |
raises(ValueError, "descr.allocate()") |
| 260 |
|
| 261 |
descr.define_fields(fields) |
| 262 |
assert repr(descr.ffitype) == '<ffi type struct foo>' |
| 263 |
assert descr.ffitype.sizeof() == longsize*2 |
| 264 |
raises(ValueError, "descr.define_fields(fields)") |
| 265 |
|
| 266 |
def test_pointer_to_incomplete_struct(self): |
| 267 |
from _rawffi.alt import _StructDescr, Field, types |
| 268 |
fields = [ |
| 269 |
Field('x', types.slong), |
| 270 |
Field('y', types.slong), |
| 271 |
] |
| 272 |
descr = _StructDescr('foo') |
| 273 |
foo_ffitype = descr.ffitype |
| 274 |
foo_p = types.Pointer(descr.ffitype) |
| 275 |
assert foo_p.deref_pointer() is foo_ffitype |
| 276 |
descr.define_fields(fields) |
| 277 |
assert descr.ffitype is foo_ffitype |
| 278 |
assert foo_p.deref_pointer() is foo_ffitype |
| 279 |
assert types.Pointer(descr.ffitype) is foo_p |
| 280 |
|
| 281 |
def test_nested_structure(self): |
| 282 |
from _rawffi.alt import _StructDescr, Field, types |
| 283 |
longsize = types.slong.sizeof() |
| 284 |
foo_fields = [ |
| 285 |
Field('x', types.slong), |
| 286 |
Field('y', types.slong), |
| 287 |
] |
| 288 |
foo_descr = _StructDescr('foo', foo_fields) |
| 289 |
|
| 290 |
bar_fields = [ |
| 291 |
Field('x', types.slong), |
| 292 |
Field('foo', foo_descr.ffitype), |
| 293 |
] |
| 294 |
bar_descr = _StructDescr('bar', bar_fields) |
| 295 |
assert bar_descr.ffitype.sizeof() == longsize*3 |
| 296 |
|
| 297 |
struct = bar_descr.allocate() |
| 298 |
struct.setfield('x', 40) |
| 299 |
|
| 300 |
struct_foo = struct.getfield('foo') |
| 301 |
struct_foo.setfield('x', 41) |
| 302 |
struct_foo.setfield('y', 42) |
| 303 |
mem = self.read_raw_mem(struct.getaddr(), 'c_long', 3) |
| 304 |
assert mem == [40, 41, 42] |
| 305 |
|
| 306 |
struct_foo2 = foo_descr.allocate() |
| 307 |
struct_foo2.setfield('x', 141) |
| 308 |
struct_foo2.setfield('y', 142) |
| 309 |
|
| 310 |
struct.setfield('foo', struct_foo2) |
| 311 |
struct_foo2.setfield('x', 241) |
| 312 |
struct_foo2.setfield('y', 242) |
| 313 |
mem = self.read_raw_mem(struct.getaddr(), 'c_long', 3) |
| 314 |
assert mem == [40, 141, 142] |
| 315 |
mem = self.read_raw_mem(struct_foo2.getaddr(), 'c_long', 2) |
| 316 |
assert mem == [241, 242] |
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
def test_compute_shape(self): |
| 321 |
from _rawffi.alt import Structure, Field, types |
| 322 |
class Point(Structure): |
| 323 |
_fields_ = [ |
| 324 |
Field('x', types.slong), |
| 325 |
Field('y', types.slong), |
| 326 |
] |
| 327 |
|
| 328 |
longsize = types.slong.sizeof() |
| 329 |
assert isinstance(Point.x, Field) |
| 330 |
assert isinstance(Point.y, Field) |
| 331 |
assert Point.x.offset == 0 |
| 332 |
assert Point.y.offset == longsize |
| 333 |
assert Point._struct_.ffitype.sizeof() == longsize*2 |
| 334 |
assert Point._struct_.ffitype.name == 'struct Point' |
| 335 |
|