py: Add builtin memoryview object (mostly using array code).
This commit is contained in:
parent
3aa09f5784
commit
dd4f4530ab
|
@ -13,6 +13,7 @@
|
||||||
#define MICROPY_HELPER_LEXER_UNIX (0)
|
#define MICROPY_HELPER_LEXER_UNIX (0)
|
||||||
#define MICROPY_ENABLE_SOURCE_LINE (0)
|
#define MICROPY_ENABLE_SOURCE_LINE (0)
|
||||||
#define MICROPY_PY_BUILTINS_BYTEARRAY (0)
|
#define MICROPY_PY_BUILTINS_BYTEARRAY (0)
|
||||||
|
#define MICROPY_PY_BUILTINS_MEMORYVIEW (0)
|
||||||
#define MICROPY_PY_BUILTINS_FROZENSET (0)
|
#define MICROPY_PY_BUILTINS_FROZENSET (0)
|
||||||
#define MICROPY_PY_BUILTINS_SET (0)
|
#define MICROPY_PY_BUILTINS_SET (0)
|
||||||
#define MICROPY_PY_BUILTINS_SLICE (0)
|
#define MICROPY_PY_BUILTINS_SLICE (0)
|
||||||
|
|
|
@ -61,6 +61,9 @@ STATIC const mp_map_elem_t mp_builtin_object_table[] = {
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_int), (mp_obj_t)&mp_type_int },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_list), (mp_obj_t)&mp_type_list },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_list), (mp_obj_t)&mp_type_list },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_map), (mp_obj_t)&mp_type_map },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_map), (mp_obj_t)&mp_type_map },
|
||||||
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_memoryview), (mp_obj_t)&mp_type_memoryview },
|
||||||
|
#endif
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_object), (mp_obj_t)&mp_type_object },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_object), (mp_obj_t)&mp_type_object },
|
||||||
#if MICROPY_PY_BUILTINS_PROPERTY
|
#if MICROPY_PY_BUILTINS_PROPERTY
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_property), (mp_obj_t)&mp_type_property },
|
||||||
|
|
|
@ -287,6 +287,11 @@ typedef double mp_float_t;
|
||||||
#define MICROPY_PY_BUILTINS_BYTEARRAY (1)
|
#define MICROPY_PY_BUILTINS_BYTEARRAY (1)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Whether to support memoryview object
|
||||||
|
#ifndef MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
#define MICROPY_PY_BUILTINS_MEMORYVIEW (0)
|
||||||
|
#endif
|
||||||
|
|
||||||
// Whether to support set object
|
// Whether to support set object
|
||||||
#ifndef MICROPY_PY_BUILTINS_SET
|
#ifndef MICROPY_PY_BUILTINS_SET
|
||||||
#define MICROPY_PY_BUILTINS_SET (1)
|
#define MICROPY_PY_BUILTINS_SET (1)
|
||||||
|
|
2
py/obj.h
2
py/obj.h
|
@ -292,6 +292,7 @@ extern const mp_obj_type_t mp_type_int;
|
||||||
extern const mp_obj_type_t mp_type_str;
|
extern const mp_obj_type_t mp_type_str;
|
||||||
extern const mp_obj_type_t mp_type_bytes;
|
extern const mp_obj_type_t mp_type_bytes;
|
||||||
extern const mp_obj_type_t mp_type_bytearray;
|
extern const mp_obj_type_t mp_type_bytearray;
|
||||||
|
extern const mp_obj_type_t mp_type_memoryview;
|
||||||
extern const mp_obj_type_t mp_type_float;
|
extern const mp_obj_type_t mp_type_float;
|
||||||
extern const mp_obj_type_t mp_type_complex;
|
extern const mp_obj_type_t mp_type_complex;
|
||||||
extern const mp_obj_type_t mp_type_tuple;
|
extern const mp_obj_type_t mp_type_tuple;
|
||||||
|
@ -528,7 +529,6 @@ void mp_obj_set_store(mp_obj_t self_in, mp_obj_t item);
|
||||||
void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step);
|
void mp_obj_slice_get(mp_obj_t self_in, mp_obj_t *start, mp_obj_t *stop, mp_obj_t *step);
|
||||||
|
|
||||||
// array
|
// array
|
||||||
mp_uint_t mp_obj_array_len(mp_obj_t self_in);
|
|
||||||
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items);
|
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items);
|
||||||
|
|
||||||
// functions
|
// functions
|
||||||
|
|
132
py/objarray.c
132
py/objarray.c
|
@ -37,26 +37,27 @@
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
#include "binary.h"
|
#include "binary.h"
|
||||||
|
|
||||||
#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY
|
#if MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
|
||||||
typedef struct _mp_obj_array_t {
|
typedef struct _mp_obj_array_t {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
mp_uint_t typecode : 8;
|
mp_uint_t typecode : 8;
|
||||||
// free is number of unused elements after len used elements
|
// free is number of unused elements after len used elements
|
||||||
// alloc size = len + free
|
// alloc size = len + free
|
||||||
|
// for memoryview, free=0 is read-only, free=1 is read-write
|
||||||
mp_uint_t free : (8 * sizeof(mp_uint_t) - 8);
|
mp_uint_t free : (8 * sizeof(mp_uint_t) - 8);
|
||||||
mp_uint_t len; // in elements
|
mp_uint_t len; // in elements
|
||||||
void *items;
|
void *items;
|
||||||
} mp_obj_array_t;
|
} mp_obj_array_t;
|
||||||
|
|
||||||
STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
|
STATIC mp_obj_t array_iterator_new(mp_obj_t array_in);
|
||||||
STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n);
|
|
||||||
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
|
STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg);
|
||||||
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
|
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags);
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* array */
|
// array
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
|
||||||
STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) {
|
||||||
mp_obj_array_t *o = o_in;
|
mp_obj_array_t *o = o_in;
|
||||||
if (o->typecode == BYTEARRAY_TYPECODE) {
|
if (o->typecode == BYTEARRAY_TYPECODE) {
|
||||||
|
@ -77,7 +78,31 @@ STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *en
|
||||||
}
|
}
|
||||||
print(env, ")");
|
print(env, ")");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
|
||||||
|
STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
|
||||||
|
int typecode_size = mp_binary_get_size('@', typecode, NULL);
|
||||||
|
if (typecode_size <= 0) {
|
||||||
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
|
||||||
|
}
|
||||||
|
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY
|
||||||
|
o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
|
||||||
|
#elif MICROPY_PY_BUILTINS_BYTEARRAY
|
||||||
|
o->base.type = &mp_type_bytearray;
|
||||||
|
#else
|
||||||
|
o->base.type = &mp_type_array;
|
||||||
|
#endif
|
||||||
|
o->typecode = typecode;
|
||||||
|
o->free = 0;
|
||||||
|
o->len = n;
|
||||||
|
o->items = m_malloc(typecode_size * o->len);
|
||||||
|
return o;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
|
||||||
STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
|
STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
|
||||||
uint len;
|
uint len;
|
||||||
// Try to create array of exact len if initializer len is known
|
// Try to create array of exact len if initializer len is known
|
||||||
|
@ -103,7 +128,9 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
|
||||||
|
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MICROPY_PY_ARRAY
|
||||||
STATIC mp_obj_t array_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
STATIC mp_obj_t array_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||||
mp_arg_check_num(n_args, n_kw, 1, 2, false);
|
mp_arg_check_num(n_args, n_kw, 1, 2, false);
|
||||||
|
|
||||||
|
@ -119,7 +146,9 @@ STATIC mp_obj_t array_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
|
||||||
return array_construct(*typecode, args[1]);
|
return array_construct(*typecode, args[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY
|
||||||
STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
|
||||||
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
mp_arg_check_num(n_args, n_kw, 0, 1, false);
|
||||||
|
|
||||||
|
@ -137,6 +166,33 @@ STATIC mp_obj_t bytearray_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t
|
||||||
return array_construct(BYTEARRAY_TYPECODE, args[0]);
|
return array_construct(BYTEARRAY_TYPECODE, args[0]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
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)
|
||||||
|
|
||||||
|
mp_arg_check_num(n_args, n_kw, 1, 1, false);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// test if the object can be written to
|
||||||
|
if (mp_get_buffer(args[0], &bufinfo, MP_BUFFER_RW)) {
|
||||||
|
self->free = 1; // used to indicate writable buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
|
STATIC mp_obj_t array_unary_op(mp_uint_t op, mp_obj_t o_in) {
|
||||||
mp_obj_array_t *o = o_in;
|
mp_obj_array_t *o = o_in;
|
||||||
|
@ -202,11 +258,22 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_NotImplementedError,
|
||||||
"only slices with step=1 (aka None) are supported"));
|
"only slices with step=1 (aka None) are supported"));
|
||||||
}
|
}
|
||||||
mp_obj_array_t *res = array_new(o->typecode, slice.stop - slice.start);
|
mp_obj_array_t *res;
|
||||||
int sz = mp_binary_get_size('@', o->typecode, NULL);
|
int sz = mp_binary_get_size('@', o->typecode, NULL);
|
||||||
assert(sz > 0);
|
assert(sz > 0);
|
||||||
byte *p = o->items;
|
if (0) {
|
||||||
memcpy(res->items, p + slice.start * sz, (slice.stop - slice.start) * sz);
|
// dummy
|
||||||
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
} else if (o->base.type == &mp_type_memoryview) {
|
||||||
|
res = m_new_obj(mp_obj_array_t);
|
||||||
|
*res = *o;
|
||||||
|
res->len = slice.stop - slice.start;
|
||||||
|
res->items += slice.start * sz;
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
res = array_new(o->typecode, slice.stop - slice.start);
|
||||||
|
memcpy(res->items, o->items + slice.start * sz, (slice.stop - slice.start) * sz);
|
||||||
|
}
|
||||||
return res;
|
return res;
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
|
@ -216,6 +283,12 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
|
||||||
return mp_binary_get_val_array(o->typecode, o->items, index);
|
return mp_binary_get_val_array(o->typecode, o->items, index);
|
||||||
} else {
|
} else {
|
||||||
// store
|
// store
|
||||||
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
if (o->base.type == &mp_type_memoryview && o->free == 0) {
|
||||||
|
// read-only memoryview
|
||||||
|
return MP_OBJ_NULL;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
mp_binary_set_val_array(o->typecode, o->items, index, value);
|
mp_binary_set_val_array(o->typecode, o->items, index, value);
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
|
@ -225,18 +298,27 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
|
||||||
|
|
||||||
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
STATIC mp_int_t array_get_buffer(mp_obj_t o_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
|
||||||
mp_obj_array_t *o = o_in;
|
mp_obj_array_t *o = o_in;
|
||||||
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
if (o->base.type == &mp_type_memoryview && o->free == 0 && (flags & MP_BUFFER_WRITE)) {
|
||||||
|
// read-only memoryview
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
bufinfo->buf = o->items;
|
bufinfo->buf = o->items;
|
||||||
bufinfo->len = o->len * mp_binary_get_size('@', o->typecode, NULL);
|
bufinfo->len = o->len * mp_binary_get_size('@', o->typecode, NULL);
|
||||||
bufinfo->typecode = o->typecode;
|
bufinfo->typecode = o->typecode;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_ARRAY
|
||||||
STATIC const mp_map_elem_t array_locals_dict_table[] = {
|
STATIC const mp_map_elem_t array_locals_dict_table[] = {
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_append), (mp_obj_t)&array_append_obj },
|
||||||
};
|
};
|
||||||
|
|
||||||
STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
|
STATIC MP_DEFINE_CONST_DICT(array_locals_dict, array_locals_dict_table);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MICROPY_PY_ARRAY
|
||||||
const mp_obj_type_t mp_type_array = {
|
const mp_obj_type_t mp_type_array = {
|
||||||
{ &mp_type_type },
|
{ &mp_type_type },
|
||||||
.name = MP_QSTR_array,
|
.name = MP_QSTR_array,
|
||||||
|
@ -249,7 +331,9 @@ const mp_obj_type_t mp_type_array = {
|
||||||
.buffer_p = { .get_buffer = array_get_buffer },
|
.buffer_p = { .get_buffer = array_get_buffer },
|
||||||
.locals_dict = (mp_obj_t)&array_locals_dict,
|
.locals_dict = (mp_obj_t)&array_locals_dict,
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY
|
||||||
const mp_obj_type_t mp_type_bytearray = {
|
const mp_obj_type_t mp_type_bytearray = {
|
||||||
{ &mp_type_type },
|
{ &mp_type_type },
|
||||||
.name = MP_QSTR_bytearray,
|
.name = MP_QSTR_bytearray,
|
||||||
|
@ -262,25 +346,28 @@ const mp_obj_type_t mp_type_bytearray = {
|
||||||
.buffer_p = { .get_buffer = array_get_buffer },
|
.buffer_p = { .get_buffer = array_get_buffer },
|
||||||
.locals_dict = (mp_obj_t)&array_locals_dict,
|
.locals_dict = (mp_obj_t)&array_locals_dict,
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
STATIC mp_obj_array_t *array_new(char typecode, mp_uint_t n) {
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
int typecode_size = mp_binary_get_size('@', typecode, NULL);
|
const mp_obj_type_t mp_type_memoryview = {
|
||||||
if (typecode_size <= 0) {
|
{ &mp_type_type },
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "bad typecode"));
|
.name = MP_QSTR_memoryview,
|
||||||
}
|
.make_new = memoryview_make_new,
|
||||||
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
|
.getiter = array_iterator_new,
|
||||||
o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;
|
.unary_op = array_unary_op,
|
||||||
o->typecode = typecode;
|
.binary_op = array_binary_op,
|
||||||
o->free = 0;
|
.subscr = array_subscr,
|
||||||
o->len = n;
|
.buffer_p = { .get_buffer = array_get_buffer },
|
||||||
o->items = m_malloc(typecode_size * o->len);
|
};
|
||||||
return o;
|
#endif
|
||||||
}
|
|
||||||
|
|
||||||
|
/* unused
|
||||||
mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
|
mp_uint_t mp_obj_array_len(mp_obj_t self_in) {
|
||||||
return ((mp_obj_array_t *)self_in)->len;
|
return ((mp_obj_array_t *)self_in)->len;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY
|
||||||
mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
|
mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
|
||||||
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
|
mp_obj_array_t *o = array_new(BYTEARRAY_TYPECODE, n);
|
||||||
memcpy(o->items, items, n);
|
memcpy(o->items, items, n);
|
||||||
|
@ -290,16 +377,17 @@ mp_obj_t mp_obj_new_bytearray(mp_uint_t n, void *items) {
|
||||||
// Create bytearray which references specified memory area
|
// Create bytearray which references specified memory area
|
||||||
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
|
mp_obj_t mp_obj_new_bytearray_by_ref(mp_uint_t n, void *items) {
|
||||||
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
|
mp_obj_array_t *o = m_new_obj(mp_obj_array_t);
|
||||||
o->base.type = &mp_type_array;
|
o->base.type = &mp_type_bytearray;
|
||||||
o->typecode = BYTEARRAY_TYPECODE;
|
o->typecode = BYTEARRAY_TYPECODE;
|
||||||
o->free = 0;
|
o->free = 0;
|
||||||
o->len = n;
|
o->len = n;
|
||||||
o->items = items;
|
o->items = items;
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
/* array iterator */
|
// array iterator
|
||||||
|
|
||||||
typedef struct _mp_obj_array_it_t {
|
typedef struct _mp_obj_array_it_t {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
|
@ -332,4 +420,4 @@ STATIC mp_obj_t array_iterator_new(mp_obj_t array_in) {
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY
|
#endif // MICROPY_PY_ARRAY || MICROPY_PY_BUILTINS_BYTEARRAY || MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
|
|
@ -134,11 +134,18 @@ Q(abs)
|
||||||
Q(all)
|
Q(all)
|
||||||
Q(any)
|
Q(any)
|
||||||
Q(args)
|
Q(args)
|
||||||
|
#if MICROPY_PY_ARRAY
|
||||||
Q(array)
|
Q(array)
|
||||||
|
#endif
|
||||||
Q(bin)
|
Q(bin)
|
||||||
Q({:#b})
|
Q({:#b})
|
||||||
Q(bool)
|
Q(bool)
|
||||||
|
#if MICROPY_PY_BUILTINS_BYTEARRAY
|
||||||
Q(bytearray)
|
Q(bytearray)
|
||||||
|
#endif
|
||||||
|
#if MICROPY_PY_BUILTINS_MEMORYVIEW
|
||||||
|
Q(memoryview)
|
||||||
|
#endif
|
||||||
Q(bytes)
|
Q(bytes)
|
||||||
Q(callable)
|
Q(callable)
|
||||||
#if MICROPY_PY_STRUCT
|
#if MICROPY_PY_STRUCT
|
||||||
|
|
|
@ -50,6 +50,7 @@
|
||||||
#define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
|
#define MICROPY_LFN_CODE_PAGE (437) /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
|
||||||
#define MICROPY_MODULE_WEAK_LINKS (1)
|
#define MICROPY_MODULE_WEAK_LINKS (1)
|
||||||
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
|
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
|
||||||
|
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
|
||||||
#define MICROPY_PY_BUILTINS_FROZENSET (1)
|
#define MICROPY_PY_BUILTINS_FROZENSET (1)
|
||||||
#define MICROPY_PY_SYS_EXIT (1)
|
#define MICROPY_PY_SYS_EXIT (1)
|
||||||
#define MICROPY_PY_SYS_STDFILES (1)
|
#define MICROPY_PY_SYS_STDFILES (1)
|
||||||
|
|
|
@ -0,0 +1,34 @@
|
||||||
|
# test memoryview
|
||||||
|
|
||||||
|
# test reading from bytes
|
||||||
|
b = b'1234'
|
||||||
|
m = memoryview(b)
|
||||||
|
print(len(m))
|
||||||
|
print(m[0], m[1], m[-1])
|
||||||
|
print(list(m))
|
||||||
|
|
||||||
|
# test writing to bytes
|
||||||
|
try:
|
||||||
|
m[0] = 1
|
||||||
|
except TypeError:
|
||||||
|
print("TypeError")
|
||||||
|
|
||||||
|
# test writing to bytearray
|
||||||
|
b = bytearray(b)
|
||||||
|
m = memoryview(b)
|
||||||
|
m[0] = 1
|
||||||
|
print(b)
|
||||||
|
print(list(m))
|
||||||
|
|
||||||
|
# test slice
|
||||||
|
m = memoryview(b'1234')
|
||||||
|
print(list(m[1:]))
|
||||||
|
print(list(m[1:-1]))
|
||||||
|
|
||||||
|
import array
|
||||||
|
a = array.array('i', [1, 2, 3, 4])
|
||||||
|
m = memoryview(a)
|
||||||
|
print(list(m))
|
||||||
|
print(list(m[1:-1]))
|
||||||
|
m[2] = 6
|
||||||
|
print(a)
|
|
@ -45,6 +45,7 @@
|
||||||
#define MICROPY_STREAMS_NON_BLOCK (1)
|
#define MICROPY_STREAMS_NON_BLOCK (1)
|
||||||
#define MICROPY_OPT_COMPUTED_GOTO (1)
|
#define MICROPY_OPT_COMPUTED_GOTO (1)
|
||||||
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
|
#define MICROPY_PY_BUILTINS_STR_UNICODE (1)
|
||||||
|
#define MICROPY_PY_BUILTINS_MEMORYVIEW (1)
|
||||||
#define MICROPY_PY_BUILTINS_FROZENSET (1)
|
#define MICROPY_PY_BUILTINS_FROZENSET (1)
|
||||||
#define MICROPY_PY_SYS_EXIT (1)
|
#define MICROPY_PY_SYS_EXIT (1)
|
||||||
#define MICROPY_PY_SYS_PLATFORM "linux"
|
#define MICROPY_PY_SYS_PLATFORM "linux"
|
||||||
|
|
Loading…
Reference in New Issue