Skip to content

Commit 62849b7

Browse files
dlechdpgeorge
authored andcommitted
py: Add config option to print warnings/errors to stderr.
This adds a new configuration option to print runtime warnings and errors to stderr. On Unix, CPython prints warnings and unhandled exceptions to stderr, so the unix port here is configured to use this option. The unix port already printed unhandled exceptions on the main thread to stderr. This patch fixes unhandled exceptions on other threads and warnings (issue adafruit#2838) not printing on stderr. Additionally, a couple tests needed to be fixed to handle this new behavior. This is done by also capturing stderr when running tests.
1 parent 9d836fe commit 62849b7

5 files changed

Lines changed: 17 additions & 9 deletions

File tree

ports/unix/mpconfigport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,11 @@
145145
// names in exception messages (may require more RAM).
146146
#define MICROPY_ERROR_REPORTING (MICROPY_ERROR_REPORTING_DETAILED)
147147
#define MICROPY_WARNINGS (1)
148+
#define MICROPY_ERROR_PRINTER (&mp_stderr_print)
148149
#define MICROPY_PY_STR_BYTES_CMP_WARN (1)
149150

151+
extern const struct _mp_print_t mp_stderr_print;
152+
150153
// Define to 1 to use undertested inefficient GC helper implementation
151154
// (if more efficient arch-specific one is not available).
152155
#ifndef MICROPY_GCREGS_SETJMP

py/modthread.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ STATIC void *thread_entry(void *args_in) {
192192
// swallow exception silently
193193
} else {
194194
// print exception out
195-
mp_printf(&mp_plat_print, "Unhandled exception in thread started by ");
196-
mp_obj_print_helper(&mp_plat_print, args->fun, PRINT_REPR);
197-
mp_printf(&mp_plat_print, "\n");
198-
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(exc));
195+
mp_printf(MICROPY_ERROR_PRINTER, "Unhandled exception in thread started by ");
196+
mp_obj_print_helper(MICROPY_ERROR_PRINTER, args->fun, PRINT_REPR);
197+
mp_printf(MICROPY_ERROR_PRINTER, "\n");
198+
mp_obj_print_exception(MICROPY_ERROR_PRINTER, MP_OBJ_FROM_PTR(exc));
199199
}
200200
}
201201

py/mpconfig.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,11 @@ typedef long long mp_longint_impl_t;
533533
#define MICROPY_WARNINGS (0)
534534
#endif
535535

536+
// This macro is used when printing runtime warnings and errors
537+
#ifndef MICROPY_ERROR_PRINTER
538+
#define MICROPY_ERROR_PRINTER (&mp_plat_print)
539+
#endif
540+
536541
// Float and complex implementation
537542
#define MICROPY_FLOAT_IMPL_NONE (0)
538543
#define MICROPY_FLOAT_IMPL_FLOAT (1)

py/warning.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@
3535
void mp_warning(const char *msg, ...) {
3636
va_list args;
3737
va_start(args, msg);
38-
mp_print_str(&mp_plat_print, "Warning: ");
39-
mp_vprintf(&mp_plat_print, msg, args);
40-
mp_print_str(&mp_plat_print, "\n");
38+
mp_print_str(MICROPY_ERROR_PRINTER, "Warning: ");
39+
mp_vprintf(MICROPY_ERROR_PRINTER, msg, args);
40+
mp_print_str(MICROPY_ERROR_PRINTER, "\n");
4141
va_end(args);
4242
}
4343

tests/run-tests

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def run_micropython(pyb, args, test_file, is_special=False):
103103
os.close(master)
104104
os.close(slave)
105105
else:
106-
output_mupy = subprocess.check_output(args + [test_file])
106+
output_mupy = subprocess.check_output(args + [test_file], stderr=subprocess.STDOUT)
107107
except subprocess.CalledProcessError:
108108
return b'CRASH'
109109

@@ -124,7 +124,7 @@ def run_micropython(pyb, args, test_file, is_special=False):
124124

125125
# run the actual test
126126
try:
127-
output_mupy = subprocess.check_output(cmdlist)
127+
output_mupy = subprocess.check_output(cmdlist, stderr=subprocess.STDOUT)
128128
except subprocess.CalledProcessError:
129129
output_mupy = b'CRASH'
130130

0 commit comments

Comments
 (0)