Skip to content

Commit 0f836ef

Browse files
committed
modstruct: Add 'O' typecode for passing mp_obj_t.
Useful as callback data, etc.
1 parent 4e4fa94 commit 0f836ef

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

py/binary.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
4949
case 'q': case 'Q':
5050
// TODO: This is for x86
5151
align = sizeof(int); size = sizeof(long long); break;
52+
case 'P': case 'O':
53+
align = size = sizeof(void*); break;
5254
}
5355
}
5456
}
@@ -131,7 +133,9 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
131133
}
132134

133135
*ptr += size;
134-
if (is_signed(val_type)) {
136+
if (val_type == 'O') {
137+
return (mp_obj_t)val;
138+
} else if (is_signed(val_type)) {
135139
return mp_obj_new_int(val);
136140
} else {
137141
return mp_obj_new_int_from_uint(val);
@@ -156,8 +160,16 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
156160
#if MP_ENDIANNESS_BIG
157161
#error Not implemented
158162
#endif
159-
machine_int_t val = mp_obj_get_int(val_in);
163+
machine_int_t val;
160164
byte *in = (byte*)&val;
165+
switch (val_type) {
166+
case 'O':
167+
in = (byte*)&val_in;
168+
break;
169+
default:
170+
val = mp_obj_get_int(val_in);
171+
}
172+
161173
int in_delta, out_delta;
162174
uint val_sz = MIN(size, sizeof(val));
163175
if (struct_type == '>') {

0 commit comments

Comments
 (0)