Skip to content

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

Open
wants to merge 35 commits into
base: master
Choose a base branch
from

Conversation

zsquareplusc
Copy link
Contributor

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 that mp_builtin___import__ is getting are from the custom callback instead of the module where import/__import__ was called. So the lookup of __path__ gives the wrong value.

The solution is that mp_import_name in runtime.c gets 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 the globals parameter or falls back to the old behavior if None is passed or fewer positional args are provided.

The globals need to be forwarded from the callers context.
@hoihu
Copy link
Contributor

hoihu commented Nov 30, 2020

+1

zsquareplusc and others added 28 commits November 30, 2020 17:53
globals() needs to be provided in case __import__ is a Python function
According to documentation time() has a precision of at least 1 second. This test runs for 2.5 seconds and calls all utime functions every 100ms. Then it checks if they returned enough different results. All functions with sub-second precision will return ~25 results. This test passes with 15 results or more. Functions that do not exist are skipped silently.
With MICROPY_FLOAT_IMPL_FLOAT the results of utime.time(), gmtime() and localtime() change only every 129 seconds. As one consequence tests/extmod/vfs_lfs_mtime.py will fail on a unix port with LFS support.

With this patch these functions only return floats if MICROPY_FLOAT_IMPL_DOUBLE is used. Otherwise they return integers.
added support for the separators keyword argument in ujson's dump and
dumps.

added keyword argument indent, only accepts indent=None.
indent was only added because it is connected to separators.
@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Nov 29, 2021
Oliver Joos and others added 5 commits March 12, 2022 20:40
The globals need to be forwarded from the callers context.
globals() needs to be provided in case __import__ is a Python function
Support relative import in custom __import__ callback
tannewt added a commit to tannewt/circuitpython that referenced this pull request Jul 30, 2022
…-name

fix name for macOS mpy-cross universal build
@projectgus
Copy link
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

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.

6 participants