Skip to content

Commit f898a95

Browse files
committed
unix modffi: Support any object implementing buffer protocol as a native arg.
1 parent 7a70a3a commit f898a95

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

unix/modffi.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,21 +253,32 @@ mp_obj_t ffifunc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *
253253
values[i] = 0;
254254
} else if (MP_OBJ_IS_INT(a)) {
255255
values[i] = mp_obj_int_get(a);
256-
} else if (MP_OBJ_IS_STR(a) || MP_OBJ_IS_TYPE(a, &mp_type_bytes)) {
256+
} else if (MP_OBJ_IS_STR(a)) {
257257
const char *s = mp_obj_str_get_str(a);
258258
values[i] = (ffi_arg)s;
259+
} else if (((mp_obj_base_t*)a)->type->buffer_p.get_buffer != NULL) {
260+
mp_obj_base_t *o = (mp_obj_base_t*)a;
261+
buffer_info_t bufinfo;
262+
o->type->buffer_p.get_buffer(o, &bufinfo, BUFFER_READ); // TODO: BUFFER_READ?
263+
if (bufinfo.buf == NULL) {
264+
goto error;
265+
}
266+
values[i] = (ffi_arg)bufinfo.buf;
259267
} else if (MP_OBJ_IS_TYPE(a, &fficallback_type)) {
260268
mp_obj_fficallback_t *p = a;
261269
values[i] = (ffi_arg)p->func;
262270
} else {
263-
assert(0);
271+
goto error;
264272
}
265273
valueptrs[i] = &values[i];
266274
}
267275

268276
ffi_arg retval;
269277
ffi_call(&self->cif, self->func, &retval, valueptrs);
270278
return return_ffi_value(retval, self->rettype);
279+
280+
error:
281+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Don't know how to pass object to native function"));
271282
}
272283

273284
STATIC const mp_obj_type_t ffifunc_type = {

0 commit comments

Comments
 (0)