Skip to content

Commit 7b41822

Browse files
committed
shared/runtime/pyexec: Add POSIX-specific execution functions.
Add pyexec_str_single() and pyexec_stdin() functions guarded by MICROPY_PYEXEC_POSIX_FUNCTIONS to consolidate execution logic previously handled by port-specific code. Signed-off-by: Andrew Leech <[email protected]>
1 parent a983716 commit 7b41822

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

shared/runtime/pyexec.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
8686
ctx->module.globals = mp_globals_get();
8787
ctx->constants = frozen->constants;
8888
module_fun = mp_make_function_from_proto_fun(frozen->proto_fun, ctx, NULL);
89-
89+
9090
#if MICROPY_PY___FILE__
9191
// Set __file__ for frozen MPY modules
9292
if (input_kind == MP_PARSE_FILE_INPUT && frozen_module_name != NULL) {
@@ -759,6 +759,20 @@ int pyexec_vstr(vstr_t *str, bool allow_keyboard_interrupt) {
759759
return parse_compile_execute(str, MP_PARSE_FILE_INPUT, exec_flags | EXEC_FLAG_SOURCE_IS_VSTR, NULL);
760760
}
761761

762+
#if MICROPY_PYEXEC_POSIX_FUNCTIONS
763+
int pyexec_str_single(const char *str, bool allow_keyboard_interrupt) {
764+
mp_uint_t exec_flags = allow_keyboard_interrupt ? 0 : EXEC_FLAG_NO_INTERRUPT;
765+
return parse_compile_execute(str, MP_PARSE_SINGLE_INPUT, exec_flags | EXEC_FLAG_ALLOW_DEBUGGING | EXEC_FLAG_IS_REPL, NULL);
766+
}
767+
768+
int pyexec_stdin(void) {
769+
mp_reader_t reader;
770+
mp_reader_new_file_from_fd(&reader, 0, false);
771+
mp_uint_t exec_flags = EXEC_FLAG_PRINT_EOF | EXEC_FLAG_SOURCE_IS_READER;
772+
return parse_compile_execute(&reader, MP_PARSE_FILE_INPUT, exec_flags, NULL);
773+
}
774+
#endif
775+
762776
#if MICROPY_REPL_INFO
763777
mp_obj_t pyb_set_repl_info(mp_obj_t o_value) {
764778
repl_display_debugging_info = mp_obj_get_int(o_value);

shared/runtime/pyexec.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ int pyexec_file(const char *filename);
4343
int pyexec_file_if_exists(const char *filename);
4444
int pyexec_frozen_module(const char *name, bool allow_keyboard_interrupt);
4545
int pyexec_vstr(vstr_t *str, bool allow_keyboard_interrupt);
46+
#if MICROPY_PYEXEC_POSIX_FUNCTIONS
47+
int pyexec_str_single(const char *str, bool allow_keyboard_interrupt);
48+
int pyexec_stdin(void);
49+
#endif
4650
void pyexec_event_repl_init(void);
4751
int pyexec_event_repl_process_char(int c);
4852
extern uint8_t pyexec_repl_active;

0 commit comments

Comments
 (0)