summaryrefslogtreecommitdiffstatshomepage
path: root/tests
Commit message (Collapse)AuthorAge
* rp2: Fix recursive atomic sections when core1 is active.Angus Gratton2024-06-25
| | | | | | | | | | | | | | | | | | | | mp_thread_begin_atomic_section() is expected to be recursive (i.e. for nested machine.disable_irq() calls, or if Python code calls disable_irq() and then the Python runtime calls mp_handle_pending() which also enters an atomic section to check the scheduler state). On rp2 when not using core1 the atomic sections are recursive. However when core1 was active (i.e. _thread) then there was a bug that caused the core to live-lock if an atomic section recursed. Adds a test case specifically for mutual exclusion and recursive atomic sections when using two threads. Without this fix the test immediately hangs on rp2. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/objint: Fix int.to_bytes() buffer size checks.Angus Gratton2024-06-24
| | | | | | | | | | | | | | | | | | | | | | | | Fixes and improvements to `int.to_bytes()` are: - No longer overflows if byte size is 0 (closes #13041). - Raises OverflowError in any case where number won't fit into byte length (now matches CPython, previously MicroPython would return a truncated bytes object). - Document that `micropython int.to_bytes()` doesn't implement the optional signed kwarg, but will behave as if `signed=True` when the integer is negative (this is the current behaviour). Add tests for this also. Requires changes for small ints, MPZ large ints, and "long long" large ints. Adds a new set of unit tests for ints between 32 and 64 bits to increase coverage of "long long" large ints, which are otherwise untested. Tested on unix port (64 bit small ints, MPZ long ints) and Zephyr STM32WB board (32 bit small ints, long long large ints). This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* tests/basics: Add tests to test repeated throw into the same generator.Damien George2024-06-21
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/emitnative: Fix native async with.Damien George2024-06-21
| | | | | | | | | | | | | | | | The code generating the entry to the finally handler of an async-with statement was simply wrong for the case of the native emitter. Among other things the layout of the stack was incorrect. This is fixed by this commit. The setup of the async-with finally handler is now put in a dedicated emit function, for both the bytecode and native emitters to implement in their own way (the bytecode emitter is unchanged, just factored to a function). With this fix all of the async-with tests now work when using the native emitter. Signed-off-by: Damien George <damien@micropython.org>
* py/emitnative: Place thrown value in dedicated local variable.Damien George2024-06-21
| | | | | | | | A value thrown/injected into a native generator needs to be stored in a dedicated variable outside `nlr_buf_t`, following the `inject_exc` variable in `py/vm.c`. Signed-off-by: Damien George <damien@micropython.org>
* tests/thread: Re-enable GC before stress_schedule test ends.Angus Gratton2024-06-21
| | | | | | | Otherwise GC stays disabled (not re-enabled by soft reset) and later test runs fail with MemoryError. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* tests/cpydiff: Remove deque difference test.Damien George2024-06-21
| | | | | | Because `collections.deque` is now a built-in type in MicroPython. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/api: Allow specifying the pystack size.Damien George2024-06-20
| | | | | | | | | | This allows increasing the Python recursion depth if needed. Also increase the default to 2k words. There is enough RAM in the browser/node context for this to be increased, and having a larger pystack allows more complex code to run without hitting the limit. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/asyncio: Schedule run loop when tasks are pushed to queue.Damien George2024-06-20
| | | | | | | | | | | In the webassembly port there is no asyncio run loop running at the top level. Instead the Python asyncio run loop is scheduled through setTimeout and run by the outer JavaScript event loop. Because tasks can become runable from an external (to Python) event (eg a JavaScript callback), the run loop must be scheduled whenever a task is pushed to the asyncio task queue, otherwise tasks may be waiting forever on the queue. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/asyncio: Support top-level await of asyncio Task and Event.Damien George2024-06-18
| | | | | | | | | | | This change allows doing a top-level await on an asyncio primitive like Task and Event. This feature enables a better interaction and synchronisation between JavaScript and Python, because `api.runPythonAsync` can now be used (called from JavaScript) to await on the completion of asyncio primitives. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/objjsproxy: Implement proxying of JS iterable protocol.Damien George2024-06-18
| | | | | | | This allows Python to iterate over JavaScript objects that provide Symbol.iterator. Signed-off-by: Damien George <damien@micropython.org>
* qemu-riscv: Add new QEMU RV32 port.Alessandro Gatti2024-06-17
| | | | | | | | | | | This adds a QEMU-based bare metal RISC-V 32 bits port. For the time being only QEMU's "virt" 32 bits board is supported, using the ilp32 ABI and the RV32IMC architecture. The top-level README and the run-tests.py files are updated for this new port. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* extmod/modlwip: Make socket.connect raise ETIMEDOUT on non-zero timeout.Damien George2024-06-08
| | | | | | | | | | | If the socket timeout is 0 then a failed socket.connect() raises EINPROGRESS (which is what the lwIP bindings already did), but if the socket timeout is non-zero then a failed socket.connect() should raise ETIMEDOUT. The latter is fixed in this commit. A test is added for these timeout cases. Signed-off-by: Damien George <damien@micropython.org>
* py/lexer: Support raw f-strings.Damien George2024-06-06
| | | | | | | | Support for raw str/bytes already exists, and extending that to raw f-strings is easy. It also reduces code size because it eliminates an error message. Signed-off-by: Damien George <damien@micropython.org>
* py/lexer: Support concatenation of adjacent f-strings.Damien George2024-06-06
| | | | | | | This is quite a simple and small change to support concatenation of adjacent f-strings, and improve compatibility with CPython. Signed-off-by: Damien George <damien@micropython.org>
* tests/basics: Move str/bytes tests that give SyntaxWarning to sep file.Damien George2024-05-28
| | | | | | | In CPython 3.12 these invalid str/bytes/fstring escapes will issue a SyntaxWarning, and so differ to MicroPython. Signed-off-by: Damien George <damien@micropython.org>
* tests/basics: Add .exp file for slice_op test.Damien George2024-05-28
| | | | | | CPython 3.12 implemented hashing for slices, so now differs to MicroPython. Signed-off-by: Damien George <damien@micropython.org>
* tests/float: Use "not" instead of ~ to invert bool value.Damien George2024-05-28
| | | | | | | | | Otherwise CPython gives a deprecation warning. This test is not actually testing inversion of bools, rather that bit of the test is used to compute the pass/fail result. Signed-off-by: Damien George <damien@micropython.org>
* tests/net_inet/tls_text_errors.py: Tweak test for newer CPython version.Damien George2024-05-27
| | | | Signed-off-by: Damien George <damien@micropython.org>
* tests/net_hosted/ssl_verify_callback.py: Make exp match actual output.Damien George2024-05-27
| | | | | | | The `cert` argument passed to the verify callback is actually a memoryview. And the `depth` argument seems to start at 1 for the tested URL. Signed-off-by: Damien George <damien@micropython.org>
* tests/basics: Split out generator.throw tests that pass multiple args.Damien George2024-05-27
| | | | | | | | The three-argument form of `.throw()` is deprecated since CPython 3.12. So split out into separate tests (with .exp files) the parts of the generator tests that test more than one argument. Signed-off-by: Damien George <damien@micropython.org>
* tests/extmod: Add .exp test files for asyncio.get_event_loop tests.Damien George2024-05-27
| | | | | | | And use `asyncio.new_event_loop()` where possible. This change is needed because CPython 3.12 deprecated the `get_event_loop()` function. Signed-off-by: Damien George <damien@micropython.org>
* tests/extmod: Fix regex strings to be of r"" type.Damien George2024-05-27
| | | | | | Otherwise escape characters like \s and \W won't work correctly. Signed-off-by: Damien George <damien@micropython.org>
* tests/run-natmodtests.py: Fix search for supported native tests.Damien George2024-05-27
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/dynruntime: Export mp_load_method_maybe and mp_arg_parse_all* funcs.Brian Pugh2024-05-24
| | | | | | | Also define `mp_type_bytearray`. These all help to write native modules. Signed-off-by: Brian Pugh <bnp117@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_c: Return undefined if dict lookup failed on JS side.Damien George2024-05-16
| | | | | | | | Instead of raising KeyError. These semantics match JavaScript behaviour and make it much more seamless to pass Python dicts through to JavaScript as though they were JavaScript {} objects. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_js: Create a special "undefined" type for Python.Damien George2024-05-16
| | | | | | | This adds a new undefined singleton to Python, that corresponds directly to JavaScript `undefined`. It's accessible via `js.undefined`. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_js: Revert back to converting Py None to JS null.Damien George2024-05-16
| | | | | | | | | This reverts part of commit fa23e4b093f81f03a24187c7ea0c928a9b4a661b, to make it so that Python `None` converts to JavaScript `null` (and JavaScript `null` already converts to Python `None`). That's consistent with how the `json` module converts these values back and forth. Signed-off-by: Damien George <damien@micropython.org>
* tests/micropython/import_mpy_invalid.py: Skip if target cant import mpy.Damien George2024-05-14
| | | | Signed-off-by: Damien George <damien@micropython.org>
* webassembly/mpconfigport: Enable importing of .mpy files.Damien George2024-05-14
| | | | Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_js: Convert JS undefined and JS null to Py None.Damien George2024-05-13
| | | | | | | | | | | | | | And change Py None conversion so it converts to JS undefined. The semantics for conversion of these objects are then: - Python None -> JavaScript undefined - JavaScript undefined -> Python None - JavaScript null -> Python None This follows Pyodide: https://pyodide.org/en/stable/usage/type-conversions.html Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_c: Ensure objs thrown into generators are exceptions.Damien George2024-05-13
| | | | | | | | | | | This commit defines a new `JsException` exception type which is used on the Python side to wrap JavaScript errors. That's then used when a JavaScript Promise is rejected, and the reason is then converted to a `JsException` for the Python side to handle. This new exception is exposed as `jsffi.JsException`. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/asyncio: Fix case where a Promise is resolved with no arg.Damien George2024-05-13
| | | | Signed-off-by: Damien George <damien@micropython.org>
* webassembly/api: Resolve thenables returned from runPythonAsync.Damien George2024-05-07
| | | | | | | | | JavaScript semantics are such that the caller of an async function does not need to await that function for it to run to completion. This commit makes that behaviour also apply to top-level async Python code run via `runPythonAsync()`. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/objpyproxy: Implement JS iterator protocol for Py iterables.Damien George2024-05-07
| | | | | | This allows using JavaScript for..of on Python iterables. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_c: Reject promises with a PythonError instance.Damien George2024-05-06
| | | | | | | The `reason` in a rejected promise should be an instance of `Error`. That leads to better error messages on the JavaScript side. Signed-off-by: Damien George <damien@micropython.org>
* webassembly: Add JavaScript-based asyncio support.Damien George2024-04-24
| | | | | | | | | | | | | | | | | | | | | | | This commit adds a significant portion of the existing MicroPython asyncio module to the webassembly port, using parts of the existing asyncio code and some custom JavaScript parts. The key difference to the standard asyncio is that this version uses the JavaScript runtime to do the actual scheduling and waiting on events, eg Promise fulfillment, timeouts, fetching URLs. This implementation does not include asyncio.run(). Instead one just uses asyncio.create_task(..) to start tasks and then returns to the JavaScript. Then JavaScript will run the tasks. The implementation here tries to reuse as much existing asyncio code as possible, and gets all the semantics correct for things like cancellation and asyncio.wait_for. An alternative approach would reimplement Task, Event, etc using JavaScript Promise's. That approach is very difficult to get right when trying to implement cancellation (because it's not possible to cancel a JavaScript Promise). Signed-off-by: Damien George <damien@micropython.org>
* tests/cpydiff: Add a note about risk of resizing memoryview targets.Angus Gratton2024-04-22
| | | | | | | | This a stop-gap until there is a proper fix for this. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/objarray: Fix use-after-free if extending a bytearray from itself.Angus Gratton2024-04-22
| | | | | | | | | | | | | | | | | | | | | | | Two cases, one assigning to a slice. Closes https://github.com/micropython/micropython/issues/13283 Second is extending a slice from itself, similar logic. In both cases the problem occurs when m_renew causes realloc to move the buffer, leaving a dangling pointer behind. There are more complex and hard to fix cases when either argument is a memoryview into the buffer, currently resizing to a new address breaks memoryviews into that object. Reproducing this bug and confirming the fix was done by running the unix port under valgrind with GC-aware extensions. Note in default configurations with GIL this bug exists but has no impact (the free buffer won't be reused while the function is still executing, and is no longer referenced after it returns). Signed-off-by: Angus Gratton <angus@redyak.com.au>
* webassembly/proxy_js: Allow a Python proxy of a function to be undone.Damien George2024-03-30
| | | | | | | | | This optimises the case where a Python function is, for example, stored to a JavaScript attribute and then later retrieved from Python. The Python function no longer needs to be a proxy with double proxying needed for the call from Python -> JavaScript -> Python. Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_js: Promote Python thenable to a Promise.Damien George2024-03-30
| | | | Signed-off-by: Damien George <damien@micropython.org>
* webassembly/proxy_c: Ensure return value of async fun is passed to JS.Damien George2024-03-30
| | | | Signed-off-by: Damien George <damien@micropython.org>
* tests/net_inet: Add simpler tls sites test, and skip existing on axtls.Damien George2024-03-29
| | | | | | | | | Ports that use axtls cannot run the `test_tls_sites.py` test because the sites it connects to use advanced ciphers. So skip this test on such ports, and add a new, simpler test that doesn't require certificate verification and works with axtls. Signed-off-by: Damien George <damien@micropython.org>
* py/persistentcode: Bump .mpy sub-version to 6.3.Damien George2024-03-28
| | | | | | | | | | | | | This is required because the .mpy native ABI was changed by the introduction of `mp_proto_fun_t`, see commits: - 416465d81e911b088836f4e7c37fac2bc0f67917 - 5e3006f1172d0eabbbefeb3268dfb942ec7cf9cd - e2ff00e81113d7a3f32f860652017644b5d68bf1 And three `mp_binary` functions were added to `mp_fun_table` in commit d2276f0d41c2fa66a224725fdb2411846c91cf1a. Signed-off-by: Damien George <damien@micropython.org>
* tests/ports/webassembly: Add webassembly JS tests.Damien George2024-03-22
| | | | Signed-off-by: Damien George <damien@micropython.org>
* tests/run-tests.py: Support running webassembly tests via node.Damien George2024-03-22
| | | | | | | This allows running tests with a .js/.mjs suffix, and also .py tests using node and the webassembly port. Signed-off-by: Damien George <damien@micropython.org>
* tests/float/float_struct_e.py: Add specific test for struct 'e' type.Damien George2024-03-20
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/binary: Support half-float 'e' format in struct pack/unpack.Matthias Urlichs2024-03-20
| | | | | | | | | This commit implements the 'e' half-float format: 10-bit mantissa, 5-bit exponent. It uses native _Float16 if supported by the compiler, otherwise uses custom bitshifting encoding/decoding routines. Signed-off-by: Matthias Urlichs <matthias@urlichs.de> Signed-off-by: Damien George <damien@micropython.org>
* py/emitnative: Implement viper unary ops positive, negative and invert.Damien George2024-03-19
| | | | Signed-off-by: Damien George <damien@micropython.org>
* tests/basics: Split MicroPython-specific deque tests to separate file.Damien George2024-03-19
| | | | | | | So that the MicroPython-specific behaviour can be isolated, and the CPython compatible test don't need a .exp file. Signed-off-by: Damien George <damien@micropython.org>