Skip to content

Commit e9db840

Browse files
committed
py: Start implementing "struct" module.
Only calcsize() and unpack() functions provided so far, for little-endian byte order. Format strings don't support repition spec (like "2b3i"). Unfortunately, dealing with all the various binary type sizes and alignments will lead to quite a bloated "binary" helper functions - if optimizing for speed. Need to think if using dynamic parametrized algos makes more sense.
1 parent acb133d commit e9db840

9 files changed

Lines changed: 163 additions & 0 deletions

File tree

py/binary.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,52 @@ mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
7777
return MP_OBJ_NEW_SMALL_INT(val);
7878
}
7979

80+
mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr) {
81+
machine_int_t val = 0;
82+
byte *p = *ptr;
83+
switch (typecode) {
84+
case 'b':
85+
val = (int8_t)*p++;
86+
break;
87+
case BYTEARRAY_TYPECODE:
88+
case 'B':
89+
val = *p++;
90+
break;
91+
case 'h':
92+
val = (int16_t)((p[1] << 8) | p[0]);
93+
break;
94+
case 'H':
95+
val = (p[1] << 8) | p[0];
96+
break;
97+
case 'i':
98+
case 'l':
99+
val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
100+
*ptr = p + 4;
101+
return mp_obj_new_int(val);
102+
case 'I':
103+
case 'L':
104+
val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
105+
*ptr = p + 4;
106+
return mp_obj_new_int_from_uint(val);
107+
#if 0 //TODO
108+
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
109+
case 'q':
110+
case 'Q':
111+
// TODO: Explode API more to cover signedness
112+
return mp_obj_new_int_from_ll(((long long*)p)[index]);
113+
#endif
114+
#if MICROPY_ENABLE_FLOAT
115+
case 'f':
116+
return mp_obj_new_float(((float*)p)[index]);
117+
case 'd':
118+
return mp_obj_new_float(((double*)p)[index]);
119+
#endif
120+
#endif
121+
}
122+
*ptr = p;
123+
return MP_OBJ_NEW_SMALL_INT(val);
124+
}
125+
80126
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
81127
machine_int_t val = 0;
82128
if (MP_OBJ_IS_INT(val_in)) {

py/binary.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44

55
int mp_binary_get_size(char typecode);
66
mp_obj_t mp_binary_get_val(char typecode, void *p, int index);
7+
mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr);
78
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in);

py/builtin.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ extern const mp_obj_module_t mp_module_collections;
4141
extern const mp_obj_module_t mp_module_io;
4242
extern const mp_obj_module_t mp_module_math;
4343
extern const mp_obj_module_t mp_module_micropython;
44+
extern const mp_obj_module_t mp_module_struct;

py/builtintables.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ STATIC const mp_map_elem_t mp_builtin_module_table[] = {
127127
{ MP_OBJ_NEW_QSTR(MP_QSTR_io), (mp_obj_t)&mp_module_io },
128128
#endif
129129
{ MP_OBJ_NEW_QSTR(MP_QSTR_collections), (mp_obj_t)&mp_module_collections },
130+
#if MICROPY_ENABLE_MOD_STRUCT
131+
{ MP_OBJ_NEW_QSTR(MP_QSTR_struct), (mp_obj_t)&mp_module_struct },
132+
#endif
130133

131134
#if MICROPY_ENABLE_FLOAT
132135
{ MP_OBJ_NEW_QSTR(MP_QSTR_math), (mp_obj_t)&mp_module_math },

py/modstruct.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include <assert.h>
2+
#include <string.h>
3+
#include "misc.h"
4+
#include "mpconfig.h"
5+
#include "qstr.h"
6+
#include "obj.h"
7+
#include "builtin.h"
8+
#include "objtuple.h"
9+
#include "binary.h"
10+
11+
#if MICROPY_ENABLE_MOD_STRUCT
12+
13+
STATIC char get_fmt_type(const char **fmt) {
14+
char t = **fmt;
15+
switch (t) {
16+
case '!':
17+
t = '>';
18+
break;
19+
case '@':
20+
case '=':
21+
case '<':
22+
case '>':
23+
break;
24+
default:
25+
return '@';
26+
}
27+
// Skip type char
28+
(*fmt)++;
29+
return t;
30+
}
31+
32+
STATIC uint calcsize_items(const char *fmt) {
33+
// TODO
34+
return strlen(fmt);
35+
}
36+
37+
STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
38+
const char *fmt = mp_obj_str_get_str(fmt_in);
39+
char fmt_type = get_fmt_type(&fmt);
40+
assert(fmt_type == '<'); (void)fmt_type;
41+
uint size;
42+
for (size = 0; *fmt; fmt++) {
43+
int sz = mp_binary_get_size(*fmt);
44+
// TODO
45+
assert(sz != -1);
46+
size += sz;
47+
}
48+
return MP_OBJ_NEW_SMALL_INT(size);
49+
}
50+
MP_DEFINE_CONST_FUN_OBJ_1(struct_calcsize_obj, struct_calcsize);
51+
52+
STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
53+
// TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
54+
const char *fmt = mp_obj_str_get_str(fmt_in);
55+
char fmt_type = get_fmt_type(&fmt);
56+
assert(fmt_type == '<'); (void)fmt_type;
57+
uint size = calcsize_items(fmt);
58+
mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
59+
buffer_info_t bufinfo;
60+
mp_get_buffer_raise(data_in, &bufinfo);
61+
byte *p = bufinfo.buf;
62+
63+
for (uint i = 0; i < size; i++) {
64+
mp_obj_t item = mp_binary_get_val_unaligned_le(*fmt++, &p);
65+
res->items[i] = item;
66+
}
67+
return res;
68+
}
69+
MP_DEFINE_CONST_FUN_OBJ_2(struct_unpack_obj, struct_unpack);
70+
71+
STATIC const mp_map_elem_t mp_module_struct_globals_table[] = {
72+
{ MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_struct) },
73+
{ MP_OBJ_NEW_QSTR(MP_QSTR_calcsize), (mp_obj_t)&struct_calcsize_obj },
74+
{ MP_OBJ_NEW_QSTR(MP_QSTR_unpack), (mp_obj_t)&struct_unpack_obj },
75+
};
76+
77+
STATIC const mp_obj_dict_t mp_module_struct_globals = {
78+
.base = {&mp_type_dict},
79+
.map = {
80+
.all_keys_are_qstrs = 1,
81+
.table_is_fixed_array = 1,
82+
.used = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
83+
.alloc = sizeof(mp_module_struct_globals_table) / sizeof(mp_map_elem_t),
84+
.table = (mp_map_elem_t*)mp_module_struct_globals_table,
85+
},
86+
};
87+
88+
const mp_obj_module_t mp_module_struct = {
89+
.base = { &mp_type_module },
90+
.name = MP_QSTR_struct,
91+
.globals = (mp_obj_dict_t*)&mp_module_struct_globals,
92+
};
93+
94+
#endif

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ typedef double mp_float_t;
115115
#define MICROPY_ENABLE_MOD_IO (1)
116116
#endif
117117

118+
// Whether to provide "struct" module
119+
#ifndef MICROPY_ENABLE_MOD_STRUCT
120+
#define MICROPY_ENABLE_MOD_STRUCT (1)
121+
#endif
122+
118123
// Whether to support slice object and correspondingly
119124
// slice subscript operators
120125
#ifndef MICROPY_ENABLE_SLICE

py/py.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ PY_O_BASENAME = \
8181
modio.o \
8282
modmath.o \
8383
modmicropython.o \
84+
modstruct.o \
8485
vm.o \
8586
showbc.o \
8687
repl.o \

py/qstrdefs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ Q(bool)
8484
Q(bytearray)
8585
Q(bytes)
8686
Q(callable)
87+
#if MICROPY_ENABLE_MOD_STRUCT
88+
Q(calcsize)
89+
#endif
8790
Q(chr)
8891
Q(classmethod)
8992
Q(collections)
@@ -127,10 +130,16 @@ Q(staticmethod)
127130
Q(sum)
128131
Q(super)
129132
Q(str)
133+
#if MICROPY_ENABLE_MOD_STRUCT
134+
Q(struct)
135+
#endif
130136
Q(sys)
131137
Q(to_bytes)
132138
Q(tuple)
133139
Q(type)
140+
#if MICROPY_ENABLE_MOD_STRUCT
141+
Q(unpack)
142+
#endif
134143
Q(value)
135144
Q(zip)
136145

tests/basics/struct1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import struct
2+
print(struct.calcsize("<bI"))
3+
print(struct.unpack("<bI", b"\x80\0\0\x01\0"))

0 commit comments

Comments
 (0)