shared/runtime/pyexec: Add __file__ support for frozen modules.#18221
Open
andrewleech wants to merge 2 commits into
Open
shared/runtime/pyexec: Add __file__ support for frozen modules.#18221andrewleech wants to merge 2 commits into
andrewleech wants to merge 2 commits into
Conversation
addecac to
1768350
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
Code size report: |
This improves repl usage consistency across ports. Only enabled when MICROPY_USE_READLINE == 1 (default). Signed-off-by: Andrew Leech <[email protected]>
1768350 to
219fa60
Compare
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]>
219fa60 to
d173e99
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR was split off from #12802 for separate review.
Adds
__file__attribute support for frozen MPY modules executed viapyexec_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 callingparse_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.pyandmain.pymodules to access__file__for path-relative operations and debugging.Testing
Tested with existing test infrastructure:
tests/ports/unix/extra_coverage.pyvalidatesfrozentest.__file__returns"frozentest.py"./build-coverage/micropython -c "import frozentest; print(frozentest.__file__)"outputsfrozentest.pyTrade-offs and Alternatives
Initial implementation added a
frozen_module_nameparameter toparse_compile_execute(), requiring NULL arguments at all 10 call sites. This was refactored to set__file__directly inpyexec_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.