diff options
| author | Andrew Leech <andrew.leech@planetinnovation.com.au> | 2025-05-26 19:28:02 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-11-22 00:06:58 +1100 |
| commit | 7b3a0fc6b7fe935be008d2518eb480b75a8809d1 (patch) | |
| tree | a72cb0fadb897ca5733e6e712f838b934cbdf04f | |
| parent | 3b2b8dd53826ef3b7ef755013e1ecdccd368cb8c (diff) | |
shared/runtime/pyexec: Provide support for compile-only mode.
When `MICROPY_PYEXEC_COMPILE_ONLY` is enabled and the global
`mp_compile_only` is True, code is compiled but not executed.
Also add comprehensive tests for compile-only functionality covering both
successful compilation and syntax error detection.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
| -rw-r--r-- | pyproject.toml | 8 | ||||
| -rw-r--r-- | shared/runtime/pyexec.c | 8 | ||||
| -rw-r--r-- | tests/cmdline/cmd_compile_only.py | 13 | ||||
| -rw-r--r-- | tests/cmdline/cmd_compile_only.py.exp | 1 | ||||
| -rw-r--r-- | tests/cmdline/cmd_compile_only_error.py | 6 | ||||
| -rw-r--r-- | tests/cmdline/cmd_compile_only_error.py.exp | 1 |
6 files changed, 35 insertions, 2 deletions
diff --git a/pyproject.toml b/pyproject.toml index 041dd7419..cdc79bb8a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ target-version = "py37" [tool.ruff.lint] exclude = [ # Ruff finds Python SyntaxError in these files + "tests/cmdline/cmd_compile_only_error.py", "tests/cmdline/repl_autocomplete.py", "tests/cmdline/repl_autocomplete_underscore.py", "tests/cmdline/repl_autoindent.py", @@ -69,4 +70,9 @@ mccabe.max-complexity = 40 # basics: needs careful attention before applying automatic formatting # repl_: not real python files # viper_args: uses f(*) -exclude = ["tests/basics/*.py", "tests/*/repl_*.py", "tests/micropython/viper_args.py"] +exclude = [ + "tests/basics/*.py", + "tests/*/repl_*.py", + "tests/cmdline/cmd_compile_only_error.py", + "tests/micropython/viper_args.py", +] diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c index 619636783..910b39c08 100644 --- a/shared/runtime/pyexec.c +++ b/shared/runtime/pyexec.c @@ -130,7 +130,12 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input #if MICROPY_REPL_INFO start = mp_hal_ticks_ms(); #endif - mp_call_function_0(module_fun); + #if MICROPY_PYEXEC_COMPILE_ONLY + if (!mp_compile_only) + #endif + { + mp_call_function_0(module_fun); + } mp_hal_set_interrupt_char(-1); // disable interrupt mp_handle_pending(true); // handle any pending exceptions (and any callbacks) nlr_pop(); @@ -706,6 +711,7 @@ int pyexec_file(const char *filename) { return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_FILENAME); } + int pyexec_file_if_exists(const char *filename) { #if MICROPY_MODULE_FROZEN if (mp_find_frozen_module(filename, NULL, NULL) == MP_IMPORT_STAT_FILE) { diff --git a/tests/cmdline/cmd_compile_only.py b/tests/cmdline/cmd_compile_only.py new file mode 100644 index 000000000..89964c1b5 --- /dev/null +++ b/tests/cmdline/cmd_compile_only.py @@ -0,0 +1,13 @@ +# cmdline: -X compile-only +# test compile-only functionality +print("This should not be printed") +x = 1 + 2 + + +def hello(): + return "world" + + +class TestClass: + def __init__(self): + self.value = 42 diff --git a/tests/cmdline/cmd_compile_only.py.exp b/tests/cmdline/cmd_compile_only.py.exp new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/tests/cmdline/cmd_compile_only.py.exp @@ -0,0 +1 @@ + diff --git a/tests/cmdline/cmd_compile_only_error.py b/tests/cmdline/cmd_compile_only_error.py new file mode 100644 index 000000000..326937a5c --- /dev/null +++ b/tests/cmdline/cmd_compile_only_error.py @@ -0,0 +1,6 @@ +# cmdline: -X compile-only +# test compile-only with syntax error +print("This should not be printed") +def broken_syntax( + # Missing closing parenthesis + return "error" diff --git a/tests/cmdline/cmd_compile_only_error.py.exp b/tests/cmdline/cmd_compile_only_error.py.exp new file mode 100644 index 000000000..3911f71d6 --- /dev/null +++ b/tests/cmdline/cmd_compile_only_error.py.exp @@ -0,0 +1 @@ +CRASH
\ No newline at end of file |
