8000 unix/main: Use standard pyexec/repl for unix & windows ports. · micropython/micropython@ad27be1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad27be1

Browse files
Andrew Leechandrewleech
authored andcommitted
unix/main: Use standard pyexec/repl for unix & windows ports.
This improves repl usage consistency across ports. Only enabled when MICROPY_USE_READLINE == 1 (default). Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
1 parent b78eb8c commit ad27be1

15 files changed

+36
-90
lines changed

ports/unix/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,10 @@ endif
124124

125125
ifeq ($(MICROPY_USE_READLINE),1)
126126
INC += -I$(TOP)/shared/readline
127+
INC += -I$(TOP)/shared/runtime
127128
CFLAGS += -DMICROPY_USE_READLINE=1
128129
SHARED_SRC_C_EXTRA += readline/readline.c
130+
SHARED_SRC_C_EXTRA += runtime/pyexec.c
129131
endif
130132
ifeq ($(MICROPY_PY_TERMIOS),1)
131133
CFLAGS += -DMICROPY_PY_TERMIOS=1

ports/unix/main.c

Lines changed: 16 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "extmod/vfs_posix.h"
5454
#include "genhdr/mpversion.h"
5555
#include "input.h"
56+
#include "shared/runtime/pyexec.h"
5657

5758
// Command line options, with their defaults
5859
STATIC bool compile_only = false;
@@ -193,91 +194,27 @@ STATIC char *strjoin(const char *s1, int sep_char, const char *s2) {
193194
#endif
194195

195196
STATIC int do_repl(void) {
196-
mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION);
197-
mp_hal_stdout_tx_str("; " MICROPY_BANNER_MACHINE);
198-
mp_hal_stdout_tx_str("\nUse Ctrl-D to exit, Ctrl-E for paste mode\n");
199-
197+
int ret = 0;
200198
#if MICROPY_USE_READLINE == 1
201-
202-
// use MicroPython supplied readline
203-
204-
vstr_t line;
205-
vstr_init(&line, 16);
199+
// use MicroPython supplied readline based repl
200+
mp_hal_stdio_mode_raw();
206201
for (;;) {
207-
mp_hal_stdio_mode_raw();
208-
209-
input_restart:
210-
vstr_reset(&line);
211-
int ret = readline(&line, mp_repl_get_ps1());
212-
mp_parse_input_kind_t parse_input_kind = MP_PARSE_SINGLE_INPUT;
213-
214-
if (ret == CHAR_CTRL_C) {
215-
// cancel input
216-
mp_hal_stdout_tx_str("\r\n");
217-
goto input_restart;
218-
} else if (ret == CHAR_CTRL_D) {
219-
// EOF
220-
printf("\n");
221-
mp_hal_stdio_mode_orig();
222-
vstr_clear(&line);
223-
return 0;
224-
} else if (ret == CHAR_CTRL_E) {
225-
// paste mode
226-
mp_hal_stdout_tx_str("\npaste mode; Ctrl-C to cancel, Ctrl-D to finish\n=== ");
227-
vstr_reset(&line);
228-
for (;;) {
229-
char c = mp_hal_stdin_rx_chr();
230-
if (c == CHAR_CTRL_C) {
231-
// cancel everything
232-
mp_hal_stdout_tx_str("\n");
233-
goto input_restart;
234-
} else if (c == CHAR_CTRL_D) {
235-
// end of input
236-
mp_hal_stdout_tx_str("\n");
237-
break;
238-
} else {
239-
// add char to buffer and echo
240-
vstr_add_byte(&line, c);
241-
if (c == '\r') {
242-
mp_hal_stdout_tx_str("\n=== ");
243-
} else {
244-
mp_hal_stdout_tx_strn(&c, 1);
245-
}
246-
}
247-
}
248-
parse_input_kind = MP_PARSE_FILE_INPUT;
249-
} else if (line.len == 0) {
250-
if (ret != 0) {
251-
printf("\n");
202+
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
203+
if ((ret = pyexec_raw_repl()) != 0) {
204+
break;
252205
}
253-
goto input_restart;
254206
} else {
255-
// got a line with non-zero length, see if it needs continuing
256-
while (mp_repl_continue_with_input(vstr_null_terminated_str(&line))) {
257-
vstr_add_byte(&line, '\n');
258-
ret = readline(&line, mp_repl_get_ps2());
259-
if (ret == CHAR_CTRL_C) {
260-
// cancel everything
261-
printf("\n");
262-
goto input_restart;
263-
F438 } else if (ret == CHAR_CTRL_D) {
264-
// stop entering compound statement
265-
break;
266-
}
207+
if ((ret = pyexec_friendly_repl()) != 0) {
208+
break;
267209
}
268210
}
269-
270-
mp_hal_stdio_mode_orig();
271-
272-
ret = execute_from_lexer(LEX_SRC_VSTR, &line, parse_input_kind, true);
273-
if (ret & FORCED_EXIT) {
274-
return ret;
275-
}
276211
}
277-
212+
mp_hal_stdio_mode_orig();
278213
#else
279-
280214
// use simple readline
215+
mp_hal_stdout_tx_str(MICROPY_BANNER_NAME_AND_VERSION);
216+
mp_hal_stdout_tx_str("; " MICROPY_BANNER_MACHINE);
217+
mp_hal_stdout_tx_str("\nUse Ctrl-D to exit, Ctrl-E for paste mode\n");
281218

282219
for (;;) {
283220
char *line = prompt((char *)mp_repl_get_ps1());
@@ -296,16 +233,14 @@ STATIC int do_repl(void) {
296233
line = line3;
297234
}
298235

299-
int ret = execute_from_lexer(LEX_SRC_STR, line, MP_PARSE_SINGLE_INPUT, true);
236+
ret = execute_from_lexer(LEX_SRC_STR, line, MP_PARSE_SINGLE_INPUT, true);
300237
free(line);
301-
if (ret & FORCED_EXIT) {
302-
return ret;
303-
}
304238
}
305-
306239
#endif
240+
return ret;
307241
}
308242

243+
309244
STATIC int do_file(const char *file) {
310245
return execute_from_lexer(LEX_SRC_FILENAME, file, MP_PARSE_FILE_INPUT, false);
311246
}

ports/unix/mphalport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ void mp_hal_set_interrupt_char(char c);
3636
void mp_hal_stdio_mode_raw(void);
3737
void mp_hal_stdio_mode_orig(void);
3838

39+
// pyexec/repl needs stdio to be in raw mode, but this may be cleared before running code.
40+
#if MICROPY_REPL_RESET_RAW_BEFORE_EXEC
41+
#define MICROPY_BOARD_BEFORE_PYTHON_EXEC(input_kind, exec_flags) mp_hal_stdio_mode_orig()
42+
#define MICROPY_BOARD_AFTER_PYTHON_EXEC(input_kind, exec_flags, ret_val, ret) mp_hal_stdio_mode_raw()
43+
#endif
44+
3945
#if MICROPY_PY_BUILTINS_INPUT && MICROPY_USE_READLINE == 0
4046

4147
#include <malloc.h>

ports/windows/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
7676
ifeq ($(MICROPY_USE_READLINE),1)
7777
CFLAGS += -DMICROPY_USE_READLINE=1
7878
SRC_C += shared/readline/readline.c
79+
SRC_C += shared/runtime/pyexec.c
7980
endif
8081

8182
LIB += -lws2_32

ports/windows/micropython.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
<ClCompile Include="@(PyCoreSource)" />
8989
<ClCompile Include="@(PyExtModSource)" />
9090
<ClCompile Include="$(PyBaseDir)shared\readline\*.c" />
91+
<ClCompile Include="$(PyBaseDir)shared\runtime\pyexec.c" />
9192
<ClCompile Include="$(PyBaseDir)shared\runtime\gchelper_generic.c" />
9293
<ClCompile Include="$(PyBaseDir)ports\windows\*.c" />
9394
<ClCompile Include="$(PyBaseDir)ports\windows\msvc\*.c" />

shared/runtime/pyexec.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#endif
4242
#include "shared/readline/readline.h"
4343
#include "shared/runtime/pyexec.h"
44+
#include "extmod/modplatform.h"
4445
#include "genhdr/mpversion.h"
4546

4647
pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;

tests/cmdline/repl_autocomplete.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MicroPython \.\+ version
2-
Use \.\+
2+
Type "help()" for more information.
33
>>> # tests for autocompletion
44
>>> import sys
55
>>> not_exist.

tests/cmdline/repl_autoindent.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MicroPython \.\+ version
2-
Use \.\+
2+
Type "help()" for more information.
33
>>> # tests for autoindent
44
>>> if 1:
55
... print(1)

tests/cmdline/repl_basic.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MicroPython \.\+ version
2-
Use \.\+
2+
Type "help()" for more information.
33
>>> # basic REPL tests
44
>>> print(1)
55
1

tests/cmdline/repl_cont.py.exp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MicroPython \.\+ version
2-
Use \.\+
2+
Type "help()" for more information.
33
>>> # check REPL allows to continue input
44
>>> 1 \\\\
55
... + 2

0 commit comments

Comments
 (0)
0