from binascii import hexlify
from ctypes import *
-from ctypes.test import is_resource_enabled
def bin(s):
return hexlify(buffer(s)).upper()
s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
self.failUnlessEqual(bin(s1), bin(s2))
- if is_resource_enabled("unaligned_access"):
-
- def test_unaligned_nonnative_struct_fields(self):
- if sys.byteorder == "little":
- base = BigEndianStructure
- fmt = ">b h xi xd"
- else:
- base = LittleEndianStructure
- fmt = "<b h xi xd"
+ def test_unaligned_nonnative_struct_fields(self):
+ if sys.byteorder == "little":
+ base = BigEndianStructure
+ fmt = ">b h xi xd"
+ else:
+ base = LittleEndianStructure
+ fmt = "<b h xi xd"
- class S(base):
- _pack_ = 1
- _fields_ = [("b", c_byte),
+ class S(base):
+ _pack_ = 1
+ _fields_ = [("b", c_byte),
- ("h", c_short),
+ ("h", c_short),
- ("_1", c_byte),
- ("i", c_int),
+ ("_1", c_byte),
+ ("i", c_int),
- ("_2", c_byte),
- ("d", c_double)]
+ ("_2", c_byte),
+ ("d", c_double)]
- s1 = S(0x12, 0x1234, 0, 0x12345678, 0, 3.14)
- s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
- self.failUnlessEqual(bin(s1), bin(s2))
+ s1 = S()
+ s1.b = 0x12
+ s1.h = 0x1234
+ s1.i = 0x12345678
+ s1.d = 3.14
+ s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
+ self.failUnlessEqual(bin(s1), bin(s2))
- def test_unaligned_native_struct_fields(self):
- if sys.byteorder == "little":
- fmt = "<b h xi xd"
- else:
- base = LittleEndianStructure
- fmt = ">b h xi xd"
+ def test_unaligned_native_struct_fields(self):
+ if sys.byteorder == "little":
+ fmt = "<b h xi xd"
+ else:
+ base = LittleEndianStructure
+ fmt = ">b h xi xd"
- class S(Structure):
- _pack_ = 1
- _fields_ = [("b", c_byte),
+ class S(Structure):
+ _pack_ = 1
+ _fields_ = [("b", c_byte),
- ("h", c_short),
+ ("h", c_short),
- ("_1", c_byte),
- ("i", c_int),
+ ("_1", c_byte),
+ ("i", c_int),
- ("_2", c_byte),
- ("d", c_double)]
+ ("_2", c_byte),
+ ("d", c_double)]
- s1 = S(0x12, 0x1234, 0, 0x12345678, 0, 3.14)
- s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
- self.failUnlessEqual(bin(s1), bin(s2))
+ s1 = S()
+ s1.b = 0x12
+ s1.h = 0x1234
+ s1.i = 0x12345678
+ s1.d = 3.14
+ s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
+ self.failUnlessEqual(bin(s1), bin(s2))
if __name__ == "__main__":
unittest.main()
h_set(void *ptr, PyObject *value, unsigned size)
{
long val;
+ short x;
if (get_long(value, &val) < 0)
return NULL;
- *(short *)ptr = (short)SET(*(short *)ptr, (short)val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (short)val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
short field;
if (get_long(value, &val) < 0)
return NULL;
- field = SWAP_2(*(short *)ptr);
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_2(field);
field = SET(field, (short)val, size);
- *(short *)ptr = SWAP_2(field);
+ field = SWAP_2(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
h_get(void *ptr, unsigned size)
{
- short val = *(short *)ptr;
+ short val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
- return PyInt_FromLong(val);
+ return PyInt_FromLong((long)val);
}
static PyObject *
h_get_sw(void *ptr, unsigned size)
{
- short val = *(short *)ptr;
+ short val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_2(val);
GET_BITFIELD(val, size);
return PyInt_FromLong(val);
H_set(void *ptr, PyObject *value, unsigned size)
{
unsigned long val;
+ unsigned short x;
if (get_ulong(value, &val) < 0)
return NULL;
- *(unsigned short *)ptr = (unsigned short)SET(*(unsigned short *)ptr,
- (unsigned short)val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (unsigned short)val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
unsigned short field;
if (get_ulong(value, &val) < 0)
return NULL;
- field = SWAP_2(*(unsigned short *)ptr);
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_2(field);
field = SET(field, (unsigned short)val, size);
- *(unsigned short *)ptr = SWAP_2(field);
+ field = SWAP_2(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
H_get(void *ptr, unsigned size)
{
- unsigned short val = *(unsigned short *)ptr;
+ unsigned short val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyInt_FromLong(val);
}
static PyObject *
H_get_sw(void *ptr, unsigned size)
{
- unsigned short val = *(unsigned short *)ptr;
+ unsigned short val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_2(val);
GET_BITFIELD(val, size);
return PyInt_FromLong(val);
i_set(void *ptr, PyObject *value, unsigned size)
{
long val;
+ int x;
if (get_long(value, &val) < 0)
return NULL;
- *(int *)ptr = (int)SET(*(int *)ptr, (int)val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (int)val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
int field;
if (get_long(value, &val) < 0)
return NULL;
- field = SWAP_INT(*(int *)ptr);
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_INT(field);
field = SET(field, (int)val, size);
- *(int *)ptr = SWAP_INT(field);
+ field = SWAP_INT(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
i_get(void *ptr, unsigned size)
{
- int val = *(int *)ptr;
+ int val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyInt_FromLong(val);
}
static PyObject *
i_get_sw(void *ptr, unsigned size)
{
- int val = *(int *)ptr;
+ int val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_INT(val);
GET_BITFIELD(val, size);
return PyInt_FromLong(val);
I_set(void *ptr, PyObject *value, unsigned size)
{
unsigned long val;
+ unsigned int x;
if (get_ulong(value, &val) < 0)
return NULL;
- *(unsigned int *)ptr = (unsigned int)SET(*(unsigned int *)ptr, (unsigned int)val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, (unsigned int)val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
unsigned int field;
if (get_ulong(value, &val) < 0)
return NULL;
- field = SWAP_INT(*(unsigned int *)ptr);
+ memcpy(&field, ptr, sizeof(field));
field = (unsigned int)SET(field, (unsigned int)val, size);
- *(unsigned int *)ptr = SWAP_INT(field);
+ field = SWAP_INT(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
I_get(void *ptr, unsigned size)
{
- unsigned int val = *(unsigned int *)ptr;
+ unsigned int val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
}
static PyObject *
I_get_sw(void *ptr, unsigned size)
{
- unsigned int val = *(unsigned int *)ptr;
+ unsigned int val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_INT(val);
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
l_set(void *ptr, PyObject *value, unsigned size)
{
long val;
+ long x;
if (get_long(value, &val) < 0)
return NULL;
- *(long *)ptr = (long)SET(*(long *)ptr, val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
long field;
if (get_long(value, &val) < 0)
return NULL;
- field = SWAP_LONG(*(long *)ptr);
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_LONG(field);
field = (long)SET(field, val, size);
- *(long *)ptr = SWAP_LONG(field);
+ field = SWAP_LONG(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
l_get(void *ptr, unsigned size)
{
- long val = *(long *)ptr;
+ long val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyInt_FromLong(val);
}
static PyObject *
l_get_sw(void *ptr, unsigned size)
{
- long val = *(long *)ptr;
+ long val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_LONG(val);
GET_BITFIELD(val, size);
return PyInt_FromLong(val);
L_set(void *ptr, PyObject *value, unsigned size)
{
unsigned long val;
+ unsigned long x;
if (get_ulong(value, &val) < 0)
return NULL;
- *(unsigned long *)ptr = (unsigned long)SET(*(unsigned long *)ptr, val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
unsigned long field;
if (get_ulong(value, &val) < 0)
return NULL;
- field = SWAP_LONG(*(unsigned long *)ptr);
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_LONG(field);
field = (unsigned long)SET(field, val, size);
- *(unsigned long *)ptr = SWAP_LONG(field);
+ field = SWAP_LONG(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
L_get(void *ptr, unsigned size)
{
- unsigned long val = *(unsigned long *)ptr;
+ unsigned long val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
}
static PyObject *
L_get_sw(void *ptr, unsigned size)
{
- unsigned long val = *(unsigned long *)ptr;
+ unsigned long val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_LONG(val);
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLong(val);
q_set(void *ptr, PyObject *value, unsigned size)
{
PY_LONG_LONG val;
+ PY_LONG_LONG x;
if (get_longlong(value, &val) < 0)
return NULL;
- *(PY_LONG_LONG *)ptr = (PY_LONG_LONG)SET(*(PY_LONG_LONG *)ptr, val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
PY_LONG_LONG field;
if (get_longlong(value, &val) < 0)
return NULL;
- field = SWAP_8(*(PY_LONG_LONG *)ptr);
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_8(field);
field = (PY_LONG_LONG)SET(field, val, size);
- *(PY_LONG_LONG *)ptr = SWAP_8(field);
+ field = SWAP_8(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
q_get(void *ptr, unsigned size)
{
- PY_LONG_LONG val = *(PY_LONG_LONG *)ptr;
+ PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromLongLong(val);
}
static PyObject *
q_get_sw(void *ptr, unsigned size)
{
- PY_LONG_LONG val = *(PY_LONG_LONG *)ptr;
+ PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_8(val);
GET_BITFIELD(val, size);
return PyLong_FromLongLong(val);
Q_set(void *ptr, PyObject *value, unsigned size)
{
unsigned PY_LONG_LONG val;
+ unsigned PY_LONG_LONG x;
if (get_ulonglong(value, &val) < 0)
return NULL;
- *(unsigned PY_LONG_LONG *)ptr = (unsigned PY_LONG_LONG)SET(*(unsigned PY_LONG_LONG *)ptr, val, size);
+ memcpy(&x, ptr, sizeof(x));
+ x = SET(x, val, size);
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
unsigned PY_LONG_LONG field;
if (get_ulonglong(value, &val) < 0)
return NULL;
- field = SWAP_8(*(unsigned PY_LONG_LONG *)ptr);
+ memcpy(&field, ptr, sizeof(field));
+ field = SWAP_8(field);
field = (unsigned PY_LONG_LONG)SET(field, val, size);
- *(unsigned PY_LONG_LONG *)ptr = SWAP_8(field);
+ field = SWAP_8(field);
+ memcpy(ptr, &field, sizeof(field));
_RET(value);
}
static PyObject *
Q_get(void *ptr, unsigned size)
{
- unsigned PY_LONG_LONG val = *(unsigned PY_LONG_LONG *)ptr;
+ unsigned PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLongLong(val);
}
static PyObject *
Q_get_sw(void *ptr, unsigned size)
{
- unsigned PY_LONG_LONG val = *(unsigned PY_LONG_LONG *)ptr;
+ unsigned PY_LONG_LONG val;
+ memcpy(&val, ptr, sizeof(val));
val = SWAP_8(val);
GET_BITFIELD(val, size);
return PyLong_FromUnsignedLongLong(val);
value->ob_type->tp_name);
return NULL;
}
- *(double *)ptr = x;
+ memcpy(ptr, &x, sizeof(double));
_RET(value);
}
static PyObject *
d_get(void *ptr, unsigned size)
{
- return PyFloat_FromDouble(*(double *)ptr);
+ double val;
+ memcpy(&val, ptr, sizeof(val));
+ return PyFloat_FromDouble(val);
}
static PyObject *
value->ob_type->tp_name);
return NULL;
}
- *(float *)ptr = x;
+ memcpy(ptr, &x, sizeof(x));
_RET(value);
}
static PyObject *
f_get(void *ptr, unsigned size)
{
- return PyFloat_FromDouble(*(float *)ptr);
+ float val;
+ memcpy(&val, ptr, sizeof(val));
+ return PyFloat_FromDouble(val);
}
static PyObject *