summaryrefslogtreecommitdiffstatshomepage
path: root/py
Commit message (Collapse)AuthorAge
* all: Bump version to 1.24.1.v1.24.1v1.24-releaseDamien George2024-11-29
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/objfloat: Workaround non-constant NAN definition on Windows MSVC.Angus Gratton2024-11-28
| | | | | | | | | | | Recent MSVC versions have changed the definition of NAN to a non-constant expression! This is a bug, C standard says it should be a constant. Good explanation and workaround at: https://stackoverflow.com/a/79199887 This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/objdeque: Fix buffer overflow in deque_subscr.Jan Sturm2024-11-18
| | | | | | | | | | | | In `deque_subscr()`, if `index_val` equals `self->alloc`, the index correction `index_val -= self->alloc` does not execute, leading to an out-of-bounds access in `self->items[index_val]`. The fix in this commit ensures that the index correction is applied whenever `index_val >= self->alloc`, preventing access beyond the allocated buffer size. Signed-off-by: Jan Sturm <jansturm92@googlemail.com>
* pic16bit: Make it build with recent XC16 versions.Alessandro Gatti2024-11-18
| | | | | | | The PIC16 port didn't catch up with the other ports, so it required a bit of work to make it build with the latest version of XC16. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* all: Bump version to 1.24.0.v1.24.0Damien George2024-10-26
| | | | Signed-off-by: Damien George <damien@micropython.org>
* extmod/modtls_mbedtls: Add a thread-global ptr for current SSL context.iabdalkader2024-10-25
| | | | | | | This is necessary for mbedTLS callbacks that do not carry any user state, so those callbacks can be customised per SSL context. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
* py/objtype: Don't delegate lookup of descriptor methods to __getattr__.Damien George2024-10-16
| | | | | | | | When descriptors are enabled, lookup of the `__get__`, `__set__` and `__delete__` descriptor methods should not be delegated to `__getattr__`. That follows CPython behaviour. Signed-off-by: Damien George <damien@micropython.org>
* py/usermod.cmake: Check target exists in usermod_gather_sources.Phil Howard2024-10-15
| | | | | | | Check a target exists before accessing properties. Otherwise usermod_gather_sources would recurse into garbage property names and break. Signed-off-by: Phil Howard <phil@gadgetoid.com>
* py/objtype: Allow passing keyword arguments to native base __init__.stijn2024-10-07
| | | | | | | | | | | | | | | Allowing passing keyword arguments to a native base's __init__, i.e. `make_new` in the C code. Previously only positional arguments were allowed. The main trade-off in this commit is that every call to the native base's `make_new` is now going to be preceded by a call to `mp_map_init_fixed_table` even though most of what that does is unused and instead it merely serves as a way to pass the number of keyword arguments. Fixes issue #15465. Signed-off-by: stijn <stijn@ignitron.net>
* py/nlrrv64: Add RISC-V RV64I NLR implementation.Alessandro Gatti2024-10-03
| | | | | | Add custom NLR support for 64 bits RISC-V RV64I targets. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/parse: Remove old esp32 compiler workaround.Alessandro Gatti2024-09-27
| | | | | | | | | | | | The ESP32 port contains a workaround to avoid having a certain function in `py/parse.c` being generated incorrectly. The compiler in question is not part of any currently supported version of ESP-IDF anymore, and the problem inside the compiler (well, assembler in this case) has been corrected a few years ago. This commit removes all traces of that workaround from the source tree. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/mpz: Skip separators when running out of digits to print.Alessandro Gatti2024-09-26
| | | | | | | | | | This commit fixes the addition of a stray separator before the number when printing an MPZ-backed integer and the first group is three digits long. This fixes #8984. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/persistentcode: Explicitly track native BSS/rodata when needed.Damien George2024-09-26
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py/objringio: Add micropython.RingIO() interface for general use.Andrew Leech2024-09-19
| | | | | | | | | | | | This commit adds a new `RingIO` type which exposes the internal ring-buffer code for general use in Python programs. It has the stream interface making it similar to `StringIO` and `BytesIO`, except `RingIO` has a fixed buffer size and is automatically safe when reads and writes are in different threads or an IRQ. This new type is enabled at the "extra features" ROM level. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
* py/scheduler: Only run callbacks on the main thread if GIL is disabled.Angus Gratton2024-09-19
| | | | | | | | | | Otherwise it's very difficult to reason about thread safety in a scheduler callback, as it can run at any time on any thread - including racing against any bytecode operation on any thread. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/objint: Make byteorder argument optional in int.from_bytes() method.Amirreza Hamzavi2024-09-02
| | | | | | This was made optional in CPython 3.11. Signed-off-by: Amirreza Hamzavi <amirrezahamzavi2000@gmail.com>
* py/objint: Make length argument optional in int.to_bytes() method.Amirreza Hamzavi2024-09-02
| | | | | | This was made optional in CPython 3.11. Signed-off-by: Amirreza Hamzavi <amirrezahamzavi2000@gmail.com>
* py/objint: Make byteorder argument optional in int.to_bytes() method.Amirreza Hamzavi2024-09-02
| | | | | | This was made optional in CPython 3.11. Signed-off-by: Amirreza Hamzavi <amirrezahamzavi2000@gmail.com>
* py/mkrules.mk: Fix 'make submodules' when building out-of-tree.Christian Walther2024-08-26
| | | | | | | | | | When MicroPython is used as a submodule and built from the containing project, e.g. for the embed port, `make submodules` fails because it goes looking for the sub-sub-module paths in the outer repository instead of in the micropython repository. Fix this by invoking git inside the micropython submodule. Signed-off-by: Christian Walther <cwalther@gmx.ch>
* py/asmrv32: Use REG_TEMP2 whenever possible.Alessandro Gatti2024-08-19
| | | | | | | | The RV32 emitter used an additional temporary register, as certain code sequences required extra storage. This commit removes its usage in all but one case, using REG_TEMP2 instead. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/asmrv32: Emit C.LW opcodes only when necessary.Alessandro Gatti2024-08-19
| | | | | | | | | | | The RV32 emitter sometimes generated short load opcodes even when it was not supposed to. This commit fixes an off-by-one error in its offset eligibility range calculation and corrects one case of offset calculation, operating on the raw label index number rather than its effective offset in the stack (C.LW assumes all loads are word-aligned). Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/asmrv32: Fix short/long jumps scheduling.Alessandro Gatti2024-08-19
| | | | | | | | | | The RV32 emitter always scheduled short jumps even outside the emit compiler pass. Running the full test suite through the native emitter instead of just the tests that depend on the emitter at runtime (as in, `micropython/native_*` and `micropython/viper_* tests`) uncovered more places where the invalid behaviour was still present. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/objstr: Skip whitespace in bytes.fromhex().Glenn Moloney2024-08-19
| | | | | | | | | Skip whitespace characters between pairs of hex numbers. This makes `bytes.fromhex()` compatible with cpython. Includes simple test in `tests/basic/builtin_str_hex.py`. Signed-off-by: Glenn Moloney <glenn.moloney@gmail.com>
* py: Add new cstack API for stack checking, with limit margin macro.Angus Gratton2024-08-14
| | | | | | | | | | | | | | | | | | | | Currently the stack limit margin is hard-coded in each port's call to `mp_stack_set_limit()`, but on threaded ports it's fiddlier and can lead to bugs (such as incorrect thread stack margin on esp32). This commit provides a new API to initialise the C Stack in one function call, with a config macro to set the margin. Where possible the new call is inlined to reduce code size in thread-free ports. Intended replacement for `MP_TASK_STACK_LIMIT_MARGIN` on esp32. The previous `stackctrl.h` API is still present and unmodified apart from a deprecation comment. However it's not available when the `MICROPY_PREVIEW_VERSION_2` macro is set. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/obj: Remove the legacy object API for version 2.Angus Gratton2024-08-14
| | | | | | | | | These were changed in v1.11 (2019). Prepare to remove the compatibility macros as part of V2 changes. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/modmath: Add option to work around -inf bug in a port's tgamma.Angus Gratton2024-08-07
| | | | | | | | | This is needed for a workaround on esp32 port (in child commit), which produces incorrect results otherwise. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/emitnative: Fix case of clobbered REG_TEMP0 when loading const obj.Damien George2024-08-07
| | | | | | | | | | | | | | | | The `emit_load_reg_with_object()` helper function will clobber `REG_TEMP0`. This is currently OK on architectures where `REG_RET` and `REG_TEMP0` are the same (all architectures except RV32), because all callers of `emit_load_reg_with_object()` use either `REG_RET` or `REG_TEMP0` as the destination register. But on RV32 these registers are different and so when `REG_RET` is the destination, `REG_TEMP0` is clobbered, leading to incorrectly generated machine code. This commit fixes the issue simply by using `REG_TEMP0` as the destination register for all uses of `emit_load_reg_with_object()`, and adds a comment to make sure the caller of this function is careful. Signed-off-by: Damien George <damien@micropython.org>
* py/py.mk: Add SRC_USERMOD_LIB_ASM to include assembly files.George Hopkins2024-08-01
| | | | | | | | Introduce SRC_USERMOD_LIB_ASM to allow users to include assembly files as part of their user modules. It could be used to include optimized functions or outputs of other programming languages. Signed-off-by: George Hopkins <george-hopkins@null.net>
* py/runtime: Fix self arg passed to classmethod when accessed via super.Damien George2024-07-25
| | | | | | Thanks to @AJMansfield for the original test case. Signed-off-by: Damien George <damien@micropython.org>
* py/misc: Fix msvc and C++ compatibility.stijn2024-07-25
| | | | | | | | | | | Use explicit casts to suppress warnings about implicit conversions, add a workaround for constant expression conditional, and make functions static inline (as is done in the rest of the codebase) to suppress 'warning C4505: unreferenced function with internal linkage has been removed'. (Follow up to fix commit 908ab1ceca15ee6fd0ef82ca4cba770a3ec41894) Signed-off-by: stijn <stijn@ignitron.net>
* py/objtype: Validate super() arguments.stijn2024-07-25
| | | | | | | | | | This fixes various null dereferencing and out-of-bounds access because super_attr assumes the held obj is effectively an object of the held type, which is now verified. Fixes issue #12830. Signed-off-by: stijn <stijn@ignitron.net>
* py/objtype: Avoid crash on calling members of uninitialized native type.Laurens Valk2024-07-25
| | | | | | | | | | | | | 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>
* extmod/modmachine: Use sys.exit as implementation of machine.soft_reset.Damien George2024-07-20
| | | | | | It does the same thing, raising `SystemExit`. Signed-off-by: Damien George <damien@micropython.org>
* py/gc: Remove commented-out functions.Damien George2024-07-19
| | | | | | | These are old, unused, and most of them no longer compile. The `gc_test()` function is superseded by the test suite. Signed-off-by: Damien George <damien@micropython.org>
* py/sequence: Remove unused len argument from mp_seq_extract_slice.Damien George2024-07-18
| | | | | | | Also put this function inside the `MICROPY_PY_BUILTINS_SLICE` guard, because it's only usable when that option is enabled. Signed-off-by: Damien George <damien@micropython.org>
* py/lexer: Add static assert that token enum values all fit in a byte.Damien George2024-07-18
| | | | Signed-off-by: Damien George <damien@micropython.org>
* py: Remove 5 TODOs in emitbc, objrange and repl.Damien George2024-07-18
| | | | | | | | | | | | | | | | These TODOs don't need to be done: - Calling functions with keyword arguments is less common than without them, so adding an extra byte overhead to all calls regardless of whether they use keywords or not would overall increase generated bytecode size. - Restricting `range` objects to machine-sized ints has been adequate for a long time now, so no need to change that and make it more complicated and slower. - Printing spaces in tab completion does not need to be optimised. Signed-off-by: Damien George <damien@micropython.org>
* extmod/machine_spi: Support firstbit=LSB for machine.SoftSPI.robert-hh2024-07-12
| | | | | | | | | | | | | | | | Being able to send data out in LSB format can be useful, and having support in the low-level driver is much better than requiring Python code to reorder the bits before sending them / after receiving them. In particular if the hardware does not support the LSB format (eg RP2040) then one needs to use the SoftSPI in LSB mode. For this change a default definition of `MICROPY_PY_MACHINE_SPI_MSB/_LSB` was added to `py/mpconfig.h`, making them available to all ports. The identical defines in `esp32/mpconfigport.h` were deleted. Resolves issues #5340, #11404. Signed-off-by: robert-hh <robert@hammelrath.com>
* extmod/modmachine: Allow more than one argument to machine.freq().robert-hh2024-07-11
| | | | | | | | | | | The limit is set by a `MICROPY_PY_MACHINE_FREQ_NUM_ARGS_MAX` define, which defaults to 1 and is set for stm32 to 4. For stm32 this fixes a regression introduced in commit e1ec6af654b1c5c4a973b6c6b029ee68bb92eb89 where the maximum number of arguments was changed from 4 to 1. Signed-off-by: robert-hh <robert@hammelrath.com>
* all: Use new mp_obj_new_str_from_cstr() function.Jon Foster2024-07-04
| | | | | | | Use new function mp_obj_new_str_from_cstr() where appropriate. It simplifies the code, and makes it smaller too. Signed-off-by: Jon Foster <jon@jon-foster.co.uk>
* py/objstr: Add new mp_obj_new_str_from_cstr() helper function.Jon Foster2024-07-04
| | | | | | | | | | | | | | | There were lots of places where this pattern was duplicated, to convert a standard C string to a MicroPython string: x = mp_obj_new_str(s, strlen(s)); This commit provides a simpler method that removes this code duplication: x = mp_obj_new_str_from_cstr(s); This gives clearer, and probably smaller, code. Signed-off-by: Jon Foster <jon@jon-foster.co.uk>
* py/asmrv32: Do not use binary literals.Alessandro Gatti2024-07-01
| | | | | | | As per discussion in #15347, non-standard binary literals have been removed in favour of their hexadecimal counterparts. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/objint: Try to convert big-int back to small-int after binary op.Jim Mussared2024-07-01
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Before this change, long/mpz ints propagated into all future calculations, even if their value could fit in a small-int object. With this change, the result of a big-int binary op will now be converted to a small-int object if the value fits in a small-int. For example, a relatively common operation like `x = a * b // c` where a,b,c all small ints would always result in a long/mpz int, even if it didn't need to, and then this would impact all future calculations with x. This adds +24 bytes on PYBV11 but avoids heap allocations and potential surprises (e.g. `big-big` is now a small `0`, and can safely be accessed with MP_OBJ_SMALL_INT_VALUE). Performance tests are unchanged on PYBV10, except for `bm_pidigits.py` which makes heavy use of big-ints and gains about 8% in speed. Unix coverage tests have been updated to cover mpz code that is now unreachable by normal Python code (removing the unreachable code would lead to some surprising gaps in the internal C functions and the functionality may be needed in the future, so it is kept because it has minimal overhead). This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/asmrv32: Make some code sequences smaller.Alessandro Gatti2024-07-01
| | | | | | | | | | | | | | This commit changes a few code sequences to use more compressed opcodes where possible. The sequences in question are the ones that show up the most in the test suite and require the least amount of code changes, namely short offset loads from memory to RET/ARG registers, indirect calls through the function table, register-based jumps, locals' offset calculation, reg-is-null jumps, and register comparisons. There are no speed losses or gains from these changes, but there is an average 15-20% generated code size reduction. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
* py/objarray: Fix buffer overflow in case of memory allocation failure.Yoctopuce2024-06-26
| | | | | | | | | If `array.append()` fails with an exception due to heap exhaustion, the next attempt to grow the buffer will cause a buffer overflow because the free slot count is increased before performing the allocation, and will stay as if the allocation succeeded. Signed-off-by: Yoctopuce <dev@yoctopuce.com>
* 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>
* py/misc: Move mp_clz and mp_ctz intrinsics into misc.h.Angus Gratton2024-06-24
| | | | Signed-off-by: Angus Gratton <angus@redyak.com.au>
* 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>
* py/emitndebug: Add native debug emitter.Damien George2024-06-21
| | | | | | | | | | | This emitter prints out pseudo-machine instructions, instead of the usual output of the native emitter. It can be enabled on any port via `MICROPY_EMIT_NATIVE_DEBUG` (make sure other native emitters are disabled) but the easiest way to use it is with mpy-cross: $ mpy-cross -march=debug file.py Signed-off-by: Damien George <damien@micropython.org>