Skip to content
This repository was archived by the owner on Apr 25, 2021. It is now read-only.

Commit 1d7508e

Browse files
committed
Add stderr hook
1 parent 031f40a commit 1d7508e

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void mp_init_port() {
2626
MP_STATE_PORT(signal_hook_obj) = mp_const_none;
2727
MP_STATE_PORT(stdin_hook_obj) = mp_const_none;
2828
MP_STATE_PORT(stdout_hook_obj) = mp_const_none;
29+
MP_STATE_PORT(stderr_hook_obj) = mp_const_none;
2930
}
3031

3132
void do_str(const char *src, mp_parse_input_kind_t input_kind) {

src/modmachine.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ STATIC mp_obj_t machine_hook_signal(mp_obj_t hook_obj) {
2424
MP_DEFINE_CONST_FUN_OBJ_1(machine_hook_signal_obj, machine_hook_signal);
2525

2626

27+
STATIC mp_obj_t machine_hook_stdin(mp_obj_t hook_obj) {
28+
MP_STATE_PORT(stdin_hook_obj) = hook_obj;
29+
return hook_obj;
30+
}
31+
32+
MP_DEFINE_CONST_FUN_OBJ_1(machine_hook_stdin_obj, machine_hook_stdin);
33+
34+
2735
STATIC mp_obj_t machine_hook_stdout(mp_obj_t hook_obj) {
2836
MP_STATE_PORT(stdout_hook_obj) = hook_obj;
2937
return hook_obj;
@@ -32,12 +40,12 @@ STATIC mp_obj_t machine_hook_stdout(mp_obj_t hook_obj) {
3240
MP_DEFINE_CONST_FUN_OBJ_1(machine_hook_stdout_obj, machine_hook_stdout);
3341

3442

35-
STATIC mp_obj_t machine_hook_stdin(mp_obj_t hook_obj) {
36-
MP_STATE_PORT(stdin_hook_obj) = hook_obj;
43+
STATIC mp_obj_t machine_hook_stderr(mp_obj_t hook_obj) {
44+
MP_STATE_PORT(stderr_hook_obj) = hook_obj;
3745
return hook_obj;
3846
}
3947

40-
MP_DEFINE_CONST_FUN_OBJ_1(machine_hook_stdin_obj, machine_hook_stdin);
48+
MP_DEFINE_CONST_FUN_OBJ_1(machine_hook_stderr_obj, machine_hook_stderr);
4149

4250

4351
STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
@@ -48,8 +56,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
4856

4957
// hook
5058
{MP_ROM_QSTR(MP_QSTR_hook_signal), MP_ROM_PTR(&machine_hook_signal_obj)},
51-
{MP_ROM_QSTR(MP_QSTR_hook_stdout), MP_ROM_PTR(&machine_hook_stdout_obj)},
5259
{MP_ROM_QSTR(MP_QSTR_hook_stdin), MP_ROM_PTR(&machine_hook_stdin_obj)},
60+
{MP_ROM_QSTR(MP_QSTR_hook_stdout), MP_ROM_PTR(&machine_hook_stdout_obj)},
61+
{MP_ROM_QSTR(MP_QSTR_hook_stderr), MP_ROM_PTR(&machine_hook_stderr_obj)},
5362
};
5463

5564
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);

src/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ extern const struct _mp_obj_type_t mp_type_SystemError;
236236
mp_obj_t signal_hook_obj; \
237237
mp_obj_t stdin_hook_obj; \
238238
mp_obj_t stdout_hook_obj; \
239+
mp_obj_t stderr_hook_obj; \
239240
mp_obj_t object_hook_obj; \
240241

241242
#define mp_type_fileio mp_type_vfs_openpie_fileio

src/mphalport.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
#include "mphalport.h"
99
#include "syscall.h"
1010

11+
12+
void mp_hal_stdio_tx_strn(mp_obj_t hook_obj, const char *str, mp_uint_t len) {
13+
if (hook_obj == mp_const_none) {
14+
__syscall2(SYS_DEBUG, (int)str, (int)len);
15+
} else {
16+
mp_obj_t str_obj = mp_obj_new_str(str, len);
17+
mp_call_function_1(hook_obj, str_obj);
18+
}
19+
}
20+
1121
int mp_hal_stdin_rx_chr(void) {
1222
if (MP_STATE_PORT(stdin_hook_obj) == mp_const_none) {
1323
return 0;
@@ -22,12 +32,7 @@ void mp_hal_stdout_tx_str(const char *str) {
2232
}
2333

2434
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
25-
if (MP_STATE_PORT(stdout_hook_obj) == mp_const_none) {
26-
__syscall2(SYS_DEBUG, (int)str, (int)len);
27-
} else {
28-
mp_obj_t str_obj = mp_obj_new_str(str, len);
29-
mp_call_function_1(MP_STATE_PORT(stdout_hook_obj), str_obj);
30-
}
35+
mp_hal_stdio_tx_strn(MP_STATE_PORT(stdout_hook_obj), str, len);
3136
}
3237

3338
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {

src/openpie_vfs.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,24 @@ STATIC mp_uint_t vfs_openpie_file_write(mp_obj_t o_in, const void *buf, mp_uint_
121121
mp_obj_vfs_openpie_file_t *o = MP_OBJ_TO_PTR(o_in);
122122
check_fd_is_open(o);
123123
#if MICROPY_PY_OS_DUPTERM
124-
if (o->fd <= STDERR_FILENO && o->flag & 1) {
125-
mp_hal_stdout_tx_strn(buf, size);
126-
return size;
124+
if (o->flag & 1) {
125+
switch (o->fd) {
126+
case STDIN_FILENO:
127+
return 0;
128+
case STDOUT_FILENO:
129+
// mp_hal_stdout_tx_strn(buf, size);
130+
mp_hal_stdio_tx_strn(MP_STATE_PORT(stdout_hook_obj), buf, size);
131+
return size;
132+
case STDERR_FILENO:
133+
{
134+
mp_obj_t stdio_hook_obj = MP_STATE_PORT(stderr_hook_obj);
135+
if (stdio_hook_obj == NULL)
136+
stdio_hook_obj = MP_STATE_PORT(stdout_hook_obj);
137+
138+
mp_hal_stdio_tx_strn(stdio_hook_obj, buf, size);
139+
return size;
140+
}
141+
}
127142
}
128143
#endif
129144
int written = 0;

src/umodules/machine.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
# machine
22
"functions related to the hardware"
3+
from typing import Callable, Tuple
34

45

56
def debug(arg: str):
67
pass
78

89

9-
def hook_stdin(func):
10+
def hook_signal(func: Callable[[Tuple[str, Tuple]], None]):
1011
pass
1112

1213

13-
def hook_stdout(func):
14+
def hook_stdin(func: Callable[[str], None]):
1415
pass
1516

1617

17-
def hook_signal(func):
18+
def hook_stdout(func: Callable[[str], None]):
19+
pass
20+
21+
22+
def hook_stderr(func: Callable[[str], None]):
1823
pass

0 commit comments

Comments
 (0)