Skip to content

Commit d92898a

Browse files
committed
unix: Convert to use core-provided version of built-in import().
1 parent 6ff0ecf commit d92898a

3 files changed

Lines changed: 16 additions & 42 deletions

File tree

unix/input.c

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,8 @@
3737
#include "lib/mp-readline/readline.h"
3838
#endif
3939

40+
#if MICROPY_USE_READLINE == 0
4041
char *prompt(char *p) {
41-
#if MICROPY_USE_READLINE == 1
42-
// MicroPython supplied readline
43-
vstr_t vstr;
44-
vstr_init(&vstr, 16);
45-
mp_hal_stdio_mode_raw();
46-
int ret = readline(&vstr, p);
47-
mp_hal_stdio_mode_orig();
48-
if (ret != 0) {
49-
vstr_clear(&vstr);
50-
if (ret == CHAR_CTRL_D) {
51-
// EOF
52-
printf("\n");
53-
return NULL;
54-
} else {
55-
printf("\n");
56-
char *line = malloc(1);
57-
line[0] = '\0';
58-
return line;
59-
}
60-
}
61-
vstr_null_terminated_str(&vstr);
62-
char *line = malloc(vstr.len + 1);
63-
memcpy(line, vstr.buf, vstr.len + 1);
64-
vstr_clear(&vstr);
65-
#else
6642
// simple read string
6743
static char buf[256];
6844
fputs(p, stdout);
@@ -78,9 +54,9 @@ char *prompt(char *p) {
7854
}
7955
char *line = malloc(l);
8056
memcpy(line, buf, l);
81-
#endif
8257
return line;
8358
}
59+
#endif
8460

8561
void prompt_read_history(void) {
8662
#if MICROPY_USE_READLINE_HISTORY
@@ -143,18 +119,3 @@ void prompt_write_history(void) {
143119
#endif
144120
#endif
145121
}
146-
147-
STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) {
148-
if (n_args == 1) {
149-
mp_obj_print(args[0], PRINT_STR);
150-
}
151-
152-
char *line = prompt("");
153-
if (line == NULL) {
154-
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
155-
}
156-
mp_obj_t o = mp_obj_new_str(line, strlen(line), false);
157-
free(line);
158-
return o;
159-
}
160-
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);

unix/mpconfigport.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#define MICROPY_PY_BUILTINS_FROZENSET (1)
8282
#define MICROPY_PY_BUILTINS_COMPILE (1)
8383
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1)
84+
#define MICROPY_PY_BUILTINS_INPUT (1)
8485
#define MICROPY_PY_BUILTINS_POW3 (1)
8586
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
8687
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
@@ -279,7 +280,6 @@ void mp_unix_mark_exec(void);
279280
#endif
280281

281282
#define MICROPY_PORT_BUILTINS \
282-
{ MP_ROM_QSTR(MP_QSTR_input), MP_ROM_PTR(&mp_builtin_input_obj) }, \
283283
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
284284

285285
#define MP_STATE_PORT MP_STATE_VM

unix/mphalport.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,19 @@ void mp_hal_set_interrupt_char(char c);
3434
void mp_hal_stdio_mode_raw(void);
3535
void mp_hal_stdio_mode_orig(void);
3636

37+
#if MICROPY_USE_READLINE == 1 && MICROPY_PY_BUILTINS_INPUT
38+
#include "py/misc.h"
39+
#include "lib/mp-readline/readline.h"
40+
// For built-in input() we need to wrap the standard readline() to enable raw mode
41+
#define mp_hal_readline mp_hal_readline
42+
static inline int mp_hal_readline(vstr_t *vstr, const char *p) {
43+
mp_hal_stdio_mode_raw();
44+
int ret = readline(vstr, p);
45+
mp_hal_stdio_mode_orig();
46+
return ret;
47+
}
48+
#endif
49+
3750
// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep:
3851
// "The useconds argument shall be less than one million."
3952
static inline void mp_hal_delay_ms(mp_uint_t ms) { usleep((ms) * 1000); }

0 commit comments

Comments
 (0)