unix: Convert to use core-provided version of built-in import().

This commit is contained in:
Damien George 2017-06-01 16:01:27 +10:00
parent 6ff0ecfffc
commit d92898a35a
3 changed files with 16 additions and 42 deletions

View File

@ -37,32 +37,8 @@
#include "lib/mp-readline/readline.h"
#endif
#if MICROPY_USE_READLINE == 0
char *prompt(char *p) {
#if MICROPY_USE_READLINE == 1
// MicroPython supplied readline
vstr_t vstr;
vstr_init(&vstr, 16);
mp_hal_stdio_mode_raw();
int ret = readline(&vstr, p);
mp_hal_stdio_mode_orig();
if (ret != 0) {
vstr_clear(&vstr);
if (ret == CHAR_CTRL_D) {
// EOF
printf("\n");
return NULL;
} else {
printf("\n");
char *line = malloc(1);
line[0] = '\0';
return line;
}
}
vstr_null_terminated_str(&vstr);
char *line = malloc(vstr.len + 1);
memcpy(line, vstr.buf, vstr.len + 1);
vstr_clear(&vstr);
#else
// simple read string
static char buf[256];
fputs(p, stdout);
@ -78,9 +54,9 @@ char *prompt(char *p) {
}
char *line = malloc(l);
memcpy(line, buf, l);
#endif
return line;
}
#endif
void prompt_read_history(void) {
#if MICROPY_USE_READLINE_HISTORY
@ -143,18 +119,3 @@ void prompt_write_history(void) {
#endif
#endif
}
STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) {
if (n_args == 1) {
mp_obj_print(args[0], PRINT_STR);
}
char *line = prompt("");
if (line == NULL) {
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
}
mp_obj_t o = mp_obj_new_str(line, strlen(line), false);
free(line);
return o;
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);

View File

@ -81,6 +81,7 @@
#define MICROPY_PY_BUILTINS_FROZENSET (1)
#define MICROPY_PY_BUILTINS_COMPILE (1)
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (1)
#define MICROPY_PY_BUILTINS_INPUT (1)
#define MICROPY_PY_BUILTINS_POW3 (1)
#define MICROPY_PY_MICROPYTHON_MEM_INFO (1)
#define MICROPY_PY_ALL_SPECIAL_METHODS (1)
@ -279,7 +280,6 @@ void mp_unix_mark_exec(void);
#endif
#define MICROPY_PORT_BUILTINS \
{ MP_ROM_QSTR(MP_QSTR_input), MP_ROM_PTR(&mp_builtin_input_obj) }, \
{ MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) },
#define MP_STATE_PORT MP_STATE_VM

View File

@ -34,6 +34,19 @@ void mp_hal_set_interrupt_char(char c);
void mp_hal_stdio_mode_raw(void);
void mp_hal_stdio_mode_orig(void);
#if MICROPY_USE_READLINE == 1 && MICROPY_PY_BUILTINS_INPUT
#include "py/misc.h"
#include "lib/mp-readline/readline.h"
// For built-in input() we need to wrap the standard readline() to enable raw mode
#define mp_hal_readline mp_hal_readline
static inline int mp_hal_readline(vstr_t *vstr, const char *p) {
mp_hal_stdio_mode_raw();
int ret = readline(vstr, p);
mp_hal_stdio_mode_orig();
return ret;
}
#endif
// TODO: POSIX et al. define usleep() as guaranteedly capable only of 1s sleep:
// "The useconds argument shall be less than one million."
static inline void mp_hal_delay_ms(mp_uint_t ms) { usleep((ms) * 1000); }