objmemoryview: Introduce mp_obj_new_memoryview().

This follows existing pattern for object constructor API and allows to
create memoryview objects e.g. in external modules.
This commit is contained in:
Paul Sokolovsky 2015-03-06 21:35:26 +02:00
parent 24c1000741
commit cb0fc063ed
2 changed files with 15 additions and 6 deletions

View File

@ -430,6 +430,7 @@ mp_obj_t mp_obj_new_super(mp_obj_t type, mp_obj_t obj);
mp_obj_t mp_obj_new_bound_meth(mp_obj_t meth, mp_obj_t self);
mp_obj_t mp_obj_new_getitem_iter(mp_obj_t *args);
mp_obj_t mp_obj_new_module(qstr module_name);
mp_obj_t mp_obj_new_memoryview(byte typecode, mp_uint_t nitems, void *items);
mp_obj_type_t *mp_obj_get_type(mp_const_obj_t o_in);
const char *mp_obj_get_type_str(mp_const_obj_t o_in);

View File

@ -208,6 +208,17 @@ STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
#endif
#if MICROPY_PY_BUILTINS_MEMORYVIEW
mp_obj_t mp_obj_new_memoryview(byte typecode, mp_uint_t nitems, void *items) {
mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
self->base.type = &mp_type_memoryview;
self->typecode = typecode;
self->free = 0;
self->len = nitems;
self->items = items;
return self;
}
STATIC mp_obj_t memoryview_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
// TODO possibly allow memoryview constructor to take start/stop so that one
// can do memoryview(b, 4, 8) instead of memoryview(b)[4:8] (uses less RAM)
@ -217,12 +228,9 @@ STATIC mp_obj_t memoryview_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(args[0], &bufinfo, MP_BUFFER_READ);
mp_obj_array_t *self = m_new_obj(mp_obj_array_t);
self->base.type = type_in;
self->typecode = bufinfo.typecode;
self->free = 0;
self->len = bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL); // element len
self->items = bufinfo.buf;
mp_obj_array_t *self = mp_obj_new_memoryview(bufinfo.typecode,
bufinfo.len / mp_binary_get_size('@', bufinfo.typecode, NULL),
bufinfo.buf);
// test if the object can be written to
if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {