Skip to content

Commit 393d0c1

Browse files
committed
extmod/moductypes: Implement buffer protocol.
This is required to write structures to files, pass to FFI functions, etc.
1 parent 79f404a commit 393d0c1

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

extmod/moductypes.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,18 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
556556
}
557557
}
558558

559+
STATIC mp_int_t uctypes_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
560+
(void)flags;
561+
mp_obj_uctypes_struct_t *self = self_in;
562+
mp_uint_t max_field_size = 0;
563+
mp_uint_t size = uctypes_struct_size(self->desc, &max_field_size);
564+
565+
bufinfo->buf = self->addr;
566+
bufinfo->len = size;
567+
bufinfo->typecode = BYTEARRAY_TYPECODE;
568+
return 0;
569+
}
570+
559571
/// \function addressof()
560572
/// Return address of object's data (applies to object providing buffer
561573
/// interface).
@@ -592,6 +604,7 @@ STATIC const mp_obj_type_t uctypes_struct_type = {
592604
.make_new = uctypes_struct_make_new,
593605
.attr = uctypes_struct_attr,
594606
.subscr = uctypes_struct_subscr,
607+
.buffer_p = { .get_buffer = uctypes_get_buffer },
595608
};
596609

597610
STATIC const mp_map_elem_t mp_module_uctypes_globals_table[] = {

0 commit comments

Comments
 (0)