Skip to content

Commit 1f85d62

Browse files
committed
py: Add tentative scheme for error messages configuration.
1 parent 68551a8 commit 1f85d62

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

py/mpconfig.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ typedef long long mp_longint_impl_t;
9494
#define MICROPY_ENABLE_DOC_STRING (0)
9595
#endif
9696

97+
// Exception messages are short static strings (TODO)
98+
#define MICROPY_ERROR_REPORTING_TERSE (1)
99+
// Exception messages provide basic error details
100+
#define MICROPY_ERROR_REPORTING_NORMAL (2)
101+
// Exception messages provide full info, e.g. object names
102+
#define MICROPY_ERROR_REPORTING_DETAILED (3)
103+
104+
#ifndef MICROPY_ERROR_REPORTING
105+
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_NORMAL)
106+
#endif
107+
97108
// Float and complex implementation
98109
#define MICROPY_FLOAT_IMPL_NONE (0)
99110
#define MICROPY_FLOAT_IMPL_FLOAT (1)

py/objfun.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,18 @@ STATIC void dump_args(const mp_obj_t *a, int sz) {
152152
#endif
153153

154154
STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, uint given) {
155+
#if MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_TERSE
156+
// Generic message, to be reused for other argument issues
157+
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError,
158+
"argument num/types mismatch"));
159+
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_NORMAL
155160
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
156161
"function takes %d positional arguments but %d were given", expected, given));
162+
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
163+
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
164+
"%s() takes %d positional arguments but %d were given",
165+
mp_obj_fun_get_name(f), expected, given));
166+
#endif
157167
}
158168

159169
// If it's possible to call a function without allocating new argument array,

unix/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#define MICROPY_USE_COMPUTED_GOTO (1)
1717
#define MICROPY_MOD_SYS_STDFILES (1)
1818
#define MICROPY_ENABLE_MOD_CMATH (1)
19+
// Define to MICROPY_ERROR_REPORTING_DETAILED to get function, etc.
20+
// names in exception messages (may require more RAM).
21+
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
1922

2023
extern const struct _mp_obj_module_t mp_module_time;
2124
extern const struct _mp_obj_module_t mp_module_socket;

0 commit comments

Comments
 (0)