Skip to content

Commit

Permalink
Merge pull request micropython#1555 from tannewt/print_flush
Browse files Browse the repository at this point in the history
Support print("", flush=True)
  • Loading branch information
dhalbert committed Feb 16, 2019
2 parents 11ef64e + e6b140e commit 619b4f1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
20 changes: 17 additions & 3 deletions lib/utils/sys_stdio_mphal.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ STATIC const sys_stdio_obj_t stdio_buffer_obj;
#endif

void stdio_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
sys_stdio_obj_t *self = self_in;
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "<io.FileIO %d>", self->fd);
}

STATIC mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
sys_stdio_obj_t *self = self_in;
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (self->fd == STDIO_FD_IN) {
for (uint i = 0; i < size; i++) {
int c = mp_hal_stdin_rx_chr();
Expand All @@ -75,7 +75,7 @@ STATIC mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *er
}

STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
sys_stdio_obj_t *self = self_in;
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
mp_hal_stdout_tx_strn_cooked(buf, size);
return size;
Expand All @@ -85,6 +85,19 @@ STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size,
}
}

STATIC mp_uint_t stdio_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
(void) self;

// For now, pretend we actually flush the stdio stream.
if (request == MP_STREAM_FLUSH) {
return 0;
} else {
*errcode = MP_EINVAL;
return MP_STREAM_ERROR;
}
}

STATIC mp_obj_t stdio_obj___exit__(size_t n_args, const mp_obj_t *args) {
return mp_const_none;
}
Expand Down Expand Up @@ -112,6 +125,7 @@ STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table);
STATIC const mp_stream_p_t stdio_obj_stream_p = {
.read = stdio_read,
.write = stdio_write,
.ioctl = stdio_ioctl,
.is_text = true,
};

Expand Down
6 changes: 5 additions & 1 deletion py/modbuiltins.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,10 +371,11 @@ STATIC mp_obj_t mp_builtin_pow(size_t n_args, const mp_obj_t *args) {
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);

STATIC mp_obj_t mp_builtin_print(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_sep, ARG_end, ARG_file };
enum { ARG_sep, ARG_end, ARG_flush, ARG_file };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_sep, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR__space_)} },
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR__0x0a_)} },
{ MP_QSTR_flush, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
{ MP_QSTR_file, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_sys_stdout_obj)} },
#endif
Expand Down Expand Up @@ -414,6 +415,9 @@ STATIC mp_obj_t mp_builtin_print(size_t n_args, const mp_obj_t *pos_args, mp_map
}
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
mp_stream_write_adaptor(print.data, end_data, u.len[1]);
if (u.args[ARG_flush].u_bool) {
mp_stream_flush(MP_OBJ_FROM_PTR(print.data));
}
#else
mp_print_strn(&mp_plat_print, end_data, u.len[1], 0, 0, 0);
#endif
Expand Down
7 changes: 5 additions & 2 deletions py/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,16 +468,19 @@ STATIC mp_obj_t stream_tell(mp_obj_t self) {
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);

STATIC mp_obj_t stream_flush(mp_obj_t self) {
mp_obj_t mp_stream_flush(mp_obj_t self) {
const mp_stream_p_t *stream_p = mp_get_stream(self);
int error;
if (stream_p->ioctl == NULL) {
mp_raise_OSError(MP_EINVAL);
}
mp_uint_t res = stream_p->ioctl(self, MP_STREAM_FLUSH, 0, &error);
if (res == MP_STREAM_ERROR) {
mp_raise_OSError(error);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, stream_flush);
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, mp_stream_flush);

STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
mp_buffer_info_t bufinfo;
Expand Down
1 change: 1 addition & 0 deletions py/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf, mp_uint_t size, int *errcode,
#define mp_stream_read_exactly(stream, buf, size, err) mp_stream_rw(stream, buf, size, err, MP_STREAM_RW_READ)

void mp_stream_write_adaptor(void *self, const char *buf, size_t len);
mp_obj_t mp_stream_flush(mp_obj_t self);

#if MICROPY_STREAMS_POSIX_API
// Functions with POSIX-compatible signatures
Expand Down

0 comments on commit 619b4f1

Please sign in to comment.