summaryrefslogtreecommitdiff
path: root/tests/misc
AgeCommit message (Collapse)Author
2025-07-24py/obj: Fix REPR_C bias toward zero.Yoctopuce dev
Current implementation of REPR_C works by clearing the two lower bits of the mantissa to zero. As this happens after each floating point operation, this tends to bias floating point numbers towards zero, causing decimals like .9997 instead of rounded numbers. This is visible in test cases involving repeated computations, such as `tests/misc/rge_sm.py` for instance. The suggested fix fills in the missing bits by copying the previous two bits. Although this cannot recreate missing information, it fixes the bias by inserting plausible values for the lost bits, at a relatively low cost. Some float tests involving irrational numbers have to be softened in case of REPR_C, as the 30 bits are not always enough to fulfill the expectations of the original test, and the change may randomly affect the last digits. Such cases have been made explicit by testing for REPR_C or by adding a clear comment. The perf_test fft code was also missing a call to round() before casting a log_2 operation to int, which was causing a failure due to a last-decimal change. Signed-off-by: Yoctopuce dev <dev@yoctopuce.com>
2025-07-06tests/misc: Improve test coverage of py/profile.c.Jeff Epler
Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-06-05tests/run-tests.py: Automatically skip tests that are too large.Damien George
Some tests are just too big for targets that don't have much heap memory, eg `tests/extmod/vfs_rom.py`. Other tests are too large because the target doesn't have enough IRAM for native code, eg esp8266 running `tests/micropython/viper_args.py`. Previously, such tests were explicitly skipped on targets known to have little memory, eg esp8266. But this doesn't scale to multiple targets, nor to more and more tests which are too large. This commit addresses that by adding logic to the test runner so it can automatically skip tests when they don't fit in the target's memory. It does this by prepending a `print('START TEST')` to every test, and if a `MemoryError` occurs before that line is printed then the test was too big. This works for standard tests, tests that go via .mpy files, and tests that run in native emitter mode via .mpy files. For tests that are too big, it prints `lrge <test name>` on the output, and at the end prints them on a separate line of skipped tests so they can be distinguished. They are also distinguished in the `_result.json` file as a skipped test with reason "too large". Signed-off-by: Damien George <damien@micropython.org>
2025-03-02py/objstr: Support tuples and start/end args in startswith and endswith.Glenn Moloney
This change allows tuples to be passed as the prefix/suffix argument to the `str.startswith()` and `str.endswith()` methods. The methods will return `True` if the string starts/ends with any of the prefixes/suffixes in the tuple. Also adds full support for the `start` and `end` arguments to both methods for compatibility with CPython. Tests have been updated for the new behaviour. Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
2024-11-28tests/misc/sys_settrace_features.py: Add note about CPython 3.12 issue.Angus Gratton
CPython 3.12 has a documented issue with settrace for opcodes, apparently due to PEP 669. "This behavior will be changed back in 3.13 to be consistent with previous versions." No easy way to make the test pass on CPython 3.12, but at least this helps signal what the problem is to anyone who runs into a failure. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-07-25py/objtype: Avoid crash on calling members of uninitialized native type.Laurens Valk
When subclassing a native type, calling native members in `__init__` before `super().__init__()` has been called could cause a crash. In this situation, `self` in `mp_convert_member_lookup` is the `native_base_init_wrapper_obj`. The check added in this commit ensures that an `AttributeError` is raised before this happens, which is consistent with other failed lookups. Also fix a typo in a related comment. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2024-07-25examples/usercmodule/cexample: Add more advanced native class.Laurens Valk
This adds a separate `AdvancedTimer` class that demonstrates a few more advanced concepts usch as custom handlers for printing and attributes. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2023-11-03all: Update Python formatting to ruff-format.Jim Mussared
This updates a small number of files that change with ruff-format's (vs black's) rules. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-08-30py/profile: Remove the requirement to disable MICROPY_COMP_CONST.Jim Mussared
The only reason that const had to be disabled was to make the test output match CPython when const was involved. Instead, this commit fixes the test to handle the lines where const is used. Also: - remove the special handling for MICROPY_PERSISTENT_CODE_SAVE in unix/mpconfigport.h, and make this automatic. - move the check for MICROPY_PERSISTENT_CODE_SAVE to where it's used (like we do for other similar checks) and add a comment explaining it. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-08-30tests/misc/sys_settrace_features.py: Fix to run on newer CPython.Jim Mussared
This test was failing on CPython 3.11 as it now emits `0` as the line number for the "call" event corresponding to import, where as in 3.6 it had `1` as the line number. We maintain the old behavior, but in order to make this test pass on both CPython versions, the trace handler now converts the `0` to a `1`. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
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-04-27all: Fix spelling mistakes based on codespell check.Damien George
Signed-off-by: Damien George <damien@micropython.org>
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-11-25tests/misc/cexample_class: Fix timing sensitivity.Laurens Valk
This test could occasionally fail because some operations take longer than expected. This relaxes the timing constraints and defers printing until the very end. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-11-25tests/misc/cexample_module: Test class presence.Laurens Valk
Now that the Timer class has been merged in a separate pull request, this can be added to the module test too. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-11-23examples/usercmodule: Add example of a native C class.Laurens Valk
This shows how ports can add their own custom types/classes. It is part of the unix coverage build, so we can use it for tests too. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-11-23tests/misc: Add test for cexample module.Laurens Valk
This also moves the existing test for cexample.add_ints originally done in extra_coverage. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-02-02all: Update Python formatting to latest Black version 22.1.0.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2020-12-18tests/misc/sys_settrace_features.py: Fix running with non-dflt encoding.stijn
Notably git-cmd which comes with git installations on Windows alters the encoding resulting in CPython tracing encodings/cp1252.py calls.
2020-12-18tests/misc/sys_settrace: Make test output independent of invoked path.stijn
The original logic of reducing a full path to a relative one assumes "tests/misc" is in the filename which is limited in usage: it never works for CPython on Windows since that will use a backslash as path separator, and also won't work when the filename is a path not relative to the tests directory which happens for example in the common case of running "./run-tests -d misc". Fix all cases by printing only the bare filename, which requires them all to start with sys_settrace_ hence the renaming.
2020-12-14tests/misc/sys_settrace_features.py: Ignore CPython zipimport traces.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2020-09-04all: Rename "sys" module to "usys".stijn
This is consistent with the other 'micro' modules and allows implementing additional features in Python via e.g. micropython-lib's sys. Note this is a breaking change (not backwards compatible) for ports which do not enable weak links, as "import sys" must now be replaced with "import usys".
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-10-22tests: Rename "array" module to "uarray".Damien George
2019-08-30tests: Add tests for sys.settrace feature.Milan Rossa
2019-08-28py/vm: Don't add traceback info for exceptions that are re-raised.Damien George
With this patch exceptions that are re-raised have improved tracebacks (less confusing, match CPython), and it makes re-raise slightly more efficient (in time and RAM) because they no longer need to add a traceback. Also general VM performance is not measurably affected. Partially fixes issue #2928.
2019-08-28py/vm: Don't add traceback info for exc's propagated through a finally.Damien George
With this patch exception tracebacks that go through a finally are improved (less confusing, match CPython), and it makes finally's slightly more efficient (in time and RAM) because they no longer need to add a traceback. Partially fixes issue #2928.
2019-08-15tests/misc/sys_atexit: Add test for new sys.atexit feature.Milan Rossa
2018-10-22tests: Make bytes/str.count() tests skippable.Paul Sokolovsky
2018-09-20py: Shorten error messages by using contractions and some rewording.Damien George
2018-08-17tests: Modify tests that print repr of an exception with 1 arg.Damien George
In Python 3.7 the behaviour of repr() of an exception with one argument changed: it no longer prints a trailing comma in the argument list. See https://bugs.python.org/issue30399 This patch modifies tests that rely on this behaviour to not rely on it. And the python34.py test is updated to include a test for this behaviour with a .exp file.
2018-08-04tests: Make tests work on targets without float support.Ayke van Laethem
2018-06-20tests: Add tests using "file" argument in print and sys.print_exception.Damien George
2018-06-08py/objtype: Optimise instance get/set/del by skipping special accessors.Damien George
This patch is a code optimisation, trading text bytes for speed. On pyboard it's an increase of 0.06% in code size for a gain (in pystone performance) of roughly 6.5%. The patch optimises load/store/delete of attributes in user defined classes by not looking up special accessors (@property, __get__, __delete__, __set__, __setattr__ and __getattr_) if they are guaranteed not to exist in the class. Currently, if you do my_obj.foo() then the runtime has to do a few checks to see if foo is a property or has __get__, and if so delegate the call. And for stores things like my_obj.foo = 1 has to first check if foo is a property or has __set__ defined on it. Doing all those checks each and every time the attribute is accessed has a performance penalty. This patch eliminates all those checks for cases when it's guaranteed that the checks will always fail, ie no attributes are properties nor have any special accessor methods defined on them. To make this guarantee it checks all attributes of a user-defined class when it is first created. If any of the attributes of the user class are properties or have special accessors, or any of the base classes of the user class have them, then it sets a flag in the class to indicate that special accessors must be checked for. Then in the load/store/delete code it checks this flag to see if it can take the shortcut and optimise the lookup. It's an optimisation that's pretty widely applicable because it improves lookup performance for all methods of user defined classes, and stores of attributes, at least for those that don't have special accessors. And, it allows to enable descriptors with minimal additional runtime overhead if they are not used for a particular user class. There is one restriction on dynamic class creation that has been introduced by this patch: a user-defined class cannot go from zero special accessors to one special accessor (or more) after that class has been subclassed. If the script attempts this an AttributeError is raised (see addition to tests/misc/non_compliant.py for an example of this case). The cost in code space bytes for the optimisation in this patch is: unix x64: +528 unix nanbox: +508 stm32: +192 cc3200: +200 esp8266: +332 esp32: +244 Performance tests that were done: - on unix x86-64, pystone improved by about 5% - on pyboard, pystone improved by about 6.5%, from 1683 up to 1794 - on pyboard, bm_chaos (from CPython benchmark suite) improved by about 5% - on esp32, pystone improved by about 30% (but there are caching effects) - on esp32, bm_chaos improved by about 11%
2018-04-10tests: Move recursive tests to the tests/stress/ subdir.Damien George
Keeping all the stress related tests in one place makes it easier to stress-test a given port, and to also not run such tests on ports that can't handle them.
2017-12-14tests: Add tests to improve coverage of py/objtype.c.Damien George
2017-10-03py/objset: Check that RHS of a binary op is a set/frozenset.Damien George
CPython docs explicitly state that the RHS of a set/frozenset binary op must be a set to prevent user errors. It also preserves commutativity of the ops, eg: "abc" & set() is a TypeError, and so should be set() & "abc". This change actually decreases unix (x64) code by 160 bytes; it increases stm32 by 4 bytes and esp8266 by 28 bytes (but previous patch already introduced a much large saving).
2017-06-10tests: Convert remaining "sys.exit()" to "raise SystemExit".Paul Sokolovsky
2017-03-11tests/misc/: Make few tests skippable.Paul Sokolovsky
2017-02-10tests/misc: Add test for line number printing with large bytecode chunk.Damien George
2017-01-19tests/misc/non_compliant: Add test for inability to assign func attrs.Damien George
2016-12-21tests: Add tests to improve coverage of objarray.c.Rami Ali
2016-10-17tests: Improve coverage of array, range, dict, slice, exc, unicode.Damien George
2016-10-11tests: Improve test coverage of py/compile.c.Damien George
2016-10-07tests: Improve coverage of struct with test for non-compliant behaviour.Damien George
2016-08-15tests/misc/non_compliant: Add tests to improve coverage testing.Damien George
2016-06-04tests/recursive_iternext: Clang/Linux is even more stack-frugal than MacOS.Paul Sokolovsky
2016-06-03tests/misc/recursive_iternext: Provide more fine-grained selection of N.Damien George
To work on a variety of ports the selection of N is very specific.
2016-06-03tests/misc/recursive_iternext.py: Increase depth N from 1000 to 2000.Damien George
This makes the test reliably overflow the recursion limit (which is the correct behaviour) on Mac OS X.
2016-05-02tests: Make "io" modules fixes for CPython compatibility.Paul Sokolovsky
Previously, "import _io" worked on both CPython and MicroPython (essentially by a chance on CPython, as there's not guarantee that its contents will stay the same across versions), but as the module was renamed to uio, need to use more robust import sequence for compatibility.