Skip to content

Commit 5ae5ec9

Browse files
committed
py: Make mp_sys_stdout_print object, wrapping sys.stdout for mp_print*.
So now all printing should go via either mp_plat_print or mp_sys_stdout_print.
1 parent 7f9d1d6 commit 5ae5ec9

4 files changed

Lines changed: 13 additions & 12 deletions

File tree

py/modbuiltins.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,8 @@ MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_print_obj, 0, mp_builtin_print);
440440
STATIC mp_obj_t mp_builtin___repl_print__(mp_obj_t o) {
441441
if (o != mp_const_none) {
442442
#if MICROPY_PY_IO
443-
mp_print_t print = {&mp_sys_stdout_obj, (mp_print_strn_t)mp_stream_write};
444-
mp_obj_print_helper(&print, o, PRINT_REPR);
445-
mp_stream_write(&mp_sys_stdout_obj, "\n", 1);
443+
mp_obj_print_helper(&mp_sys_stdout_print, o, PRINT_REPR);
444+
mp_print_str(&mp_sys_stdout_print, "\n");
446445
#else
447446
mp_obj_print(o, PRINT_REPR);
448447
printf("\n");

py/modsys.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ extern struct _mp_dummy_t mp_sys_stdin_obj;
4242
extern struct _mp_dummy_t mp_sys_stdout_obj;
4343
extern struct _mp_dummy_t mp_sys_stderr_obj;
4444

45+
#if MICROPY_PY_IO
46+
const mp_print_t mp_sys_stdout_print = {&mp_sys_stdout_obj, (mp_print_strn_t)mp_stream_write};
47+
#endif
48+
4549
/// \constant version - Python language version that this implementation conforms to, as a string
4650
STATIC const MP_DEFINE_STR_OBJ(version_obj, "3.4.0");
4751

py/mpprint.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,13 @@ typedef struct _mp_print_t {
4747
mp_print_strn_t print_strn;
4848
} mp_print_t;
4949

50-
// Wrapper for platform print function, which wraps MP_PLAT_PRINT_STRN.
51-
// All (non-debug) prints go through this interface (except some which
52-
// go through mp_sys_stdout_obj if MICROPY_PY_IO is defined).
50+
// All (non-debug) prints go through one of the two interfaces below.
51+
// 1) Wrapper for platform print function, which wraps MP_PLAT_PRINT_STRN.
5352
extern const mp_print_t mp_plat_print;
53+
#if MICROPY_PY_IO
54+
// 2) Wrapper for printing to sys.stdout.
55+
extern const mp_print_t mp_sys_stdout_print;
56+
#endif
5457

5558
int mp_print_str(const mp_print_t *print, const char *str);
5659
int mp_print_strn(const mp_print_t *print, const char *str, mp_uint_t len, int flags, char fill, int width);

py/obj.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ void mp_obj_print_helper(const mp_print_t *print, mp_obj_t o_in, mp_print_kind_t
7373

7474
void mp_obj_print(mp_obj_t o_in, mp_print_kind_t kind) {
7575
#if MICROPY_PY_IO
76-
// defined per port; type of these is irrelevant, just need pointer
77-
extern struct _mp_dummy_t mp_sys_stdout_obj;
78-
mp_print_t print;
79-
print.data = &mp_sys_stdout_obj;
80-
print.print_strn = (mp_print_strn_t)mp_stream_write;
81-
mp_obj_print_helper(&print, o_in, kind);
76+
mp_obj_print_helper(&mp_sys_stdout_print, o_in, kind);
8277
#else
8378
mp_obj_print_helper(&mp_plat_print, o_in, kind);
8479
#endif

0 commit comments

Comments
 (0)