Skip to content

Commit 582b190

Browse files
committed
py: Add checks for stream objects in print() and sys.print_exception().
1 parent 2c8d130 commit 582b190

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

py/modbuiltins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ STATIC mp_obj_t mp_builtin_print(size_t n_args, const mp_obj_t *pos_args, mp_map
386386
mp_arg_parse_all(0, NULL, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, u.args);
387387

388388
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
389-
// TODO file may not be a concrete object (eg it could be a small-int)
389+
mp_get_stream_raise(u.args[ARG_file].u_obj, MP_STREAM_OP_WRITE);
390390
mp_print_t print = {MP_OBJ_TO_PTR(u.args[ARG_file].u_obj), mp_stream_write_adaptor};
391391
#endif
392392

py/modsys.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ STATIC mp_obj_t mp_sys_print_exception(size_t n_args, const mp_obj_t *args) {
106106
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
107107
void *stream_obj = &mp_sys_stdout_obj;
108108
if (n_args > 1) {
109-
stream_obj = MP_OBJ_TO_PTR(args[1]); // XXX may fail
109+
mp_get_stream_raise(args[1], MP_STREAM_OP_WRITE);
110+
stream_obj = MP_OBJ_TO_PTR(args[1]);
110111
}
111112

112113
mp_print_t print = {stream_obj, mp_stream_write_adaptor};

0 commit comments

Comments
 (0)