From 1679696612007107dac55d936006b1923eda2475 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Sat, 6 Jun 2015 22:57:31 +0300 Subject: [PATCH] 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. --- extmod/moductypes.c | 9 +++------ tests/extmod/uctypes_bytearray.py | 2 +- tests/extmod/uctypes_le.py | 2 +- tests/extmod/uctypes_native_le.py | 2 +- tests/extmod/uctypes_ptr_le.py | 2 +- tests/extmod/uctypes_ptr_native_le.py | 2 +- tests/extmod/uctypes_sizeof.py | 2 +- tests/extmod/uctypes_sizeof_native.py | 2 +- 8 files changed, 10 insertions(+), 13 deletions(-) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index dff8abd8d4..56da809311 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -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]); diff --git a/tests/extmod/uctypes_bytearray.py b/tests/extmod/uctypes_bytearray.py index 9f3d7ca8bd..7294b7ea45 100644 --- a/tests/extmod/uctypes_bytearray.py +++ b/tests/extmod/uctypes_bytearray.py @@ -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) diff --git a/tests/extmod/uctypes_le.py b/tests/extmod/uctypes_le.py index 416a007448..ff499476f9 100644 --- a/tests/extmod/uctypes_le.py +++ b/tests/extmod/uctypes_le.py @@ -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)) diff --git a/tests/extmod/uctypes_native_le.py b/tests/extmod/uctypes_native_le.py index b4694994a2..a053b68d55 100644 --- a/tests/extmod/uctypes_native_le.py +++ b/tests/extmod/uctypes_native_le.py @@ -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)) diff --git a/tests/extmod/uctypes_ptr_le.py b/tests/extmod/uctypes_ptr_le.py index 411799db5f..4bff585171 100644 --- a/tests/extmod/uctypes_ptr_le.py +++ b/tests/extmod/uctypes_ptr_le.py @@ -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") diff --git a/tests/extmod/uctypes_ptr_native_le.py b/tests/extmod/uctypes_ptr_native_le.py index ba06b26505..0d02cfdc8e 100644 --- a/tests/extmod/uctypes_ptr_native_le.py +++ b/tests/extmod/uctypes_ptr_native_le.py @@ -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") diff --git a/tests/extmod/uctypes_sizeof.py b/tests/extmod/uctypes_sizeof.py index f6551a7382..2f2a0c0d00 100644 --- a/tests/extmod/uctypes_sizeof.py +++ b/tests/extmod/uctypes_sizeof.py @@ -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 diff --git a/tests/extmod/uctypes_sizeof_native.py b/tests/extmod/uctypes_sizeof_native.py index 0dfbfa980a..f830a1f85e 100644 --- a/tests/extmod/uctypes_sizeof_native.py +++ b/tests/extmod/uctypes_sizeof_native.py @@ -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