Skip to content

Commit 554ba06

Browse files
andrewleechpi-anl
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 784aa4c commit 554ba06

16 files changed

+36
-93
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-
} 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/modmachine.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@
3636
#define MICROPY_PAGE_MASK (MICROPY_PAGE_SIZE - 1)
3737
#endif
3838

39-
// This variable is needed for machine.soft_reset(), but the variable is otherwise unused.
40-
int pyexec_system_exit = 0;
41-
4239
uintptr_t mod_machine_mem_get_addr(mp_obj_t addr_o, uint align) {
4340
uintptr_t addr = mp_obj_get_int_truncated(addr_o);
4441
if ((addr & (align - 1)) != 0) {

ports/unix/mphalport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ void mp_hal_set_interrupt_char(char c);
4848
void mp_hal_stdio_mode_raw(void);
4949
void mp_hal_stdio_mode_orig(void);
5050

51+
// pyexec/repl needs stdio to be in raw mode, but this may be cleared before running code.
52+
#if MICROPY_REPL_RESET_RAW_BEFORE_EXEC
53+
#define MICROPY_BOARD_BEFORE_PYTHON_EXEC(input_kind, exec_flags) mp_hal_stdio_mode_orig()
54+
#define MICROPY_BOARD_AFTER_PYTHON_EXEC(input_kind, exec_flags, ret_val, ret) mp_hal_stdio_mode_raw()
55+
#endif
56+
5157
#if MICROPY_PY_BUILTINS_INPUT && MICROPY_USE_READLINE == 0
5258

5359
#include <malloc.h>

ports/windows/Makefile

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

8081
LIB += -lws2_32

ports/windows/micropython.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<ClCompile Include="@(PyExtModSource)" />
9090
<ClCompile Include="$(PyBaseDir)shared\readline\*.c" />
9191
<ClCompile Include="$(PyBaseDir)shared\runtime\gchelper_generic.c" />
92+
<ClCompile Include="$(PyBaseDir)shared\runtime\pyexec.c" />
9293
<ClCompile Include="$(PyBaseDir)ports\windows\*.c" />
9394
<ClCompile Include="$(PyBaseDir)ports\windows\msvc\*.c" />
9495
<ClCompile Include="$(PyBaseDir)ports\unix\gccollect.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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy