Skip to content

shared/runtime/pyexec: Add __file__ support for frozen modules.#18221

Open
andrewleech wants to merge 2 commits into
micropython:masterfrom
andrewleech:pyexec_frozen___FILE__
Open

shared/runtime/pyexec: Add __file__ support for frozen modules.#18221
andrewleech wants to merge 2 commits into
micropython:masterfrom
andrewleech:pyexec_frozen___FILE__

Conversation

@andrewleech
Copy link
Copy Markdown
Contributor

Summary

This PR was split off from #12802 for separate review.

Adds __file__ attribute support for frozen MPY modules executed via pyexec_frozen_module(). Previously, only lexer-compiled modules (frozen STR and regular file execution) had __file__ set. Frozen MPY modules bypass the lexer, so they require special handling.

The implementation sets __file__ in the global namespace immediately before calling parse_compile_execute() for frozen MPY modules. When the module context is created, mp_globals_get() captures these globals including the __file__ attribute.

This enables frozen boot.py and main.py modules to access __file__ for path-relative operations and debugging.

Testing

Tested with existing test infrastructure:

  • tests/ports/unix/extra_coverage.py validates frozentest.__file__ returns "frozentest.py"
  • Built and ran unix coverage variant: 1053 tests passed
  • Manual testing: ./build-coverage/micropython -c "import frozentest; print(frozentest.__file__)" outputs frozentest.py

Trade-offs and Alternatives

Initial implementation added a frozen_module_name parameter to parse_compile_execute(), requiring NULL arguments at all 10 call sites. This was refactored to set __file__ directly in pyexec_frozen_module() before the function call, avoiding parameter pollution and reducing code size impact to just 4 lines added.


Note: This PR is stacked on top of #12802. The commit history will show previous commits from that PR, but only the final commit (shared/runtime/pyexec: Add __file__ support for frozen modules.) is part of this review.

@andrewleech andrewleech force-pushed the pyexec_frozen___FILE__ branch from addecac to 1768350 Compare October 7, 2025 05:40
@codecov
Copy link
Copy Markdown

codecov Bot commented Oct 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.46%. Comparing base (2dc2e30) to head (d173e99).

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #18221   +/-   ##
=======================================
  Coverage   98.46%   98.46%           
=======================================
  Files         176      176           
  Lines       22784    22784           
=======================================
  Hits        22435    22435           
  Misses        349      349           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Oct 7, 2025

Code size report:

Reference:  esp32/boards/SEEED_XIAO_ESP32C6: Add new XIAO board definition. [2dc2e30]
Comparison: shared/runtime/pyexec: Add __file__ support for frozen modules. [merge of d173e99]
  mpy-cross:    +0 +0.000% 
   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:   +16 +0.002% standard
      stm32:   +24 +0.006% PYBV10
      esp32:   -16 -0.001% ESP32_GENERIC
     mimxrt:   +24 +0.006% TEENSY40
        rp2:   +24 +0.003% RPI_PICO_W
       samd:   +24 +0.009% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Oct 11, 2025
This improves repl usage consistency across ports.
Only enabled when MICROPY_USE_READLINE == 1 (default).

Signed-off-by: Andrew Leech <[email protected]>
@andrewleech andrewleech force-pushed the pyexec_frozen___FILE__ branch from 1768350 to 219fa60 Compare March 26, 2026 21:02
Set __file__ attribute for frozen boot.py/main.py modules during
execution. Uses temporary storage to provide the module name to
frozen MPY modules which bypass normal lexer-based __file__ setting.

Signed-off-by: Andrew Leech <[email protected]>
@andrewleech andrewleech force-pushed the pyexec_frozen___FILE__ branch from 219fa60 to d173e99 Compare March 27, 2026 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants