aboutsummaryrefslogtreecommitdiff
path: root/py/warning.c
diff options
context:
space:
mode:
authorPaul Sokolovsky2018-12-21 14:20:55 +0300
committerDamien George2019-01-31 16:48:30 +1100
commit2f5d113fad4ca2b22b084ccaf835c595cd6a7a4e (patch)
tree9d3eb975a624b96c0352ccbd715f0873b06a02db /py/warning.c
parent86f06d6a874d4eb3d6c50deec0240942344c01ea (diff)
py/warning: Support categories for warnings.
Python defines warnings as belonging to categories, where category is a warning type (descending from exception type). This is useful, as e.g. allows to disable warnings selectively and provide user-defined warning types. So, implement this in MicroPython, except that categories are represented just with strings. However, enough hooks are left to implement categories differently per-port (e.g. as types), without need to patch each and every usage.
Diffstat (limited to 'py/warning.c')
-rw-r--r--py/warning.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/py/warning.c b/py/warning.c
index 12d0f9c99..ebaf2d078 100644
--- a/py/warning.c
+++ b/py/warning.c
@@ -32,10 +32,15 @@
#if MICROPY_WARNINGS
-void mp_warning(const char *msg, ...) {
+void mp_warning(const char *category, const char *msg, ...) {
+ if (category == NULL) {
+ category = "Warning";
+ }
+ mp_print_str(MICROPY_ERROR_PRINTER, category);
+ mp_print_str(MICROPY_ERROR_PRINTER, ": ");
+
va_list args;
va_start(args, msg);
- mp_print_str(MICROPY_ERROR_PRINTER, "Warning: ");
mp_vprintf(MICROPY_ERROR_PRINTER, msg, args);
mp_print_str(MICROPY_ERROR_PRINTER, "\n");
va_end(args);
@@ -43,7 +48,7 @@ void mp_warning(const char *msg, ...) {
void mp_emitter_warning(pass_kind_t pass, const char *msg) {
if (pass == MP_PASS_CODE_SIZE) {
- mp_warning(msg, NULL);
+ mp_warning(NULL, msg);
}
}