summaryrefslogtreecommitdiff
path: root/py/runtime.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2020-11-19 16:18:52 -0600
committerJeff Epler <jepler@gmail.com>2020-11-19 16:18:52 -0600
commitb2b8520880e9d458adbd279a34b831f8d6b68d40 (patch)
tree8e29536c38518715bbeee0fbbebc6ef0c85889ea /py/runtime.c
parentc06fc8e02dc7c17e827e3fe736dc7226d7819452 (diff)
Always use preprocessor for MICROPY_ERROR_REPORTING
This ensures that only the translate("") alternative that will be used is seen after preprocessing. Improves the quality of the Huffman encoding and reduces binary size slightly. Also makes one "enhanced" error message only occur when ERROR_REPORTING_DETAILED: Instead of the word-for-word python3 error message "Type object has no attribute '%q'", the message will be "'type' object has no attribute '%q'". Also reduces binary size. (that's rolled into this commit as it was right next to a change to use the preprocessor for MICROPY_ERROR_REPORTING) Note that the odd semicolon after "value_error:" in parsenum.c is necessary due to a detail of the C grammar, in which a declaration cannot follow a label directly.
Diffstat (limited to 'py/runtime.c')
-rw-r--r--py/runtime.c76
1 files changed, 40 insertions, 36 deletions
diff --git a/py/runtime.c b/py/runtime.c
index 3745e16e3..a3acb954a 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -177,12 +177,12 @@ 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, translate("name not defined"));
- } else {
+ #else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_NameError,
translate("name '%q' is not defined"), qst));
- }
+ #endif
}
}
return elem->value;
@@ -275,13 +275,13 @@ mp_obj_t mp_unary_op(mp_unary_op_t op, mp_obj_t arg) {
return result;
}
}
- if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(translate("unsupported type for operator"));
- } else {
+ #else
mp_raise_TypeError_varg(
translate("unsupported type for %q: '%q'"),
mp_unary_op_method_name[op], mp_obj_get_type_qstr(arg));
- }
+ #endif
}
}
@@ -582,13 +582,13 @@ generic_binary_op:
}
unsupported_op:
- if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(translate("unsupported type for operator"));
- } else {
+ #else
mp_raise_TypeError_varg(
translate("unsupported types for %q: '%q', '%q'"),
mp_binary_op_method_name[op], mp_obj_get_type_qstr(lhs), mp_obj_get_type_qstr(rhs));
- }
+ #endif
zero_division:
mp_raise_msg(&mp_type_ZeroDivisionError, translate("division by zero"));
@@ -624,11 +624,11 @@ 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(translate("object not callable"));
- } else {
+ #else
mp_raise_TypeError_varg(translate("'%q' object is not callable"), mp_obj_get_type_qstr(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)
@@ -852,19 +852,19 @@ 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(translate("wrong number of values to unpack"));
- } else {
+ #else
mp_raise_ValueError_varg(translate("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(translate("wrong number of values to unpack"));
- } else {
+ #else
mp_raise_ValueError_varg(translate("too many values to unpack (expected %d)"),
(int)num);
- }
+ #endif
}
// unpacked items are stored in reverse order into the array pointed to by items
@@ -916,12 +916,12 @@ 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(translate("wrong number of values to unpack"));
- } else {
+ #else
mp_raise_ValueError_varg(translate("need more than %d values to unpack"),
(int)seq_len);
- }
+ #endif
}
mp_obj_t mp_load_attr(mp_obj_t base, qstr attr) {
@@ -1094,9 +1094,9 @@ 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_AttributeError(translate("no such attribute"));
- } else {
+ #elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
// 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,
@@ -1107,7 +1107,11 @@ void mp_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
translate("'%q' object has no attribute '%q'"),
mp_obj_get_type_qstr(base), attr));
}
- }
+ #else
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
+ translate("'%q' object has no attribute '%q'"),
+ mp_obj_get_type_qstr(base), attr));
+ #endif
}
}
@@ -1168,13 +1172,13 @@ void mp_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
}
#endif
}
- if (MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE) {
+ #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_AttributeError(translate("no such attribute"));
- } else {
+ #else
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_AttributeError,
translate("'%q' object cannot assign attribute '%q'"),
mp_obj_get_type_qstr(base), attr));
- }
+ #endif
}
mp_obj_t mp_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {
@@ -1209,12 +1213,12 @@ 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(translate("object not iterable"));
- } else {
+ #else
mp_raise_TypeError_varg(
translate("'%q' object is not iterable"), mp_obj_get_type_qstr(o_in));
- }
+ #endif
}
// may return MP_OBJ_STOP_ITERATION as an optimisation instead of raise StopIteration()
@@ -1231,12 +1235,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) {
+ #if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
mp_raise_TypeError(translate("object not an iterator"));
- } else {
+ #else
mp_raise_TypeError_varg(translate("'%q' object is not an iterator"),
mp_obj_get_type_qstr(o_in));
- }
+ #endif
}
}
}
@@ -1267,12 +1271,12 @@ 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(translate("object not an iterator"));
- } else {
+ #else
mp_raise_TypeError_varg(translate("'%q' object is not an iterator"),
mp_obj_get_type_qstr(o_in));
- }
+ #endif
}
}
}