moductypes: Swap address and descriptor args in constructor.

Now address comes first, and args related to struct type are groupped next.
Besides clear groupping, should help catch errors eagerly (e.g. forgetting
to pass address will error out).

Also, improve args number checking/reporting overall.
modussl
Paul Sokolovsky 2015-06-06 22:57:31 +03:00
parent 07408cbd1f
commit 1679696612
8 changed files with 10 additions and 13 deletions

View File

@ -122,14 +122,11 @@ STATIC NORETURN void syntax_error(void) {
}
STATIC mp_obj_t uctypes_struct_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
(void)n_kw;
if (n_args < 2 || n_args > 3) {
syntax_error();
}
mp_arg_check_num(n_args, n_kw, 2, 3, false);
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = type_in;
o->desc = args[0];
o->addr = (void*)mp_obj_get_int(args[1]);
o->addr = (void*)mp_obj_get_int(args[0]);
o->desc = args[1];
o->flags = LAYOUT_NATIVE;
if (n_args == 3) {
o->flags = mp_obj_get_int(args[2]);

View File

@ -7,7 +7,7 @@ desc = {
data = bytearray(b"01234567")
S = uctypes.struct(desc, uctypes.addressof(data), uctypes.LITTLE_ENDIAN)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
# Arrays of UINT8 are accessed as bytearrays
print(S.arr)

View File

@ -22,7 +22,7 @@ desc = {
data = bytearray(b"01")
S = uctypes.struct(desc, uctypes.addressof(data), uctypes.LITTLE_ENDIAN)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
#print(S)
print(hex(S.s0))

View File

@ -31,7 +31,7 @@ desc = {
data = bytearray(b"01")
S = uctypes.struct(desc, uctypes.addressof(data), uctypes.NATIVE)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.NATIVE)
#print(S)
print(hex(S.s0))

View File

@ -16,7 +16,7 @@ bytes = b"01"
addr = uctypes.addressof(bytes)
buf = addr.to_bytes(uctypes.sizeof(desc))
S = uctypes.struct(desc, uctypes.addressof(buf), uctypes.LITTLE_ENDIAN)
S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.LITTLE_ENDIAN)
print(S.ptr[0])
assert S.ptr[0] == ord("0")

View File

@ -17,7 +17,7 @@ bytes = b"01"
addr = uctypes.addressof(bytes)
buf = addr.to_bytes(uctypes.sizeof(desc))
S = uctypes.struct(desc, uctypes.addressof(buf), uctypes.NATIVE)
S = uctypes.struct(uctypes.addressof(buf), desc, uctypes.NATIVE)
print(S.ptr[0])
assert S.ptr[0] == ord("0")

View File

@ -10,7 +10,7 @@ desc = {
data = bytearray(b"01234567")
S = uctypes.struct(desc, uctypes.addressof(data), uctypes.LITTLE_ENDIAN)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
print(uctypes.sizeof(S.arr))
assert uctypes.sizeof(S.arr) == 2

View File

@ -32,7 +32,7 @@ S5 = {
assert uctypes.sizeof(S5) == 12
s5 = uctypes.struct(S5, 0)
s5 = uctypes.struct(0, S5)
assert uctypes.sizeof(s5) == 12
assert uctypes.sizeof(s5.sub) == 2