8000 ports/unix: Add embedded mode. by smurfix · Pull Request #12807 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ports/unix: Add embedded mode. #12807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion docs/unix/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Command line options

Usage::

micropython [ -h ] [ -i ] [ -O<level> ] [ -v ] [ -X <option> ] [ -c <command> | -m <module> | <script> ] [ <args> ]
micropython [ -h ] [ -i ] [ -e ] [ -O<level> ] [ -v ] [ -X <option> ] [ -c <command> | -m <module> | <script> ] [ <args> ]


Invocation options:
Expand All @@ -17,6 +17,20 @@ Invocation options:

Runs the code in ``<command>``. The code can be one or more Python statements.

.. option:: -e

Runs MicroPython in "embedded mode":

* ``boot.py`` and ``main.py`` are executed; boot.py unconditionally,
``main.py`` only if no script argument is used.

* Instead of the ``readline`` library, MicroPython uses the same REPL as
on embedded systems.

This mode is mainly useful for running MicroPython as a subprocess, e.g.
for testing. For interactive use, a ``stty raw; micropython -e …; stty
-raw`` wrapper is required.

.. option:: -m <module>

Runs the module ``<module>``. The module must be in ``sys.path``.
Expand Down
1 change: 1 addition & 0 deletions ports/unix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ SRC_C += \
SHARED_SRC_C += $(addprefix shared/,\
runtime/gchelper_generic.c \
timeutils/timeutils.c \
runtime/pyexec.c \
$(SHARED_SRC_C_EXTRA) \
)

Expand Down
37 changes: 37 additions & 0 deletions ports/unix/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "extmod/vfs_posix.h"
#include "genhdr/mpversion.h"
#include "input.h"
#include "shared/runtime/pyexec.h"

// Command line options, with their defaults
STATIC bool compile_only = false;
Expand Down Expand Up @@ -319,6 +320,9 @@ STATIC void print_help(char **argv) {
"usage: %s [<opts>] [-X <implopt>] [-c <command> | -m <module> | <filename>]\n"
"Options:\n"
"-h : print this help message\n"
#if MICROPY_USE_READLINE == 1
"-e : embedded mode: run 'boot.py', enable raw input\n"
#endif
"-i : enable inspection via REPL after running command/module/file\n"
#if MICROPY_DEBUG_PRINTERS
"-v : verbose (trace various operations); can be multiple\n"
Expand Down Expand Up @@ -622,6 +626,9 @@ MP_NOINLINE int main_(int argc, char **argv) {
const int NOTHING_EXECUTED = -2;
int ret = NOTHING_EXECUTED;
bool inspect = false;
#if MICROPY_USE_READLINE == 1
bool embed = false;
#endif
for (int a = 1; a < argc; a++) {
if (argv[a][0] == '-') {
if (strcmp(argv[a], "-i") == 0) {
Expand All @@ -634,6 +641,11 @@ MP_NOINLINE int main_(int argc, char **argv) {
set_sys_argv(argv, argc, a + 2); // Then what comes after the command
ret = do_str(argv[a + 1]);
break;
#if MICROPY_USE_READLINE == 1
} else if (strcmp(argv[a], "-e") == 0) {
embed = true;
pyexec_file_if_exists("boot.py");
#endif
} else if (strcmp(argv[a], "-m") == 0) {
if (a + 1 >= argc) {
return invalid_args();
Expand Down Expand Up @@ -729,6 +741,29 @@ MP_NOINLINE int main_(int argc, char **argv) {
inspect = true;
}
if (ret == NOTHING_EXECUTED || inspect) {
#if MICROPY_USE_READLINE == 1
if (embed) {
// Run main.py if not told otherwise
if (ret == NOTHING_EXECUTED) {
ret = pyexec_file_if_exists("main.py");
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
}
ret = 0;
for (;;) {
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
if (pyexec_raw_repl() != 0) {
break;
}
} else {
if (pyexec_friendly_repl() != 0) {
break;
}
}
}
} else
#endif
if (isatty(0) || inspect) {
prompt_read_history();
ret = do_repl();
Expand All @@ -738,6 +773,8 @@ MP_NOINLINE int main_(int argc, char **argv) {
}
}

soft_reset_exit:

#if MICROPY_PY_SYS_SETTRACE
MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL;
#endif
Expand Down
1 change: 1 addition & 0 deletions shared/runtime/pyexec.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "shared/readline/readline.h"
#include "shared/runtime/pyexec.h"
#include "genhdr/mpversion.h"
#include "extmod/modplatform.h"

pyexec_mode_kind_t pyexec_mode_kind = PYEXEC_MODE_FRIENDLY_REPL;
int pyexec_system_exit = 0;
Expand Down
0