8000 Merge pull request #1555 from tannewt/print_flush · tannewt/circuitpython@619b4f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 619b4f1

Browse files
authored
Merge pull request micropython#1555 from tannewt/print_flush
Support print("", flush=True)
2 parents 11ef64e + e6b140e commit 619b4f1

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

lib/utils/sys_stdio_mphal.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ STATIC const sys_stdio_obj_t stdio_buffer_obj;
5353
#endif
5454

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

6060
STATIC mp_uint_t stdio_read(mp_obj_t self_in, void *buf, mp_uint_t size, int *errcode) {
61-
sys_stdio_obj_t *self = self_in;
61+
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
6262
if (self->fd == STDIO_FD_IN) {
6363
for (uint i = 0; i < size; i++) {
6464
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
7575
}
7676

7777
STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size, int *errcode) {
78-
sys_stdio_obj_t *self = self_in;
78+
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
7979
if (self->fd == STDIO_FD_OUT || self->fd == STDIO_FD_ERR) {
8080
mp_hal_stdout_tx_strn_cooked(buf, size);
8181
return size;
@@ -85,6 +85,19 @@ STATIC mp_uint_t stdio_write(mp_obj_t self_in, const void *buf, mp_uint_t size,
8585
}
8686
}
8787

88+
STATIC mp_uint_t stdio_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) {
89+
sys_stdio_obj_t *self = MP_OBJ_TO_PTR(self_in);
90+
(void) self;
91+
92+
// For now, pretend we actually flush the stdio stream.
93+
if (request == MP_STREAM_FLUSH) {
94+
return 0;
95+
} else {
96+
*errcode = MP_EINVAL;
97+
return MP_STREAM_ERROR;
98+
}
99+
}
100+
88101
STATIC mp_obj_t stdio_obj___exit__(size_t n_args, const mp_obj_t *args) {
89102
return mp_const_none;
90103
}
@@ -112,6 +125,7 @@ STATIC MP_DEFINE_CONST_DICT(stdio_locals_dict, stdio_locals_dict_table);
112125
STATIC const mp_stream_p_t stdio_obj_stream_p = {
113126
.read = stdio_read,
114127
.write = stdio_write,
128+
.ioctl = stdio_ioctl,
115129
.is_text = true,
116130
};
117131

py/modbuiltins.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,11 @@ STATIC mp_obj_t mp_builtin_pow(size_t n_args, const mp_obj_t *args) {
371371
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_pow_obj, 2, 3, mp_builtin_pow);
372372

373373
STATIC mp_obj_t mp_builtin_print(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
374-
enum { ARG_sep, ARG_end, ARG_file };
374+
enum { ARG_sep, ARG_end, ARG_flush, ARG_file };
375375
static const mp_arg_t allowed_args[] = {
376376
{ MP_QSTR_sep, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR__space_)} },
377377
{ MP_QSTR_end, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR__0x0a_)} },
378+
{ MP_QSTR_flush, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
378379
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
379380
{ MP_QSTR_file, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_sys_stdout_obj)} },
380381
#endif
@@ -414,6 +415,9 @@ STATIC mp_obj_t mp_builtin_print(size_t n_args, const mp_obj_t *pos_args, mp_map
414415
}
415416
#if MICROPY_PY_IO && MICROPY_PY_SYS_STDFILES
416417
mp_stream_write_adaptor(print.data, end_data, u.len[1]);
418+
if (u.args[ARG_flush].u_bool) {
419+
mp_stream_flush(MP_OBJ_FROM_PTR(print.data));
420+
}
417421
#else
418422
mp_print_strn(&mp_plat_print, end_data, u.len[1], 0, 0, 0);
419423
#endif

py/stream.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,19 @@ STATIC mp_obj_t stream_tell(mp_obj_t self) {
468468
}
469469
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_tell_obj, stream_tell);
470470

471-
STATIC mp_obj_t stream_flush(mp_obj_t self) {
471+
mp_obj_t mp_stream_flush(mp_obj_t self) {
472472
const mp_stream_p_t *stream_p = mp_get_stream(self);
473473
int error;
474+
if (stream_p->ioctl == NULL) {
475+
mp_raise_OSError(MP_EINVAL);
476+
}
474477
mp_uint_t res = stream_p->ioctl(self, MP_STREAM_FLUSH, 0, &error);
475478
if (res == MP_STREAM_ERROR) {
476479
mp_raise_OSError(error);
477480
}
478481
return mp_const_none;
479482
}
480-
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, stream_flush);
483+
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_flush_obj, mp_stream_flush);
481484

482485
STATIC mp_obj_t stream_ioctl(size_t n_args, const mp_obj_t *args) {
483486
mp_buffer_info_t bufinfo;

py/stream.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ mp_uint_t mp_stream_rw(mp_obj_t stream, void *buf, mp_uint_t size, int *errcode,
113113
#define mp_stream_read_exactly(stream, buf, size, err) mp_stream_rw(stream, buf, size, err, MP_STREAM_RW_READ)
114114

115115
void mp_stream_write_adaptor(void *self, const char *buf, size_t len);
116+
mp_obj_t mp_stream_flush(mp_obj_t self);
116117

117118
#if MICROPY_STREAMS_POSIX_API
118119
// Functions with POSIX-compatible signatures

0 commit comments

Comments
 (0)
0