Skip to content

Commit 57a4b4f

Browse files
committed
py: Add typecode to buffer protocol.
When querying an object that supports the buffer protocol, that object must now return a typecode (as per binary.[ch]). This does not have to be honoured by the caller, but can be useful for determining element size.
1 parent 3fd2d7f commit 57a4b4f

File tree

7 files changed

+24
-25
lines changed

7 files changed

+24
-25
lines changed

py/modstruct.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
5555
char fmt_type = get_fmt_type(&fmt);
5656
uint size = calcsize_items(fmt);
5757
mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
58-
buffer_info_t bufinfo;
58+
mp_buffer_info_t bufinfo;
5959
mp_get_buffer_raise(data_in, &bufinfo);
6060
byte *p = bufinfo.buf;
6161

py/obj.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,20 +357,20 @@ mp_obj_t mp_identity(mp_obj_t self) {
357357
}
358358
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
359359

360-
bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo) {
360+
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo) {
361361
mp_obj_type_t *type = mp_obj_get_type(obj);
362362
if (type->buffer_p.get_buffer == NULL) {
363363
return false;
364364
}
365-
type->buffer_p.get_buffer(obj, bufinfo, BUFFER_READ);
366-
if (bufinfo->buf == NULL) {
365+
int ret = type->buffer_p.get_buffer(obj, bufinfo, MP_BUFFER_READ);
366+
if (ret != 0 || bufinfo->buf == NULL) {
367367
return false;
368368
}
369369
return true;
370370
}
371371

372-
void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo) {
372+
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo) {
373373
if (!mp_get_buffer(obj, bufinfo)) {
374-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Object with buffer protocol required"));
374+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "object with buffer protocol required"));
375375
}
376376
}

py/obj.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,32 +189,28 @@ typedef struct _mp_method_t {
189189
} mp_method_t;
190190

191191
// Buffer protocol
192-
typedef struct _buffer_info_t {
192+
typedef struct _mp_buffer_info_t {
193193
// if we'd bother to support various versions of structure
194194
// (with different number of fields), we can distinguish
195195
// them with ver = sizeof(struct). Cons: overkill for *micro*?
196196
//int ver; // ?
197197

198198
void *buf;
199-
machine_int_t len;
200-
201-
// Rationale: have array.array and have SIMD operations on them
202-
// Cons: users can pass item size to processing functions themselves,
203-
// though that's not "plug&play"
204-
// int itemsize;
199+
machine_int_t len; // in bytes
200+
int typecode; // as per binary.h
205201

206202
// Rationale: to load arbitrary-sized sprites directly to LCD
207203
// Cons: a bit adhoc usecase
208204
// int stride;
209-
} buffer_info_t;
210-
#define BUFFER_READ (1)
211-
#define BUFFER_WRITE (2)
212-
#define BUFFER_RW (BUFFER_READ | BUFFER_WRITE)
205+
} mp_buffer_info_t;
206+
#define MP_BUFFER_READ (1)
207+
#define MP_BUFFER_WRITE (2)
208+
#define MP_BUFFER_RW (MP_BUFFER_READ | MP_BUFFER_WRITE)
213209
typedef struct _mp_buffer_p_t {
214-
machine_int_t (*get_buffer)(mp_obj_t obj, buffer_info_t *bufinfo, int flags);
210+
machine_int_t (*get_buffer)(mp_obj_t obj, mp_buffer_info_t *bufinfo, int flags);
215211
} mp_buffer_p_t;
216-
bool mp_get_buffer(mp_obj_t obj, buffer_info_t *bufinfo);
217-
void mp_get_buffer_raise(mp_obj_t obj, buffer_info_t *bufinfo);
212+
bool mp_get_buffer(mp_obj_t obj, mp_buffer_info_t *bufinfo);
213+
void mp_get_buffer_raise(mp_obj_t obj, mp_buffer_info_t *bufinfo);
218214

219215
// Stream protocol
220216
typedef struct _mp_stream_p_t {

py/objarray.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,11 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
151151
}
152152
}
153153

154-
STATIC machine_int_t array_get_buffer(mp_obj_t o_in, buffer_info_t *bufinfo, int flags) {
154+
STATIC machine_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, int flags) {
155155
mp_obj_array_t *o = o_in;
156156
bufinfo->buf = o->items;
157157
bufinfo->len = o->len * mp_binary_get_size(o->typecode);
158+
bufinfo->typecode = o->typecode;
158159
return 0;
159160
}
160161

py/objfun.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ STATIC machine_uint_t convert_obj_for_inline_asm(mp_obj_t obj) {
426426
mp_obj_list_get(obj, &len, &items);
427427
return (machine_uint_t)items;
428428
} else {
429-
buffer_info_t bufinfo;
429+
mp_buffer_info_t bufinfo;
430430
if (mp_get_buffer(obj, &bufinfo)) {
431431
// supports the buffer protocol, return a pointer to the data
432432
return (machine_uint_t)bufinfo.buf;

py/objint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ STATIC mp_obj_t int_from_bytes(uint n_args, const mp_obj_t *args) {
272272
// TODO: Support signed param (assumes signed=False at the moment)
273273

274274
// get the buffer info
275-
buffer_info_t bufinfo;
275+
mp_buffer_info_t bufinfo;
276276
mp_get_buffer_raise(args[1], &bufinfo);
277277

278278
// convert the bytes to an integer

py/objstr.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,16 +1326,18 @@ STATIC mp_obj_t str_encode(uint n_args, const mp_obj_t *args) {
13261326
}
13271327
#endif
13281328

1329-
STATIC machine_int_t str_get_buffer(mp_obj_t self_in, buffer_info_t *bufinfo, int flags) {
1330-
if (flags == BUFFER_READ) {
1329+
STATIC machine_int_t str_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, int flags) {
1330+
if (flags == MP_BUFFER_READ) {
13311331
GET_STR_DATA_LEN(self_in, str_data, str_len);
13321332
bufinfo->buf = (void*)str_data;
13331333
bufinfo->len = str_len;
1334+
bufinfo->typecode = 'b';
13341335
return 0;
13351336
} else {
13361337
// can't write to a string
13371338
bufinfo->buf = NULL;
13381339
bufinfo->len = 0;
1340+
bufinfo->typecode = -1;
13391341
return 1;
13401342
}
13411343
}

0 commit comments

Comments
 (0)