py: Use shorter, static error msgs when ERROR_REPORTING_TERSE enabled.
Going from MICROPY_ERROR_REPORTING_NORMAL to
MICROPY_ERROR_REPORTING_TERSE now saves 2020 bytes ROM for ARM Thumb2,
and 2200 bytes ROM for 32-bit x86.
This is about a 2.5% code size reduction for bare-arm.
diff --git a/py/runtime.c b/py/runtime.c
index e225ba8..c0ae472 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -165,7 +165,13 @@
// TODO lookup in dynamic table of builtins first
elem = mp_map_lookup((mp_map_t*)&mp_builtin_object_dict_obj.map, MP_OBJ_NEW_QSTR(qstr), MP_MAP_LOOKUP);
if (elem == NULL) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError, "name '%s' is not defined", qstr_str(qstr)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_NameError,
+ "name not defined"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError,
+ "name '%s' is not defined", qstr_str(qstr)));
+ }
}
}
return elem->value;
@@ -231,8 +237,15 @@
return result;
}
}
- // TODO specify in error message what the operator is
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "bad operand type for unary operator: '%s'", mp_obj_get_type_str(arg)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "unsupported type for operator"));
+ } else {
+ // TODO specify in error message what the operator is
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "bad operand type for unary operator: '%s'",
+ mp_obj_get_type_str(arg)));
+ }
}
}
@@ -496,10 +509,13 @@
return mp_const_false;
}
- nlr_raise(mp_obj_new_exception_msg_varg(
- &mp_type_TypeError, "'%s' object is not iterable",
- mp_obj_get_type_str(rhs)));
- return mp_const_none;
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "object not iterable"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "'%s' object is not iterable", mp_obj_get_type_str(rhs)));
+ }
}
// generic binary_op supplied by type
@@ -515,12 +531,16 @@
// TODO implement dispatch for reverse binary ops
- // TODO specify in error message what the operator is
unsupported_op:
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
- "unsupported operand types for binary operator: '%s', '%s'",
- mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
- return mp_const_none;
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "unsupported type for operator"));
+ } else {
+ // TODO specify in error message what the operator is
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "unsupported operand types for binary operator: '%s', '%s'",
+ mp_obj_get_type_str(lhs), mp_obj_get_type_str(rhs)));
+ }
zero_division:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ZeroDivisionError, "division by zero"));
@@ -556,7 +576,13 @@
return type->call(fun_in, n_args, n_kw, args);
}
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "object not callable"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "'%s' object is not callable", mp_obj_get_type_str(fun_in)));
+ }
}
// 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)
@@ -746,9 +772,21 @@
return;
too_short:
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
+ "wrong number of values to unpack"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+ "need more than %d values to unpack", seq_len));
+ }
too_long:
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "too many values to unpack (expected %d)", num));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
+ "wrong number of values to unpack"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+ "too many values to unpack (expected %d)", num));
+ }
}
// unpacked items are stored in reverse order into the array pointed to by items
@@ -809,7 +847,13 @@
return;
too_short:
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "need more than %d values to unpack", seq_len));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError,
+ "wrong number of values to unpack"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+ "need more than %d values to unpack", seq_len));
+ }
}
mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
@@ -891,12 +935,20 @@
if (dest[0] == MP_OBJ_NULL) {
// no attribute/method called attr
- // following CPython, we give a more detailed error message for type objects
- if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
- "type object '%s' has no attribute '%s'", qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError,
+ "no such attribute"));
} else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
+ // following CPython, we give a more detailed error message for type objects
+ if (MP_OBJ_IS_TYPE(base, &mp_type_type)) {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
+ "type object '%s' has no attribute '%s'",
+ qstr_str(((mp_obj_type_t*)base)->name), qstr_str(attr)));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
+ "'%s' object has no attribute '%s'",
+ mp_obj_get_type_str(base), qstr_str(attr)));
+ }
}
}
}
@@ -909,7 +961,14 @@
return;
}
}
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_AttributeError,
+ "no such attribute"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
+ "'%s' object has no attribute '%s'",
+ mp_obj_get_type_str(base), qstr_str(attr)));
+ }
}
mp_obj_t mp_getiter(mp_obj_t o_in) {
@@ -936,7 +995,13 @@
} else {
// object not iterable
not_iterable:
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "object not iterable"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "'%s' object is not iterable", mp_obj_get_type_str(o_in)));
+ }
}
}
}
@@ -956,7 +1021,13 @@
// __next__ exists, call it and return its result
return mp_call_method_n_kw(0, 0, dest);
} else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "object not an iterator"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
+ }
}
}
}
@@ -986,7 +1057,13 @@
}
}
} else {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
+ if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
+ "object not an iterator"));
+ } else {
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
+ "'%s' object is not an iterator", mp_obj_get_type_str(o_in)));
+ }
}
}
}