From 393d0c16799a4eda787886bc0c7fce0da3c4adb8 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 26 Oct 2015 01:02:27 +0300 Subject: [PATCH] extmod/moductypes: Implement buffer protocol. This is required to write structures to files, pass to FFI functions, etc. --- extmod/moductypes.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/extmod/moductypes.c b/extmod/moductypes.c index 56da809311..21d6f451a8 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -556,6 +556,18 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob } } +STATIC mp_int_t uctypes_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) { + (void)flags; + mp_obj_uctypes_struct_t *self = self_in; + mp_uint_t max_field_size = 0; + mp_uint_t size = uctypes_struct_size(self->desc, &max_field_size); + + bufinfo->buf = self->addr; + bufinfo->len = size; + bufinfo->typecode = BYTEARRAY_TYPECODE; + return 0; +} + /// \function addressof() /// Return address of object's data (applies to object providing buffer /// interface). @@ -592,6 +604,7 @@ STATIC const mp_obj_type_t uctypes_struct_type = { .make_new = uctypes_struct_make_new, .attr = uctypes_struct_attr, .subscr = uctypes_struct_subscr, + .buffer_p = { .get_buffer = uctypes_get_buffer }, }; STATIC const mp_map_elem_t mp_module_uctypes_globals_table[] = {