Skip to content

Commit 7b3a0fc

Browse files
pi-anldpgeorge
authored andcommitted
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>
1 parent 3b2b8dd commit 7b3a0fc

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

pyproject.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ target-version = "py37"
3030

3131
[tool.ruff.lint]
3232
exclude = [ # Ruff finds Python SyntaxError in these files
33+
"tests/cmdline/cmd_compile_only_error.py",
3334
"tests/cmdline/repl_autocomplete.py",
3435
"tests/cmdline/repl_autocomplete_underscore.py",
3536
"tests/cmdline/repl_autoindent.py",
@@ -69,4 +70,9 @@ mccabe.max-complexity = 40
6970
# basics: needs careful attention before applying automatic formatting
7071
# repl_: not real python files
7172
# viper_args: uses f(*)
72-
exclude = ["tests/basics/*.py", "tests/*/repl_*.py", "tests/micropython/viper_args.py"]
73+
exclude = [
74+
"tests/basics/*.py",
75+
"tests/*/repl_*.py",
76+
"tests/cmdline/cmd_compile_only_error.py",
77+
"tests/micropython/viper_args.py",
78+
]

shared/runtime/pyexec.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input
130130
#if MICROPY_REPL_INFO
131131
start = mp_hal_ticks_ms();
132132
#endif
133-
mp_call_function_0(module_fun);
133+
#if MICROPY_PYEXEC_COMPILE_ONLY
134+
if (!mp_compile_only)
135+
#endif
136+
{
137+
mp_call_function_0(module_fun);
138+
}
134139
mp_hal_set_interrupt_char(-1); // disable interrupt
135140
mp_handle_pending(true); // handle any pending exceptions (and any callbacks)
136141
nlr_pop();
@@ -706,6 +711,7 @@ int pyexec_file(const char *filename) {
706711
return parse_compile_execute(filename, MP_PARSE_FILE_INPUT, EXEC_FLAG_SOURCE_IS_FILENAME);
707712
}
708713

714+
709715
int pyexec_file_if_exists(const char *filename) {
710716
#if MICROPY_MODULE_FROZEN
711717
if (mp_find_frozen_module(filename, NULL, NULL) == MP_IMPORT_STAT_FILE) {

tests/cmdline/cmd_compile_only.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# cmdline: -X compile-only
2+
# test compile-only functionality
3+
print("This should not be printed")
4+
x = 1 + 2
5+
6+
7+
def hello():
8+
return "world"
9+
10+
11+
class TestClass:
12+
def __init__(self):
13+
self.value = 42
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# cmdline: -X compile-only
2+
# test compile-only with syntax error
3+
print("This should not be printed")
4+
def broken_syntax(
5+
# Missing closing parenthesis
6+
return "error"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CRASH

0 commit comments

Comments
 (0)