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>
This commit is contained in:
Damien George
2021-04-22 12:13:58 +10:00
parent 30d9f77cc5
commit d4b706c4d0
16 changed files with 117 additions and 67 deletions

View File

@@ -189,7 +189,7 @@ mp_obj_t mp_load_global(qstr qst) {
#endif
elem = mp_map_lookup((mp_map_t *)&mp_module_builtins_globals.map, MP_OBJ_NEW_QSTR(qst), MP_MAP_LOOKUP);
if (elem == NULL) {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_NameError, MP_ERROR_TEXT("name not defined"));
#else
mp_raise_msg_varg(&mp_type_NameError, MP_ERROR_TEXT("name '%q' isn't defined"), qst);
@@ -289,7 +289,7 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
}
// With MP_UNARY_OP_INT, mp_unary_op() becomes a fallback for mp_obj_get_int().
// In this case provide a more focused error message to not confuse, e.g. chr(1.0)
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
if (op == MP_UNARY_OP_INT) {
mp_raise_TypeError(MP_ERROR_TEXT("can't convert to int"));
} else {
@@ -610,7 +610,7 @@ generic_binary_op:
}
unsupported_op:
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(MP_ERROR_TEXT("unsupported type for operator"));
#else
mp_raise_msg_varg(&mp_type_TypeError,
@@ -652,7 +652,7 @@ mp_obj_t mp_call_function_n_kw(mp_obj_t fun_in, size_t n_args, size_t n_kw, cons
return type->call(fun_in, n_args, n_kw, args);
}
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(MP_ERROR_TEXT("object not callable"));
#else
mp_raise_msg_varg(&mp_type_TypeError,
@@ -881,13 +881,13 @@ void mp_unpack_sequence(mp_obj_t seq_in, size_t num, mp_obj_t *items) {
return;
too_short:
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
#else
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len);
#endif
too_long:
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
#else
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("too many values to unpack (expected %d)"), (int)num);
@@ -948,7 +948,7 @@ void mp_unpack_ex(mp_obj_t seq_in, size_t num_in, mp_obj_t *items) {
return;
too_short:
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_ValueError(MP_ERROR_TEXT("wrong number of values to unpack"));
#else
mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("need more than %d values to unpack"), (int)seq_len);
@@ -1117,7 +1117,7 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
if (dest[0] == MP_OBJ_NULL) {
// no attribute/method called attr
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute"));
#else
// following CPython, we give a more detailed error message for type objects
@@ -1161,7 +1161,7 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
return;
}
}
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_msg(&mp_type_AttributeError, MP_ERROR_TEXT("no such attribute"));
#else
mp_raise_msg_varg(&mp_type_AttributeError,
@@ -1206,7 +1206,7 @@ mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
}
// object not iterable
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(MP_ERROR_TEXT("object not iterable"));
#else
mp_raise_msg_varg(&mp_type_TypeError,
@@ -1229,7 +1229,7 @@ mp_obj_t mp_iternext_allow_raise(mp_obj_t o_in) {
// __next__ exists, call it and return its result
return mp_call_method_n_kw(0, 0, dest);
} else {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator"));
#else
mp_raise_msg_varg(&mp_type_TypeError,
@@ -1265,7 +1265,7 @@ mp_obj_t mp_iternext(mp_obj_t o_in) {
}
}
} else {
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
#if MICROPY_ERROR_REPORTING <= MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(MP_ERROR_TEXT("object not an iterator"));
#else
mp_raise_msg_varg(&mp_type_TypeError,
@@ -1512,6 +1512,26 @@ NORETURN void m_malloc_fail(size_t num_bytes) {
MP_ERROR_TEXT("memory allocation failed, allocating %u bytes"), (uint)num_bytes);
}
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NONE
NORETURN void mp_raise_type(const mp_obj_type_t *exc_type) {
nlr_raise(mp_obj_new_exception(exc_type));
}
NORETURN void mp_raise_ValueError_no_msg(void) {
mp_raise_type(&mp_type_ValueError);
}
NORETURN void mp_raise_TypeError_no_msg(void) {
mp_raise_type(&mp_type_TypeError);
}
NORETURN void mp_raise_NotImplementedError_no_msg(void) {
mp_raise_type(&mp_type_NotImplementedError);
}
#else
NORETURN void mp_raise_msg(const mp_obj_type_t *exc_type, mp_rom_error_text_t msg) {
if (msg == NULL) {
nlr_raise(mp_obj_new_exception(exc_type));
@@ -1536,14 +1556,16 @@ NORETURN void mp_raise_TypeError(mp_rom_error_text_t msg) {
mp_raise_msg(&mp_type_TypeError, msg);
}
NORETURN void mp_raise_OSError(int errno_) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_)));
}
NORETURN void mp_raise_NotImplementedError(mp_rom_error_text_t msg) {
mp_raise_msg(&mp_type_NotImplementedError, msg);
}
#endif
NORETURN void mp_raise_OSError(int errno_) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(errno_)));
}
#if MICROPY_STACK_CHECK || MICROPY_ENABLE_PYSTACK
NORETURN void mp_raise_recursion_depth(void) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_RuntimeError,