Skip to content

Commit 355bf8b

Browse files
committed
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
1 parent fa88446 commit 355bf8b

File tree

9 files changed

+40
-1
lines changed

9 files changed

+40
-1
lines changed

ports/atmel-samd/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define MICROPY_PY_BUILTINS_SLICE (1)
4747
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (1)
4848
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
49+
#define MICROPY_NONSTANDARD_TYPECODES (0)
4950
#define MICROPY_PY_BUILTINS_PROPERTY (1)
5051
#define MICROPY_PY_BUILTINS_MIN_MAX (1)
5152
#define MICROPY_PY___FILE__ (1)

ports/esp8266/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#define MICROPY_PY_GC (1)
5050
#define MICROPY_PY_ARRAY (1)
5151
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
52+
#define MICROPY_NONSTANDARD_TYPECODES (0)
5253
#define MICROPY_PY_COLLECTIONS (1)
5354
#define MICROPY_PY_COLLECTIONS_ORDEREDDICT (1)
5455
#define MICROPY_PY_MATH (0)

ports/nrf/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
#define MICROPY_PY_ALL_SPECIAL_METHODS (0)
100100
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
101101
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (0)
102+
#define MICROPY_NONSTANDARD_TYPECODES (0)
102103
#define MICROPY_PY_BUILTINS_SLICE_ATTRS (0)
103104
#define MICROPY_PY_SYS_EXIT (1)
104105
#define MICROPY_PY_SYS_MAXSIZE (1)

py/binary.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,10 @@ size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
5757
size = 4; break;
5858
case 'q': case 'Q':
5959
size = 8; break;
60+
#if MICROPY_NONSTANDARD_TYPECODES
6061
case 'P': case 'O': case 'S':
6162
size = sizeof(void*); break;
63+
#endif
6264
case 'f':
6365
size = sizeof(float); break;
6466
case 'd':
@@ -89,9 +91,11 @@ size_t mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign) {
8991
case 'q': case 'Q':
9092
align = alignof(long long);
9193
size = sizeof(long long); break;
94+
#if MICROPY_NONSTANDARD_TYPECODES
9295
case 'P': case 'O': case 'S':
9396
align = alignof(void*);
9497
size = sizeof(void*); break;
98+
#endif
9599
case 'f':
96100
align = alignof(float);
97101
size = sizeof(float); break;
@@ -148,12 +152,14 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
148152
case 'd':
149153
return mp_obj_new_float(((double*)p)[index]);
150154
#endif
155+
#if MICROPY_NONSTANDARD_TYPECODES
151156
// Extension to CPython: array of objects
152157
case 'O':
153158
return ((mp_obj_t*)p)[index];
154159
// Extension to CPython: array of pointers
155160
case 'P':
156161
return mp_obj_new_int((mp_int_t)(uintptr_t)((void**)p)[index]);
162+
#endif
157163
}
158164
return MP_OBJ_NEW_SMALL_INT(val);
159165
}
@@ -202,11 +208,13 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
202208

203209
long long val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
204210

205-
if (val_type == 'O') {
211+
if (MICROPY_NONSTANDARD_TYPECODES && (val_type == 'O')) {
206212
return (mp_obj_t)(mp_uint_t)val;
213+
#if MICROPY_NONSTANDARD_TYPECODES
207214
} else if (val_type == 'S') {
208215
const char *s_val = (const char*)(uintptr_t)(mp_uint_t)val;
209216
return mp_obj_new_str(s_val, strlen(s_val), false);
217+
#endif
210218
#if MICROPY_PY_BUILTINS_FLOAT
211219
} else if (val_type == 'f') {
212220
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 **
267275

268276
mp_uint_t val;
269277
switch (val_type) {
278+
#if MICROPY_NONSTANDARD_TYPECODES
270279
case 'O':
271280
val = (mp_uint_t)val_in;
272281
break;
282+
#endif
273283
#if MICROPY_PY_BUILTINS_FLOAT
274284
case 'f': {
275285
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
324334
((double*)p)[index] = mp_obj_get_float(val_in);
325335
break;
326336
#endif
337+
#if MICROPY_NONSTANDARD_TYPECODES
327338
// Extension to CPython: array of objects
328339
case 'O':
329340
((mp_obj_t*)p)[index] = val_in;
330341
break;
342+
#endif
331343
default:
332344
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
333345
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
384396
((double*)p)[index] = val;
385397
break;
386398
#endif
399+
#if MICROPY_NONSTANDARD_TYPECODES
387400
// Extension to CPython: array of pointers
388401
case 'P':
389402
((void**)p)[index] = (void*)(uintptr_t)val;
390403
break;
404+
#endif
391405
}
392406
}

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,12 @@ typedef double mp_float_t;
888888
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (0)
889889
#endif
890890

891+
// Whether to support nonstandard typecodes "O", "P" and "S"
892+
// in array and struct modules.
893+
#ifndef MICROPY_NONSTANDARD_TYPECODES
894+
#define MICROPY_NONSTANDARD_TYPECODES (1)
895+
#endif
896+
891897
// Whether to support attrtuple type (MicroPython extension)
892898
// It provides space-efficient tuples with attribute access
893899
#ifndef MICROPY_PY_ATTRTUPLE

py/objarray.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,9 +470,11 @@ STATIC mp_obj_t array_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
470470
} else {
471471
mp_seq_replace_slice_no_grow(dest_items, o->len,
472472
slice.start, slice.stop, src_items, src_len, item_sz);
473+
#if MICROPY_NONSTANDARD_TYPECODES
473474
// Clear "freed" elements at the end of list
474475
// TODO: This is actually only needed for typecode=='O'
475476
mp_seq_clear(dest_items, o->len + len_adj, o->len, item_sz);
477+
#endif
476478
// TODO: alloc policy after shrinking
477479
}
478480
o->len += len_adj;

shared-module/struct/__init__.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@
3333
#include "py/parsenum.h"
3434

3535
void struct_validate_format(char fmt) {
36+
#if MICROPY_NONSTANDARD_TYPECODES
3637
if( fmt == 'S' || fmt == 'O') {
3738
mp_raise_RuntimeError("'S' and 'O' are not supported format types");
3839
}
40+
#endif
3941
}
4042

4143
char get_fmt_type(const char **fmt) {

tests/basics/array_micropython.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
print("SKIP")
66
raise SystemExit
77

8+
try:
9+
array.array('O')
10+
except ValueError:
11+
print("SKIP")
12+
raise SystemExit
13+
814
# arrays of objects
915
a = array.array('O')
1016
a.append(1)

tests/basics/struct_micropython.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
print("SKIP")
1010
raise SystemExit
1111

12+
try:
13+
struct.pack('O', None)
14+
except ValueError:
15+
print("SKIP")
16+
raise SystemExit
17+
1218
class A():
1319
pass
1420

0 commit comments

Comments
 (0)