diff options
| author | Jeff Epler <jepler@unpythonic.net> | 2018-03-26 18:13:49 -0500 |
|---|---|---|
| committer | Jeff Epler <jepler@unpythonic.net> | 2018-03-26 18:13:49 -0500 |
| commit | 355bf8b5538b4fe2f9f07b0d8fb2f4500c2f96c6 (patch) | |
| tree | e14a249dee6038a3873715797aee82e92e9b3bec /py | |
| parent | fa884466793738338edb7389dc6b21f160bba37b (diff) | |
Conditionally compile out nonstandard array/struct typecodes
.. defaulting to off for circuitpython-supported boards, on for others.
.. fixing up the tests that fail when it is turned off, so that they skip
instead of failing
Diffstat (limited to 'py')
| -rw-r--r-- | py/binary.c | 16 | ||||
| -rw-r--r-- | py/mpconfig.h | 6 | ||||
| -rw-r--r-- | py/objarray.c | 2 |
3 files changed, 23 insertions, 1 deletions
diff --git a/py/binary.c b/py/binary.c index 62cb384f1..bb334ed3d 100644 --- a/py/binary.c +++ b/py/binary.c @@ -57,8 +57,10 @@ size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) { size = 4; break; case 'q': case 'Q': size = 8; break; +#if MICROPY_NONSTANDARD_TYPECODES case 'P': case 'O': case 'S': size = sizeof(void*); break; +#endif case 'f': size = sizeof(float); break; case 'd': @@ -89,9 +91,11 @@ size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) { case 'q': case 'Q': align = alignof(long long); size = sizeof(long long); break; +#if MICROPY_NONSTANDARD_TYPECODES case 'P': case 'O': case 'S': align = alignof(void*); size = sizeof(void*); break; +#endif case 'f': align = alignof(float); size = sizeof(float); break; @@ -148,12 +152,14 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) { case 'd': return mp_obj_new_float(((double*)p)[index]); #endif +#if MICROPY_NONSTANDARD_TYPECODES // Extension to CPython: array of objects case 'O': return ((mp_obj_t*)p)[index]; // Extension to CPython: array of pointers case 'P': return mp_obj_new_int((mp_int_t)(uintptr_t)((void**)p)[index]); +#endif } return MP_OBJ_NEW_SMALL_INT(val); } @@ -202,11 +208,13 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) { long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p); - if (val_type == 'O') { + if (MICROPY_NONSTANDARD_TYPECODES && (val_type == 'O')) { return (mp_obj_t)(mp_uint_t)val; +#if MICROPY_NONSTANDARD_TYPECODES } else if (val_type == 'S') { const char *s_val = (const char*)(uintptr_t)(mp_uint_t)val; return mp_obj_new_str(s_val, strlen(s_val), false); +#endif #if MICROPY_PY_BUILTINS_FLOAT } else if (val_type == 'f') { union { uint32_t i; float f; } fpu = {val}; @@ -267,9 +275,11 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte ** mp_uint_t val; switch (val_type) { +#if MICROPY_NONSTANDARD_TYPECODES case 'O': val = (mp_uint_t)val_in; break; +#endif #if MICROPY_PY_BUILTINS_FLOAT case 'f': { union { uint32_t i; float f; } fp_sp; @@ -324,10 +334,12 @@ void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t v ((double*)p)[index] = mp_obj_get_float(val_in); break; #endif +#if MICROPY_NONSTANDARD_TYPECODES // Extension to CPython: array of objects case 'O': ((mp_obj_t*)p)[index] = val_in; break; +#endif default: #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) { @@ -384,9 +396,11 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m ((double*)p)[index] = val; break; #endif +#if MICROPY_NONSTANDARD_TYPECODES // Extension to CPython: array of pointers case 'P': ((void**)p)[index] = (void*)(uintptr_t)val; break; +#endif } } diff --git a/py/mpconfig.h b/py/mpconfig.h index 252ab7f37..765ae1e8f 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -888,6 +888,12 @@ typedef double mp_float_t; #define MICROPY_PY_ARRAY_SLICE_ASSIGN (0) #endif +// Whether to support nonstandard typecodes "O", "P" and "S" +// in array and struct modules. +#ifndef MICROPY_NONSTANDARD_TYPECODES +#define MICROPY_NONSTANDARD_TYPECODES (1) +#endif + // Whether to support attrtuple type (MicroPython extension) // It provides space-efficient tuples with attribute access #ifndef MICROPY_PY_ATTRTUPLE diff --git a/py/objarray.c b/py/objarray.c index eb053bd8a..c4c547e19 100644 --- a/py/objarray.c +++ b/py/objarray.c @@ -470,9 +470,11 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value } else { mp_seq_replace_slice_no_grow(dest_items, o->len, slice.start, slice.stop, src_items, src_len, item_sz); +#if MICROPY_NONSTANDARD_TYPECODES // Clear "freed" elements at the end of list // TODO: This is actually only needed for typecode=='O' mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz); +#endif // TODO: alloc policy after shrinking } o->len += len_adj; |
