Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

py/runtime.c: Add support for using __all__ in star import.#17331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged

Conversation

yoctopuce
Copy link
Contributor

Summary

If a module defines the symbol__all__, that definition should be used when doing a wildcard import: all listed symbols and no others should be added to the parent global environment. This is the standard in CPython.

This pull requests fixes a longtimeFIXME inmp_parse_all to implement this behaviour.

Each item is loaded in the same way as if it would be an explicit import statement, including invoking the the module's__getattr__ function if needed. This provides a straightforward solution for fixing star import of modules using a dynamic loader, such asextmod/asyncio (see issue#7266, dated 2022).

Testing

The corresponding test cases have been added totests/import, including the default behaviour when__all__ is not defined.

As star imports are not expected to be useful on systems with very limited ressources, this improvement is only enabled for ports at BASIC_FEATURES level. In order to skip the new test cases on builds at lower feature level, the test file checks for the availability of another feature enabled at the same level (next's 2nd argument).

Trade-offs and Alternatives

This improvement will slightly increase the code size, for the sake of CPython compatibility.

On the other hand, limiting imports to a subset of symbols rather than importing most of them in the parent globals will be reduce RAM usage. The feature also fixes a longstanding issue with dynamic loading of modules, which is another method used to reduce RAM usage.

@yoctopuceyoctopuceforce-pushed theMICROPY_ENABLE_MODULE_ALL branch from88a3d7b tofa6f6c0CompareMay 20, 2025 16:40
@codecovCodecov
Copy link

codecovbot commentedMay 20, 2025
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.57%. Comparing base(35f15cf) to head(66c0148).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@##           master   #17331   +/-   ##=======================================  Coverage   98.56%   98.57%           =======================================  Files         169      169             Lines       21950    21968   +18     =======================================+ Hits        21636    21654   +18  Misses        314      314

☔ 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-actionsGitHub Actions
Copy link

github-actionsbot commentedMay 20, 2025
edited
Loading

Code size report:

   bare-arm:    +0 +0.000% minimal x86:    +0 +0.000%    unix x64:  +368 +0.043% standard      stm32:   +88 +0.022% PYBV10     mimxrt:  +176 +0.047% TEENSY40        rp2:  +112 +0.012% RPI_PICO_W       samd:   +68 +0.025% ADAFRUIT_ITSYBITSY_M4_EXPRESS  qemu rv32:   +91 +0.020% VIRT_RV32

Copy link
Contributor

@dlechdlech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

On the other hand, limiting imports to a subset of symbols rather than importing most of them in the parent globals will be reduce RAM usage

If the module containing__all__ has to be loaded in RAM (e.g. it is a .py file), then wouldn't RAM usage actually increase because of the extra list (unless there are significantly more "private" members not starting with "_" than public)?

(Not saying we shouldn't accept this, just want to make sure the details are clear.)

@dpgeorgedpgeorge added the py-coreRelates to py/ directory in source labelMay 21, 2025
@yoctopuceyoctopuceforce-pushed theMICROPY_ENABLE_MODULE_ALL branch fromfa6f6c0 to8ddf09cCompareMay 21, 2025 05:26
@yoctopuce
Copy link
ContributorAuthor

On the other hand, limiting imports to a subset of symbols rather than importing most of them in the parent globals will be reduce RAM usage

If the module containing__all__ has to be loaded in RAM (e.g. it is a .py file), then wouldn't RAM usage actually increase because of the extra list (unless there are significantly more "private" members not starting with "_" than public)?

(Not saying we shouldn't accept this, just want to make sure the details are clear.)

It will of course depend on the cases. You are correct in saying that for simple modules, default import is likely to work as well. However for more complex modules that use imports, those imported symbols would typically be unnecessarily re-exported in the parent global dictionary by a star import.

@yoctopuceyoctopuceforce-pushed theMICROPY_ENABLE_MODULE_ALL branch 2 times, most recently from30ceed9 toc3527deCompareMay 21, 2025 06:10
@yoctopuceyoctopuceforce-pushed theMICROPY_ENABLE_MODULE_ALL branch fromc3527de toaaea1e5CompareMay 30, 2025 11:28
@yoctopuce
Copy link
ContributorAuthor

(force-pushed today after rebased to head revision)

@dpgeorge
Copy link
Member

Thanks for this. The "TODO" for this feature has definitely been there a long time, more than 11 years!

It would be nice to see this take up less code size, but I can't see a way to reduce it. Well, at least it's guarded by a config option, and I agree "basic" level is a good level to enable it at.

@yoctopuceyoctopuceforce-pushed theMICROPY_ENABLE_MODULE_ALL branch 2 times, most recently froma68b9cf to93d2da2CompareJune 17, 2025 18:17
When the symbol `__all__` is defined in a module, `mp_import_all()` shouldimport all listed symbols into the global environment, rather than relyingon the underscore-is-private default.  This is the standard in CPython.Each item is loaded in the same way as if it would be an explicit importstatement, and will invoke the module's `__getattr__` function if needed.This provides a straightforward solution for fixing star import of modulesusing a dynamic loader, such as `extmod/asyncio` (see issuemicropython#7266).This improvement has been enabled at BASIC_FEATURES level, to avoidimpacting devices with limited ressources, for which star import is oflittle use anyway.Additionally, detailled reporting of errors during `__all__` import hasbeen implemented to match CPython, but this is only enabled whenERROR_REPORTING is set to MICROPY_ERROR_REPORTING_DETAILED.Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
@dpgeorgedpgeorgeforce-pushed theMICROPY_ENABLE_MODULE_ALL branch from93d2da2 to66c0148CompareJune 23, 2025 06:07
@dpgeorgedpgeorge merged commit66c0148 intomicropython:masterJun 23, 2025
68 checks passed
@yoctopuceyoctopuce deleted the MICROPY_ENABLE_MODULE_ALL branchJune 23, 2025 14:52
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@dlechdlechdlech left review comments

@dpgeorgedpgeorgedpgeorge approved these changes

Assignees
No one assigned
Labels
py-coreRelates to py/ directory in source
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

3 participants
@yoctopuce@dpgeorge@dlech

[8]ページ先頭

©2009-2025 Movatter.jp