Skip to content

Commit bc76302

Browse files
committed
py/modbuiltins: Add core-provided version of input() function.
The implementation is taken from stmhal/input.c, with code added to handle ctrl-C. This built-in is controlled by MICROPY_PY_BUILTINS_INPUT and is disabled by default. It uses readline() to capture input but this can be overridden by defining the mp_hal_readline macro.
1 parent b53a635 commit bc76302

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

py/modbuiltins.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,35 @@ STATIC mp_obj_t mp_builtin_hex(mp_obj_t o_in) {
259259
}
260260
MP_DEFINE_CONST_FUN_OBJ_1(mp_builtin_hex_obj, mp_builtin_hex);
261261

262+
#if MICROPY_PY_BUILTINS_INPUT
263+
264+
#include "py/mphal.h"
265+
#include "lib/mp-readline/readline.h"
266+
267+
// A port can define mp_hal_readline if they want to use a custom function here
268+
#ifndef mp_hal_readline
269+
#define mp_hal_readline readline
270+
#endif
271+
272+
STATIC mp_obj_t mp_builtin_input(size_t n_args, const mp_obj_t *args) {
273+
if (n_args == 1) {
274+
mp_obj_print(args[0], PRINT_STR);
275+
}
276+
vstr_t line;
277+
vstr_init(&line, 16);
278+
int ret = mp_hal_readline(&line, "");
279+
if (ret == CHAR_CTRL_C) {
280+
nlr_raise(mp_obj_new_exception(&mp_type_KeyboardInterrupt));
281+
}
282+
if (line.len == 0 && ret == CHAR_CTRL_D) {
283+
nlr_raise(mp_obj_new_exception(&mp_type_EOFError));
284+
}
285+
return mp_obj_new_str_from_vstr(&mp_type_str, &line);
286+
}
287+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_input_obj, 0, 1, mp_builtin_input);
288+
289+
#endif
290+
262291
STATIC mp_obj_t mp_builtin_iter(mp_obj_t o_in) {
263292
return mp_getiter(o_in, NULL);
264293
}
@@ -676,6 +705,9 @@ STATIC const mp_rom_map_elem_t mp_module_builtins_globals_table[] = {
676705
#endif
677706
{ MP_ROM_QSTR(MP_QSTR_hex), MP_ROM_PTR(&mp_builtin_hex_obj) },
678707
{ MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&mp_builtin_id_obj) },
708+
#if MICROPY_PY_BUILTINS_INPUT
709+
{ MP_ROM_QSTR(MP_QSTR_input), MP_ROM_PTR(&mp_builtin_input_obj) },
710+
#endif
679711
{ MP_ROM_QSTR(MP_QSTR_isinstance), MP_ROM_PTR(&mp_builtin_isinstance_obj) },
680712
{ MP_ROM_QSTR(MP_QSTR_issubclass), MP_ROM_PTR(&mp_builtin_issubclass_obj) },
681713
{ MP_ROM_QSTR(MP_QSTR_iter), MP_ROM_PTR(&mp_builtin_iter_obj) },

py/mpconfig.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,12 @@ typedef double mp_float_t;
791791
#define MICROPY_PY_BUILTINS_NOTIMPLEMENTED (0)
792792
#endif
793793

794+
// Whether to provide the built-in input() function. The implementation of this
795+
// uses mp-readline, so can only be enabled if the port uses this readline.
796+
#ifndef MICROPY_PY_BUILTINS_INPUT
797+
#define MICROPY_PY_BUILTINS_INPUT (0)
798+
#endif
799+
794800
// Whether to support min/max functions
795801
#ifndef MICROPY_PY_BUILTINS_MIN_MAX
796802
#define MICROPY_PY_BUILTINS_MIN_MAX (1)

0 commit comments

Comments
 (0)