mirror of
https://github.com/micropython/micropython.git
synced 2026-01-05 03:30:14 +01:00
py: Use preprocessor to detect error reporting level (terse/detailed).
Instead of compiler-level if-logic. This is necessary to know what error strings are included in the build at the preprocessor stage, so that string compression can be implemented.
This commit is contained in:
committed by
Damien George
parent
312c699491
commit
a9a745e4b4
181
py/runtime.c
181
py/runtime.c
@@ -185,11 +185,11 @@ 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) {
|
||||
mp_raise_msg(&mp_type_NameError, "name not defined");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_NameError, "name '%q' isn't defined", qst);
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_msg(&mp_type_NameError, "name not defined");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_NameError, "name '%q' isn't defined", qst);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return elem->value;
|
||||
@@ -285,22 +285,22 @@ 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 (op == MP_UNARY_OP_INT) {
|
||||
mp_raise_TypeError("can't convert to int");
|
||||
} else {
|
||||
mp_raise_TypeError("unsupported type for operator");
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
if (op == MP_UNARY_OP_INT) {
|
||||
mp_raise_TypeError("can't convert to int");
|
||||
} else {
|
||||
if (op == MP_UNARY_OP_INT) {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"can't convert %s to int", mp_obj_get_type_str(arg));
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"unsupported type for %q: '%s'",
|
||||
mp_unary_op_method_name[op], mp_obj_get_type_str(arg));
|
||||
}
|
||||
mp_raise_TypeError("unsupported type for operator");
|
||||
}
|
||||
#else
|
||||
if (op == MP_UNARY_OP_INT) {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"can't convert %s to int", mp_obj_get_type_str(arg));
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"unsupported type for %q: '%s'",
|
||||
mp_unary_op_method_name[op], mp_obj_get_type_str(arg));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -604,13 +604,13 @@ generic_binary_op:
|
||||
}
|
||||
|
||||
unsupported_op:
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
mp_raise_TypeError("unsupported type for operator");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"unsupported types for %q: '%s', '%s'",
|
||||
mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs));
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_TypeError("unsupported type for operator");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"unsupported types for %q: '%s', '%s'",
|
||||
mp_binary_op_method_name[op], mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs));
|
||||
#endif
|
||||
|
||||
zero_division:
|
||||
mp_raise_msg(&mp_type_ZeroDivisionError, "divide by zero");
|
||||
@@ -646,12 +646,12 @@ 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) {
|
||||
mp_raise_TypeError("object not callable");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't callable", mp_obj_get_type_str(fun_in));
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_TypeError("object not callable");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't callable", mp_obj_get_type_str(fun_in));
|
||||
#endif
|
||||
}
|
||||
|
||||
// args contains: fun self/NULL arg(0) ... arg(n_args-2) arg(n_args-1) kw_key(0) kw_val(0) ... kw_key(n_kw-1) kw_val(n_kw-1)
|
||||
@@ -875,17 +875,17 @@ 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) {
|
||||
mp_raise_ValueError("wrong number of values to unpack");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", (int)seq_len);
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_ValueError("wrong number of values to unpack");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", (int)seq_len);
|
||||
#endif
|
||||
too_long:
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
mp_raise_ValueError("wrong number of values to unpack");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", (int)num);
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_ValueError("wrong number of values to unpack");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", (int)num);
|
||||
#endif
|
||||
}
|
||||
|
||||
// unpacked items are stored in reverse order into the array pointed to by items
|
||||
@@ -942,11 +942,11 @@ 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) {
|
||||
mp_raise_ValueError("wrong number of values to unpack");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", (int)seq_len);
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_ValueError("wrong number of values to unpack");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", (int)seq_len);
|
||||
#endif
|
||||
}
|
||||
|
||||
mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
|
||||
@@ -980,12 +980,12 @@ STATIC mp_obj_t checked_fun_call(mp_obj_t self_in, size_t n_args, size_t n_kw, c
|
||||
if (n_args > 0) {
|
||||
const mp_obj_type_t *arg0_type = mp_obj_get_type(args[0]);
|
||||
if (arg0_type != self->type) {
|
||||
if (MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED) {
|
||||
mp_raise_TypeError("argument has wrong type");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"argument should be a '%q' not a '%q'", self->type->name, arg0_type->name);
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING != MICROPY_ERROR_REPORTING_DETAILED
|
||||
mp_raise_TypeError("argument has wrong type");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"argument should be a '%q' not a '%q'", self->type->name, arg0_type->name);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
return mp_call_function_n_kw(self->fun, n_args, n_kw, args);
|
||||
@@ -1105,20 +1105,20 @@ 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) {
|
||||
mp_raise_msg(&mp_type_AttributeError, "no such attribute");
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_msg(&mp_type_AttributeError, "no such attribute");
|
||||
#else
|
||||
// following CPython, we give a more detailed error message for type objects
|
||||
if (mp_obj_is_type(base, &mp_type_type)) {
|
||||
mp_raise_msg_varg(&mp_type_AttributeError,
|
||||
"type object '%q' has no attribute '%q'",
|
||||
((mp_obj_type_t *)MP_OBJ_TO_PTR(base))->name, attr);
|
||||
} else {
|
||||
// following CPython, we give a more detailed error message for type objects
|
||||
if (mp_obj_is_type(base, &mp_type_type)) {
|
||||
mp_raise_msg_varg(&mp_type_AttributeError,
|
||||
"type object '%q' has no attribute '%q'",
|
||||
((mp_obj_type_t *)MP_OBJ_TO_PTR(base))->name, attr);
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_AttributeError,
|
||||
"'%s' object has no attribute '%q'",
|
||||
mp_obj_get_type_str(base), attr);
|
||||
}
|
||||
mp_raise_msg_varg(&mp_type_AttributeError,
|
||||
"'%s' object has no attribute '%q'",
|
||||
mp_obj_get_type_str(base), attr);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1149,13 +1149,13 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
mp_raise_msg(&mp_type_AttributeError, "no such attribute");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_AttributeError,
|
||||
"'%s' object has no attribute '%q'",
|
||||
mp_obj_get_type_str(base), attr);
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_msg(&mp_type_AttributeError, "no such attribute");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_AttributeError,
|
||||
"'%s' object has no attribute '%q'",
|
||||
mp_obj_get_type_str(base), attr);
|
||||
#endif
|
||||
}
|
||||
|
||||
mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
|
||||
@@ -1194,12 +1194,13 @@ 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) {
|
||||
mp_raise_TypeError("object not iterable");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't iterable", mp_obj_get_type_str(o_in));
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_TypeError("object not iterable");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't iterable", mp_obj_get_type_str(o_in));
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
|
||||
@@ -1216,12 +1217,12 @@ 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) {
|
||||
mp_raise_TypeError("object not an iterator");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't an iterator", mp_obj_get_type_str(o_in));
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_TypeError("object not an iterator");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't an iterator", mp_obj_get_type_str(o_in));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1252,12 +1253,12 @@ mp_obj_t mp_iternext(mp_obj_t o_in) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
|
||||
mp_raise_TypeError("object not an iterator");
|
||||
} else {
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't an iterator", mp_obj_get_type_str(o_in));
|
||||
}
|
||||
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
|
||||
mp_raise_TypeError("object not an iterator");
|
||||
#else
|
||||
mp_raise_msg_varg(&mp_type_TypeError,
|
||||
"'%s' object isn't an iterator", mp_obj_get_type_str(o_in));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user