Skip to content

Commit d4b706c

Browse files
committed
py: Add option to compile without any error messages at all.
This introduces a new option, MICROPY_ERROR_REPORTING_NONE, which completely disables all error messages. To be used in cases where MicroPython needs to fit in very limited systems. Signed-off-by: Damien George <damien@micropython.org>
1 parent 30d9f77 commit d4b706c

16 files changed

Lines changed: 117 additions & 67 deletions

py/argcheck.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) {
3838
size_t n_args_max = (sig >> 1) & 0xffff;
3939

4040
if (n_kw && !takes_kw) {
41-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
41+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
4242
mp_arg_error_terse_mismatch();
4343
#else
4444
mp_raise_TypeError(MP_ERROR_TEXT("function doesn't take keyword arguments"));
@@ -47,7 +47,7 @@ void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) {
4747

4848
if (n_args_min == n_args_max) {
4949
if (n_args != n_args_min) {
50-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
50+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
5151
mp_arg_error_terse_mismatch();
5252
#else
5353
mp_raise_msg_varg(&mp_type_TypeError,
@@ -57,15 +57,15 @@ void mp_arg_check_num_sig(size_t n_args, size_t n_kw, uint32_t sig) {
5757
}
5858
} else {
5959
if (n_args < n_args_min) {
60-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
60+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
6161
mp_arg_error_terse_mismatch();
6262
#else
6363
mp_raise_msg_varg(&mp_type_TypeError,
6464
MP_ERROR_TEXT("function missing %d required positional arguments"),
6565
n_args_min - n_args);
6666
#endif
6767
} else if (n_args > n_args_max) {
68-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
68+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
6969
mp_arg_error_terse_mismatch();
7070
#else
7171
mp_raise_msg_varg(&mp_type_TypeError,
@@ -90,7 +90,7 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n
9090
mp_map_elem_t *kw = mp_map_lookup(kws, MP_OBJ_NEW_QSTR(allowed[i].qst), MP_MAP_LOOKUP);
9191
if (kw == NULL) {
9292
if (allowed[i].flags & MP_ARG_REQUIRED) {
93-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
93+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
9494
mp_arg_error_terse_mismatch();
9595
#else
9696
mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("'%q' argument required"), allowed[i].qst);
@@ -114,15 +114,15 @@ void mp_arg_parse_all(size_t n_pos, const mp_obj_t *pos, mp_map_t *kws, size_t n
114114
}
115115
if (pos_found < n_pos) {
116116
extra_positional:
117-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
117+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
118118
mp_arg_error_terse_mismatch();
119119
#else
120120
// TODO better error message
121121
mp_raise_TypeError(MP_ERROR_TEXT("extra positional arguments given"));
122122
#endif
123123
}
124124
if (kws_found < kws->used) {
125-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
125+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
126126
mp_arg_error_terse_mismatch();
127127
#else
128128
// TODO better error message

py/bc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const byte *mp_decode_uint_skip(const byte *ptr) {
7575
#endif
7676

7777
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, size_t expected, size_t given) {
78-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
78+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
7979
// generic message, used also for other argument issues
8080
(void)f;
8181
(void)expected;
@@ -212,7 +212,7 @@ void mp_setup_code_state(mp_code_state_t *code_state, size_t n_args, size_t n_kw
212212
}
213213
// Didn't find name match with positional args
214214
if ((scope_flags & MP_SCOPE_FLAG_VARKEYWORDS) == 0) {
215-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
215+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
216216
mp_raise_TypeError(MP_ERROR_TEXT("unexpected keyword argument"));
217217
#else
218218
mp_raise_msg_varg(&mp_type_TypeError,

py/builtinimport.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
396396
#endif
397397
if (module_obj == MP_OBJ_NULL) {
398398
// couldn't find the file, so fail
399-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
399+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
400400
mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("module not found"));
401401
#else
402402
mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("no module named '%q'"), mod_name);
@@ -499,7 +499,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) {
499499
#endif
500500

501501
// Couldn't find the module, so fail
502-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
502+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
503503
mp_raise_msg(&mp_type_ImportError, MP_ERROR_TEXT("module not found"));
504504
#else
505505
mp_raise_msg_varg(&mp_type_ImportError, MP_ERROR_TEXT("no module named '%q'"), module_name_qstr);

py/compile.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2597,7 +2597,7 @@ STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t *
25972597
compile_node(comp, pn_i);
25982598
if (is_dict) {
25992599
if (!is_key_value) {
2600-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
2600+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
26012601
compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("invalid syntax"));
26022602
#else
26032603
compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("expecting key:value for dict"));
@@ -2607,7 +2607,7 @@ STATIC void compile_atom_brace_helper(compiler_t *comp, mp_parse_node_struct_t *
26072607
EMIT(store_map);
26082608
} else {
26092609
if (is_key_value) {
2610-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
2610+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
26112611
compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("invalid syntax"));
26122612
#else
26132613
compile_syntax_error(comp, (mp_parse_node_t)pns, MP_ERROR_TEXT("expecting just a value for set"));

py/misc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ typedef union _mp_float_union_t {
264264

265265
#if MICROPY_ROM_TEXT_COMPRESSION
266266

267+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
268+
#error "MICROPY_ERROR_REPORTING_NONE requires MICROPY_ROM_TEXT_COMPRESSION disabled"
269+
#endif
270+
267271
#ifdef NO_QSTR
268272

269273
// Compression enabled but doing QSTR extraction.

py/modbuiltins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ STATIC mp_obj_t mp_builtin_ord(mp_obj_t o_in) {
372372
}
373373
}
374374

375-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
375+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
376376
mp_raise_TypeError(MP_ERROR_TEXT("ord expects a character"));
377377
#else
378378
mp_raise_msg_varg(&mp_type_TypeError,

py/mpconfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,8 @@ typedef long long mp_longint_impl_t;
667667
#define MICROPY_ENABLE_DOC_STRING (0)
668668
#endif
669669

670+
// Exception messages are removed (requires disabling MICROPY_ROM_TEXT_COMPRESSION)
671+
#define MICROPY_ERROR_REPORTING_NONE (0)
670672
// Exception messages are short static strings
671673
#define MICROPY_ERROR_REPORTING_TERSE (1)
672674
// Exception messages provide basic error details

py/obj.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ mp_float_t mp_obj_get_float(mp_obj_t arg) {
359359
mp_float_t val;
360360

361361
if (!mp_obj_get_float_maybe(arg, &val)) {
362-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
362+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
363363
mp_raise_TypeError(MP_ERROR_TEXT("can't convert to float"));
364364
#else
365365
mp_raise_msg_varg(&mp_type_TypeError,
@@ -399,7 +399,7 @@ bool mp_obj_get_complex_maybe(mp_obj_t arg, mp_float_t *real, mp_float_t *imag)
399399

400400
void mp_obj_get_complex(mp_obj_t arg, mp_float_t *real, mp_float_t *imag) {
401401
if (!mp_obj_get_complex_maybe(arg, real, imag)) {
402-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
402+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
403403
mp_raise_TypeError(MP_ERROR_TEXT("can't convert to complex"));
404404
#else
405405
mp_raise_msg_varg(&mp_type_TypeError,
@@ -417,7 +417,7 @@ void mp_obj_get_array(mp_obj_t o, size_t *len, mp_obj_t **items) {
417417
} else if (mp_obj_is_type(o, &mp_type_list)) {
418418
mp_obj_list_get(o, len, items);
419419
} else {
420-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
420+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
421421
mp_raise_TypeError(MP_ERROR_TEXT("expected tuple/list"));
422422
#else
423423
mp_raise_msg_varg(&mp_type_TypeError,
@@ -431,7 +431,7 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, size_t len, mp_obj_t **items) {
431431
size_t seq_len;
432432
mp_obj_get_array(o, &seq_len, items);
433433
if (seq_len != len) {
434-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
434+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
435435
mp_raise_ValueError(MP_ERROR_TEXT("tuple/list has wrong length"));
436436
#else
437437
mp_raise_msg_varg(&mp_type_ValueError,
@@ -446,7 +446,7 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
446446
if (mp_obj_is_small_int(index)) {
447447
i = MP_OBJ_SMALL_INT_VALUE(index);
448448
} else if (!mp_obj_get_int_maybe(index, &i)) {
449-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
449+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
450450
mp_raise_TypeError(MP_ERROR_TEXT("indices must be integers"));
451451
#else
452452
mp_raise_msg_varg(&mp_type_TypeError,
@@ -466,7 +466,7 @@ size_t mp_get_index(const mp_obj_type_t *type, size_t len, mp_obj_t index, bool
466466
}
467467
} else {
468468
if (i < 0 || (mp_uint_t)i >= len) {
469-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
469+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
470470
mp_raise_msg(&mp_type_IndexError, MP_ERROR_TEXT("index out of range"));
471471
#else
472472
mp_raise_msg_varg(&mp_type_IndexError, MP_ERROR_TEXT("%q index out of range"), type->name);
@@ -500,7 +500,7 @@ mp_obj_t mp_obj_id(mp_obj_t o_in) {
500500
mp_obj_t mp_obj_len(mp_obj_t o_in) {
501501
mp_obj_t len = mp_obj_len_maybe(o_in);
502502
if (len == MP_OBJ_NULL) {
503-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
503+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
504504
mp_raise_TypeError(MP_ERROR_TEXT("object has no len"));
505505
#else
506506
mp_raise_msg_varg(&mp_type_TypeError,
@@ -541,21 +541,21 @@ mp_obj_t mp_obj_subscr(mp_obj_t base, mp_obj_t index, mp_obj_t value) {
541541
// TODO: call base classes here?
542542
}
543543
if (value == MP_OBJ_NULL) {
544-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
544+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
545545
mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item deletion"));
546546
#else
547547
mp_raise_msg_varg(&mp_type_TypeError,
548548
MP_ERROR_TEXT("'%s' object doesn't support item deletion"), mp_obj_get_type_str(base));
549549
#endif
550550
} else if (value == MP_OBJ_SENTINEL) {
551-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
551+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
552552
mp_raise_TypeError(MP_ERROR_TEXT("object isn't subscriptable"));
553553
#else
554554
mp_raise_msg_varg(&mp_type_TypeError,
555555
MP_ERROR_TEXT("'%s' object isn't subscriptable"), mp_obj_get_type_str(base));
556556
#endif
557557
} else {
558-
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
558+
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
559559
mp_raise_TypeError(MP_ERROR_TEXT("object doesn't support item assignment"));
560560
#else
561561
mp_raise_msg_varg(&mp_type_TypeError,

py/obj.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,13 @@ mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag);
741741
mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type);
742742
mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg);
743743
mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args, const mp_obj_t *args);
744+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
745+
#define mp_obj_new_exception_msg(exc_type, msg) mp_obj_new_exception(exc_type)
746+
#define mp_obj_new_exception_msg_varg(exc_type, ...) mp_obj_new_exception(exc_type)
747+
#else
744748
mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg);
745749
mp_obj_t mp_obj_new_exception_msg_varg(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, ...); // counts args by number of % symbols in fmt, excluding %%; can only handle void* sizes (ie no float/double!)
750+
#endif
746751
#ifdef va_start
747752
mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_error_text_t fmt, va_list arg); // same fmt restrictions as above
748753
#endif

py/objexcept.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ mp_obj_t mp_obj_new_exception_args(const mp_obj_type_t *exc_type, size_t n_args,
373373
return mp_obj_exception_make_new(exc_type, n_args, 0, args);
374374
}
375375

376+
#if MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_NONE
377+
376378
mp_obj_t mp_obj_new_exception_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg) {
377379
// Check that the given type is an exception type
378380
assert(exc_type->make_new == mp_obj_exception_make_new);
@@ -518,6 +520,8 @@ mp_obj_t mp_obj_new_exception_msg_vlist(const mp_obj_type_t *exc_type, mp_rom_er
518520
return mp_obj_exception_make_new(exc_type, 1, 0, &arg);
519521
}
520522

523+
#endif
524+
521525
// return true if the given object is an exception type
522526
bool mp_obj_is_exception_type(mp_obj_t self_in) {
523527
if (mp_obj_is_type(self_in, &mp_type_type)) {

0 commit comments

Comments
 (0)