From e6b140e7a0cf07fa365269e7801bb111565cb478 Mon Sep 17 00:00:00 2001
From: Scott Shawcroft <scott@tannewt.org>
Date: Fri, 15 Feb 2019 16:53:19 -0800
Subject: [PATCH] Support print("", flush=True)

Fixes #1127
---
 lib/utils/sys_stdio_mphal.c | 20 +++++++++++++++++---
 py/modbuiltins.c            |  6 +++++-
 py/stream.c                 |  7 +++++--
 py/stream.h                 |  1 +
 4 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/lib/utils/sys_stdio_mphal.c b/lib/utils/sys_stdio_mphal.c
index fc8a74e7d6..266783f376 100644
--- a/lib/utils/sys_stdio_mphal.c
+++ b/lib/utils/sys_stdio_mphal.c
@@ -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();
@@ -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;
@@ -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;
 }
@@ -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,
 };
 
diff --git a/py/modbuiltins.c b/py/modbuiltins.c
index e7ce7f9981..b4f4fca34b 100644
--- a/py/modbuiltins.c
+++ b/py/modbuiltins.c
@@ -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
@@ -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
diff --git a/py/stream.c b/py/stream.c
index 9d8be445c5..08c749cc30 100644
--- a/py/stream.c
+++ b/py/stream.c
@@ -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;
diff --git a/py/stream.h b/py/stream.h
index bcba4f234b..03e24c7469 100644
--- a/py/stream.h
+++ b/py/stream.h
@@ -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