-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
py/builtinimport: support relative import in custom __import__ callbacks #6665
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
py/builtinimport: support relative import in custom __import__ callbacks #6665
Conversation
|
+1 |
…-name fix name for macOS mpy-cross universal build
This comment was marked as outdated.
This comment was marked as outdated.
|
@zsquareplusc sorry no one took a look at this when it was opened. It actually looks really good, it fixes a missing piece of the import machinery. Would you be able to rebase this on the latest master? If not, I can do that. |
cc31c03 to
2d50d42
Compare
|
I have rebased this on latest master. It was a bit tricky due to the many merge commits, but I managed to retain the original patch. |
|
Code size report: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #6665 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 171 171
Lines 22294 22298 +4
=======================================
+ Hits 21933 21937 +4
Misses 361 361 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
The globals need to be forwarded from the callers context.
globals() needs to be provided in case __import__ is a Python function
Following CPython behaviour. Signed-off-by: Damien George <damien@micropython.org>
Signed-off-by: Damien George <damien@micropython.org>
2d50d42 to
bd758b8
Compare
|
I adjusted the code here so it better matches CPython (only check the globals argument if level>0). And removed the error message to reduce code size (this error message telling that globals must be a dict would rarely be hit in the real world, so not worth spending bytes on it). Also adjusted the tests:
|
|
Only 2 weeks ago I prepared a rebased branch of this PR. However I encountered open questions. And my goal was just making custom Now I compared my branch with this PR. This PR has:
My branch is well tested since years. And the differences of this PR look good to me. |
|
Thanks @oliver-joos for the feedback. |
The globals need to be forwarded from the callers context.
This pull request adds a test with relative import and override as well as a fix.
I've tried to use a custom python function for import to make some performance measurements (actually tracking approximate memory usage). There i noticed that's impossible for a python handler function to support relative imports (
level> 0). This is due to the globals thatmp_builtin___import__is getting are from the custom callback instead of the module whereimport/__import__was called. So the lookup of__path__gives the wrong value.The solution is that
mp_import_nameinruntime.cgets the globals and passes them along in de parameter that already exists for this purpose (i see CPython is doing this too).mp_builtin___import__uses theglobalsparameter or falls back to the old behavior ifNoneis passed or fewer positional args are provided.