summaryrefslogtreecommitdiff
path: root/tests/import
AgeCommit message (Collapse)Author
2025-07-24all: Go back to using default ruff quote style.Damien George
Commit dc2fcfcc5511a371ff684f7d7772e7a7b479246d seems to have accidentally changed the ruff quote style to "preserve", instead of keeping it at the default which is "double". Put it back to the default and update relevant .py files with this rule. Signed-off-by: Damien George <damien@micropython.org>
2025-06-23py/runtime: Add support for using __all__ in star import.Yoctopuce dev
When the symbol `__all__` is defined in a module, `mp_import_all()` should import all listed symbols into the global environment, rather than relying on 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 import statement, and will invoke the module's `__getattr__` function if needed. This provides a straightforward solution for fixing star import of modules using a dynamic loader, such as `extmod/asyncio` (see issue #7266). This improvement has been enabled at BASIC_FEATURES level, to avoid impacting devices with limited ressources, for which star import is of little use anyway. Additionally, detailled reporting of errors during `__all__` import has been implemented to match CPython, but this is only enabled when ERROR_REPORTING is set to MICROPY_ERROR_REPORTING_DETAILED. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2024-01-05all: Fix "reuse" and "overridden" spelling mistakes.Damien George
Codespell doesn't pick up "re-used" or "re-uses", and ignores the tests/ directory, so fix these manually. Signed-off-by: Damien George <damien@micropython.org>
2023-06-08tests: Replace umodule with module everywhere.Jim Mussared
This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-05py/builtinimport: Remove partially-loaded modules from sys.modules.David Grayson
Prior to this commit, importing a module that exists but has a syntax error or some other problem that happens at import time would result in a potentially-incomplete module object getting added to sys.modules. Subsequent imports would use that object, resulting in confusing error messages that hide the root cause of the problem. This commit fixes that issue by removing the failed module from sys.modules using the new NLR callback mechanism. Note that it is still important to add the module to sys.modules while the import is happening so that we can support circular imports just like CPython does. Fixes issue #967. Signed-off-by: David Grayson <davidegrayson@gmail.com>
2023-06-01tests/import/import_pkg9.py: Add test for subpackage attribute.Jim Mussared
When foo.bar is imported, bar is added as an attribute to foo. Previously this happened on every import, but should only happen on first import. This verifies the behavior for relative imports and overriding. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01tests/import/builtin_ext.py: Add test for built-in module override.Jim Mussared
This verifies the behavior: - Exact matches of built-ins bypass filesystem. - u-prefix modules can be overridden from the filesystem. - Builtin import can be forced using either u-prefix or sys.path=[]. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-02-02top: Update Python formatting to black "2023 stable style".Jim Mussared
See https://black.readthedocs.io/en/stable/the_black_code_style/index.html Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19py/objmodule: Add support for __dict__.Jim Mussared
This matches class `__dict__`, and is similarly gated on MICROPY_CPYTHON_COMPAT. Unlike class though, because modules's globals are actually dict instances, the result is a mutable dictionary. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-05-30py/builtinimport: Change relative import's ValueError to ImportError.Damien George
Following CPython change, see https://bugs.python.org/issue37444. Signed-off-by: Damien George <damien@micropython.org>
2020-07-26tests: Move .mpy import tests from import/ to micropython/ dir.Damien George
These tests are specific to MicroPython so have a better home in the micropython/ test subdir, and putting them here allows them to be run by all targets, not just those that have access to the local filesystem (eg the unix port). Signed-off-by: Damien George <damien@micropython.org>
2020-03-30tests: Format all Python code with black, except tests in basics subdir.David Lechner
This adds the Python files in the tests/ directory to be formatted with ./tools/codeformat.py. The basics/ subdirectory is excluded for now so we aren't changing too much at once. In a few places `# fmt: off`/`# fmt: on` was used where the code had special formatting for readability or where the test was actually testing the specific formatting.
2019-12-13tests: Add .exp files for basics/parser and import/import_override.Damien George
Because CPython 3.8.0 now produces different output: - basics/parser.py: CPython does not allow '\\\n' as input. - import/import_override: CPython imports _io.
2019-12-12tests/import: Add test for importing viper code with additional flags.Damien George
2019-11-26py/builtinimport: Raise exception on empty module name.Léa Saviot
To prevent a crash returning MP_OBJ_NULL. A test is added for this case.
2019-10-15py/persistentcode: Make .mpy more compact with qstr directly in prelude.Damien George
Instead of encoding 4 zero bytes as placeholders for the simple_name and source_file qstrs, and storing the qstrs after the bytecode, store the qstrs at the location of these 4 bytes. This saves 4 bytes per bytecode function stored in a .mpy file (for example lcd160cr.mpy drops by 232 bytes, 4x 58 functions). And resulting code size is slightly reduced on ports that use this feature.
2019-10-04py/compile: Disallow 'import *' outside module level.Petr Viktorin
This check follows CPython's behaviour, because 'import *' always populates the globals with the imported names, not locals. Since it's safe to do this (doesn't lead to a crash or undefined behaviour) the check is only enabled for MICROPY_CPYTHON_COMPAT. Fixes issue #5121.
2019-10-01py: Rework and compress second part of bytecode prelude.Damien George
This patch compresses the second part of the bytecode prelude which contains the source file name, function name, source-line-number mapping and cell closure information. This part of the prelude now begins with a single varible length unsigned integer which encodes 2 numbers, being the byte-size of the following 2 sections in the header: the "source info section" and the "closure section". After decoding this variable unsigned integer it's possible to skip over one or both of these sections very easily. This scheme saves about 2 bytes for most functions compared to the original format: one in the case that there are no closure cells, and one because padding was eliminated.
2019-10-01py: Compress first part of bytecode prelude.Damien George
The start of the bytecode prelude contains 6 numbers telling the amount of stack needed for the Python values and exceptions, and the signature of the function. Prior to this patch these numbers were all encoded one after the other (2x variable unsigned integers, then 4x bytes), but using so many bytes is unnecessary. An entropy analysis of around 150,000 bytecode functions from the CPython standard library showed that the optimal Shannon coding would need about 7.1 bits on average to encode these 6 numbers, compared to the existing 48 bits. This patch attempts to get close to this optimal value by packing the 6 numbers into a single, varible-length unsigned integer via bit-wise interleaving. The interleaving scheme is chosen to minimise the average number of bytes needed, and at the same time keep the scheme simple enough so it can be implemented without too much overhead in code size or speed. The scheme requires about 10.5 bits on average to store the 6 numbers. As a result most functions which originally took 6 bytes to encode these 6 numbers now need only 1 byte (in 80% of cases).
2019-09-26py/persistentcode: Bump .mpy version to 5.Damien George
The bytecode opcodes have changed (there are more, and they have been reordered).
2019-09-26tests: Update tests for changes to opcode ordering.Damien George
2019-07-31tests: Add tests for overriding builtins.__import__.Damien George
2019-03-08tests/import: Add test for importing x64 native code.Damien George
2019-03-05py/persistentcode: Add a qstr window to save mpy files more efficiently.Damien George
This is an implementation of a sliding qstr window used to reduce the number of qstrs stored in a .mpy file. The window size is configured to 32 entries which takes a fixed 64 bytes (16-bits each) on the C stack when loading/saving a .mpy file. It allows to remember the most recent 32 qstrs so they don't need to be stored again in the .mpy file. The qstr window uses a simple least-recently-used mechanism to discard the least recently used qstr when the window overflows (similar to dictionary compression). This scheme only needs a single pass to save/load the .mpy file. Reduces mpy file size by about 25% with a window size of 32.
2018-11-01tests/import_long_dyn: Test for "import *" of a long dynamic name.Paul Sokolovsky
Such names aren't stored as qstr in module dict, and there was a bug in "import *" handling which assumed any name in a module dict is a qstr.
2018-10-23tests/import: Add .exp file for module_getattr.py to not require Py 3.7.Damien George
2018-10-23py/objmodule: Implement PEP 562's __getattr__ for modules.Paul m. p. P
Configurable via MICROPY_MODULE_GETATTR, disabled by default. Among other things __getattr__ for modules can help to build lazy loading / code unloading at runtime.
2018-06-18tests/import: Add test for importing invalid .mpy file.Damien George
2017-06-28tests/import: Update comment now that uPy raises correct exception.Damien George
2017-06-28tests/import: Add a test for the builtin __import__ function.Damien George
2017-01-16py/builtinimport: Raise ValueError for bad relative import, per CPython.Damien George
2017-01-16tests/import: Improve builtinimport.c test coverage.Rami Ali
2016-12-21tests/import: Add a test which uses ... in from-import statement.Damien George
2016-10-11tests: Improve test coverage of py/compile.c.Damien George
2016-09-30tests/import: Add test for compiling "import a.b as c".Damien George
2015-06-27tests: Add test for relative import without package context.Paul Sokolovsky
2015-02-16tests: Add another testcase for relative imports.Paul Sokolovsky
2014-07-28py: Implement __file__ attribute for modules.Paul Sokolovsky
2014-07-05tests: Rename test scripts, changing - to _ for consistency.Damien George
From now on, all new tests must use underscore. Addresses issue #727.
2014-04-17tests: Move gen_context to import tests, because it relies on import.Damien George
2014-04-17tests: Split out those tests requiring float and import.Damien George
Tests in basics (which should probably be renamed to core) should not rely on float, or import any non-built-in files. This way these tests can be run when those features are not available. All test in basics now pass on the pyboard using stmhal port, except for string-repr which has some issues with character hex printing.