Skip to content

Commit 13394a6

Browse files
committed
unix/unix_mphal: Hack to make uos.dupterm() actually work.
See micropython#1736 for the list of complications. This workaround instead of duplicating REPL to another stream, switches to it, because read(STDIN) we use otherwise is blocking call, so it and custom REPL stream can't be used together.
1 parent 53ad5ed commit 13394a6

1 file changed

Lines changed: 20 additions & 13 deletions

File tree

unix/unix_mphal.c

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ static int call_dupterm_read(void) {
117117
read_m[2] = MP_OBJ_NEW_SMALL_INT(1);
118118
mp_obj_t res = mp_call_method_n_kw(1, 0, read_m);
119119
if (res == mp_const_none) {
120-
return -1;
120+
return -2;
121121
}
122122
mp_buffer_info_t bufinfo;
123123
mp_get_buffer_raise(res, &bufinfo, MP_BUFFER_READ);
@@ -143,25 +143,32 @@ static int call_dupterm_read(void) {
143143

144144
int mp_hal_stdin_rx_chr(void) {
145145
unsigned char c;
146-
#if MICROPY_PY_OS_DUPTERM
147-
while (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
148-
int c = call_dupterm_read();
146+
#if MICROPY_PY_OS_DUPTERM
147+
if (MP_STATE_PORT(term_obj) != MP_OBJ_NULL) {
148+
int c;
149+
do {
150+
c = call_dupterm_read();
151+
} while (c == -2);
149152
if (c == -1) {
150-
break;
153+
goto main_term;
151154
}
152155
if (c == '\n') {
153156
c = '\r';
154157
}
155158
return c;
159+
} else {
160+
main_term:;
161+
#endif
162+
int ret = read(0, &c, 1);
163+
if (ret == 0) {
164+
c = 4; // EOF, ctrl-D
165+
} else if (c == '\n') {
166+
c = '\r';
167+
}
168+
return c;
169+
#if MICROPY_PY_OS_DUPTERM
156170
}
157-
#endif
158-
int ret = read(0, &c, 1);
159-
if (ret == 0) {
160-
c = 4; // EOF, ctrl-D
161-
} else if (c == '\n') {
162-
c = '\r';
163-
}
164-
return c;
171+
#endif
165172
}
166173

167174
void mp_hal_stdout_tx_strn(const char *str, size_t len) {

0 commit comments

Comments
 (0)