diff options
| author | Andrew Leech <andrew.leech@planetinnovation.com.au> | 2025-05-26 19:28:40 +1000 |
|---|---|---|
| committer | Damien George <damien@micropython.org> | 2025-11-22 00:01:28 +1100 |
| commit | 3b2b8dd53826ef3b7ef755013e1ecdccd368cb8c (patch) | |
| tree | 97acdba6fe6447393401338dd87e450769e3bce1 /shared/runtime/pyexec.c | |
| parent | 5b3c928f53a0f0e2a1d6178569e594dbe9cf8f92 (diff) | |
shared/runtime/pyexec: Set __file__ for file input when enabled.
When `MICROPY_MODULE___FILE__` is enabled and parsing file input, set the
global `__file__` variable to the source filename. This matches the
behavior of the unix port and provides the current filename to the
executing script.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Diffstat (limited to 'shared/runtime/pyexec.c')
| -rw-r--r-- | shared/runtime/pyexec.c | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/shared/runtime/pyexec.c b/shared/runtime/pyexec.c index 237a6f9df..619636783 100644 --- a/shared/runtime/pyexec.c +++ b/shared/runtime/pyexec.c @@ -103,6 +103,11 @@ static int parse_compile_execute(const void *source, mp_parse_input_kind_t input } // source is a lexer, parse and compile the script qstr source_name = lex->source_name; + #if MICROPY_MODULE___FILE__ + if (input_kind == MP_PARSE_FILE_INPUT) { + mp_store_global(MP_QSTR___file__, MP_OBJ_NEW_QSTR(source_name)); + } + #endif mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); #if defined(MICROPY_UNIX_COVERAGE) // allow to print the parse tree in the coverage build |
