summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2025-10-23all: Simplify mp_int_t/mp_uint_t definition.Jeff Epler
Assuming proper C99 language support, we can select "the int type as big as a pointer" (most of the time) or "the 64-bit int type" (nanboxing with REPR_D), and then define everything else automatically. This simplifies port configuration files. And the types can still be overridden if needed. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-06py/mpprint: Correctly format leading zeros with separators.Jeff Epler
Correctly format integers when there are leading zeros with a grouping character, such as "{:04,d}".format(0x100) -> "0,256". The new padding patterns for commas-and-zeroes and underscores-and-zeroes are smooshed together into the existing pad_zeroes to save space. Only the two combinations of (decimal + commas) and (other bases + underscores) are properly supported. Also add a test for it. Fixes issue #18082. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-04py/objmodule: Avoid interning a string unnecessarily.Jeff Epler
If the lookup of the module name less the leading "u" is going to succeed, it's already a qstr (because that qstr is the key in `mp_builtin_extensible_module_map`). So, use a function that will avoid interning it if it's not already. For example, if you `import unavailable` it used to intern the string `navailable`, but now it won't. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-04py/makemoduledefs.py: Avoid empty extensible module lists.Jeff Epler
An empty array is a C extension supported by clang & GCC but not MSVC. This also saves a bit of code size if there are no extensible modules. Fixes issue #18141. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-04py/py.mk: Regenerate moduledefs.h if makemoduledefs.py changes.Jeff Epler
This makes iterating on the script easier. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-04py/misc: Don't warn about a GNU extension for static assert macro.Jeff Epler
This warning was enabled by default on clang 17.0.0 on macOS 26. Disable it, because we want to make these checks at compile-time even if it requires an extension. Fixes issue #18116. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-04py/misc: Use _Static_assert for MP_STATIC_ASSERT where possible.Jeff Epler
Or use C++ `static_assert` when that's available. For the same reasons that C++ has trouble with "nonconstexpr" static assertions, `_Static_assert` rejects such expression as well. So, fall back to the old sizeof-array based implementation in that case. When `_Static_assert` can be used, the diagnostic quality is improved: ../py/objint.c: In function ‘mp_obj_int_make_new’: ../py/misc.h:67:32: error: static assertion failed: "37 == 42" ../py/objint.c:45:5: note: in expansion of macro ‘MP_STATIC_ASSERT’ As compared to a diagnostic about ../py/misc.h:71:50: error: size of unnamed array is negative Testing on godbolt indicated that this actually works back to gcc 4.5, but it's easier to use GNUC >= 5 as the test; hypothetical users of 4.5, 4.6, or 4.7 will just get slightly worse diagnostics. See related issue #18116. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-04all: Replace legacy name with MicroPython and MPy as applicable.Angus Gratton
With the aim of getting consistency, and removing the need to learn an additional term, replace uses of uPy/uPython with MPy/MicroPython. Rule of thumb was to use "MPy" abbreviation where "CPy" is used nearby, but the full word MicroPython otherwise. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-10-03py/scheduler: Allow selective handling in mp_handle_pending.iabdalkader
Extend mp_handle_pending to support three distinct behaviors via mp_handle_pending_internal(): - MP_HANDLE_PENDING_CALLBACKS_ONLY: process callbacks only - MP_HANDLE_PENDING_CALLBACKS_AND_EXCEPTIONS: callbacks + raise exceptions - MP_HANDLE_PENDING_CALLBACKS_AND_CLEAR_EXCEPTIONS: callbacks + clear only Original mp_handle_pending(bool) preserved as inline wrapper for backward compatibility. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2025-10-03py/runtime: Fix printing of failed allocation amounts.Jeff Epler
On LP64 and LLP64 systems, size_t is bigger than unsigned. Printing the failed allocation using the new SIZE_FMT macro allows the correct failed allocation size to be shown. Example where this affects the failed allocation message (on x86_64 coverage build): >>> "a" * (1 << 54) Before, this would print the size as 1. Now it prints it as 18014398509481985 (2**54 + 1). Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-10-03py/mpconfig: Introduce SIZE_FMT macro.Jeff Epler
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-02all: Use "static inline" consistently in function definitions.Jeff Epler
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-10-01py/modmath: Make MICROPY_PY_MATH_POW_FIX_NAN also fix pow(x, NaN) cases.Damien George
This is needed by the zephyr port. Combining it with the existing `MICROPY_PY_MATH_POW_FIX_NAN` option should be safe, and eliminates the need for a separate option. Signed-off-by: Damien George <damien@micropython.org>
2025-10-01py: Add MICROPY_USE_GCC_MUL_OVERFLOW_INTRINSIC.Jeff Epler
Most MCUs apart from Cortex-M0 with Thumb 1 have an instruction for computing the "high part" of a multiplication (e.g., the upper 32 bits of a 32x32 multiply). When they do, gcc uses this to implement a small and fast overflow check using the __builtin_mul_overflow intrinsic, which is preferable to the guard division method previously used in smallint.c. However, in contrast to the previous mp_small_int_mul_overflow routine, which checks that the result fits not only within mp_int_t but is SMALL_INT_FITS(), __builtin_mul_overflow only checks for overflow of the C type. As a result, a slight change in the code flow is needed for MP_BINARY_OP_MULTIPLY. Other sites using mp_small_int_mul_overflow already had the result value flow through to a SMALL_INT_FITS check so they didn't need any additional changes. Do similarly for the _ll and _ull multiply overflows checks. Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-09-30py/runtime: Support importing a method from an instance.Damien George
This change follows CPython behaviour, allowing use of: from instance import method to import a bound method from a class instance, eg registered via setting `sys.modules["instance"] = instance`. Admittedly this is probably a very rarely used pattern in Python, but it resolves a long standing comment about whether or not this is actually possible (it turns out it is possible!). A test is added to show how it works. The main reason for this change is to fix a problem with imports in the webassembly port: prior to this fix, it was not possible to do `from js_module import function`, where `js_module` is a JavaScript object registered to be visible to Python through the webassembly API function `registerJsModule(js_module)`. But now with this fix that is possible. Signed-off-by: Damien George <damien@micropython.org>
2025-09-28py/objint: Fix converting float to int with OBJ_REPR_B.Jeff Epler
The check for 'fits in a small int' is specific to the 31-bit int of other object representations. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-09-26py/mkrules.mk: Add %.sz rule to print size of an object file.Jeff Epler
It's frequently the case that a developer will want to compare the object code size of various alternatives. When this can be done at the single object code level, the turnaround is faster. Provide a rule `$(BUILD)/%.sz` to print the size of a given object. Because it is a normal Makefile target that depends on an object file, it rebuilds the object file if needed. Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-09-26py/objrange: Allow return of non-small ints.Jeff Epler
The magnitude of `range()` arguments is not restricted to "small" ints, but includes "machine ints" which fit inside a register but can only be represented as "long integer" objects in Python. Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-09-26py/stream: Reuse write implementation for readinto.Damien George
This commit refactors some common code in the core stream implementation, to reduce code size while retaining the same functionality. With the factoring, `readinto`/`readinto1` could now support an additional 4th argument (like write) but it's best not to introduce even more CPython incompatibility, so they are left as having a maximum of 3 args. Signed-off-by: Damien George <damien@micropython.org>
2025-09-25shared/runtime/mpirq: Check separate hard IRQ stack correctly.Chris Webb
On the zephyr port, hard IRQ handlers run with a separate stack on a different thread, so each call to mp_irq_dispatch() and mp_irq_handler() has to be wrapped with adjustments to the stack-limit checker. Move these adjustments into the shared mp_irq_dispatch(), introducing MICROPY_STACK_SIZE_HARD_IRQ which a port can define to non-zero if it uses a separate stack for hard IRQ handlers. We only need wrap the hard dispatch case. This should reduce binary size on zephyr without affecting other ports. Signed-off-by: Chris Webb <chris@arachsys.com>
2025-09-19py/emitinlinerv32: Add Zba opcodes to the inline assembler.Alessandro Gatti
This commit adds support for Zba opcodes to the RV32 inline assembler. Three new opcodes were added, SH1ADD, SH2ADD, and SH3ADD, which performs a scaled addition (by 1, 2, or 3 bits respectively). At the moment only qemu's VIRT_RV32 and rp2's RPI_PICO2/RPI_PICO2_W ports support these opcodes (the latter only when using the RISCV variant). Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-09-19py/asmrv32: Use RV32 Zba opcodes if possible.Alessandro Gatti
This commit adds optional support for selected Zba opcodes (address generation) to speed up Viper and native code generation on MCUs where those opcodes are supported (namely RP2350). Right now support for these opcodes is opt-in, as extension detection granularity on the RISC-V platform is still a bit in flux. Relying on the 'B' bit in the MISA register may yield both false positives and false negatives depending on the RISC-V implementation the check runs on. As a side-effect of Zba support, regular non-byte load/stores have been made shorter by two bytes. Whilst this makes code using Zba take up the same space as non-Zba code, the former will still be faster as it will have to process just one instruction instead of two, without stalling registers between the shift and the addition needed to compute the final offset. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-09-19py/mpstate: Make it possible for mpy-cross to set emitter options.Alessandro Gatti
This commit introduces a way for mpy-cross to pass a set of options to the chosen emitter. This is achieved by adding an opaque pointer to the dynamic compiler state structure that is only accessed by emitters that have a need to receive options from mpy-cross when generating code. That's a way to make this feature possible without breaking any existing code or emitter, and without re-engineering the compiler entry point function (together with passing the options struct downstream until it's time to emit code). The main use case for this is letting the RV32 emitter know which optional extensions it can generate code with, to be able to emit better suited code for the platform in use. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-09-19mpy-cross: Add RISC-V RV64IMC support in MPY files.Alessandro Gatti
MPY files can now hold data to be run on RV64IMC. This can be accomplished by passing the `-march=rv64imc` flag to mpy-cross. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-09-16py/py.cmake: Add nlraarch64.Ayush Singh
- Required for aarch64 zephyr port targets to build. - Tested with PocketBeagle 2 [0] A53 cores. [0]: https://docs.zephyrproject.org/latest/boards/beagle/pocketbeagle_2/doc/index.html Signed-off-by: Ayush Singh <ayush@beagleboard.org>
2025-09-16py/obj: Document undocumented MP_TYPE_FLAG values.Anson Mansfield
This adds comments documentign the meaning of the type flag bits `MP_TYPE_FLAG_IS_SUBCLASSED` and `MP_TYPE_FLAG_HAS_SPECIAL_ACCESSORS`, for consistency with the comments added later on documenting the other flags. These flags were were originally made part of the public API in c3450effd4c3a402eeccf44a84a05ef4b36d69a0. This block of doc comments was only added later on when `MP_TYPE_FLAG_EQ_NOT_REFLEXIVE`, `MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE`, and `MP_TYPE_FLAG_EQ_HAS_NEQ_TEST` were added in 9ec1caf42e7733b5141b7aecf1b6e58834a94bf7. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-09-15py: Remove unneeded future imports.Jeff Epler
Signed-off-by: Jeff Epler <jepler@unpythonic.net>
2025-09-15all: Remove Python 2.7 support.Jeff Epler
Python 2.7 has been EOL since January 2020. Ubuntu oldoldlts (Focal Fossa, 20.04) has Python 3.8. Debian oldoldstable (Buster, from 2019) has Python 3.7. RHEL 8 (from 2019) has Python 3.6. It's easier than ever to install a modern Python using uv. Given this, it seems like a fine idea to drop Python 2.7 support. Even though the build is not tested on Python as old as 3.3, I left comments stating that "3.3+" is the baseline Python version. However, it might make sense to bump this to e.g., 3.10, the oldest Python 3 version used during CI. Or, using uv or another method actually test on the oldest Python interpreter that is desirable to support (uv goes back to Python 3.7 easily; in October 2025, the oldest supported Python interpreter version will be 3.10) Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-09-11py/stream: Support additional arguments for mp_stream_write1_obj.robert-hh
As suggested by @dpgeorge adding a `stream_write_generic()` function. Tested the different write timeouts with a RP2 using flow control. Tested support of the additional arguments of `uart.write()`. Signed-off-by: robert-hh <robert@hammelrath.com>
2025-09-11py/stream: Add a stream.readinto1() method for machine.UART.robert-hh
Avoiding the double timeout when used with the UART class. `stream.readinto1()` returns after the first timeout. Fixes issue #17611. Signed-off-by: robert-hh <robert@hammelrath.com>
2025-09-10py/compile: Throw SyntaxError instead of asserting.Jeff Epler
This condition corresponds to invalid asm code like ``` @micropython.asm_rv32 def l(): a=di(a2, a2, -1) ``` and possibly other forms where nodes[0] is not a STRUCT. Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-09-10py/gc: Clean up usage of GC_ALLOC_FLAG_HAS_FINALISER flag.Anson Mansfield
The calls signature for gc_malloc was changed in 5ed578e5b48730606536ded9a711223ae9a70262, without cleaning up existing code on the rationale that the previous bool is automatically converted to an int with the same meaning. This commit goes back and cleans up existing invocations to make their behavior more readable in a modern context. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-09-10py/obj: Update with_finaliser version to match mp_obj_malloc_var.Anson Mansfield
Cleans up 24234937747e6d7fd920d21358fb26b826398e1a, and fixes builds that include LFS but not MICROPY_ENABLE_FINALISER. Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-09-10py/makeversionhdr.py: Always abbreviate Git hashes to same length.Daniël van de Giessen
The Git hash is embedded in the version number. The hash is abbreviated by Git. This commit changes the length of the Git hash abbreviation to a fixed number, so that the length of the version string no longer varies based on external factors (it can still vary, but will now be at least 10 characters). This change is made because builds of the same MicroPython commit on multiple machines were sometimes giving a version string with different lengths, eg due to commits on other local branches having a clashing abbreviated hash. This change may also help the code size report to be more consistent, because it will less often be impacted by random changes in the version string length, at the cost of always being a few bytes longer. Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2025-08-28py/mkrules.mk: Force ".pp" files to always rebuild.Jeff Epler
These files are only built on demand for developers, and it is a quick process. Without FORCE, a sequence like this would leave the developer with an outdated `main.pp` to inspect: make build-standard/main.pp touch input.h make build-standard/main.pp # Rebuilds now, wouldn't have before Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-08-28py/parsenum: Fix parsing LLONG_MIN in longlong configuration.Jeff Epler
Re-organize `mp_parse_num_integer()` (for longlong) slightly so that the most negative 64-bit integer can be parsed. Fixes issue #17932. Signed-off-by: Jeff Epler <jepler@gmail.com>
2025-08-28py/obj: Remove unused map new/free function declarations.Anson Mansfield
These functions were removed in 6c9fca2 for v1.9.3. This commit removes their declarations as well. See-also: 6c9fca2aa911e31f6c1b48d3b950b4dc058473d4 Signed-off-by: Anson Mansfield <amansfield@mantaro.com>
2025-08-26py/obj: Fix a comment regarding make_new slot.David Schneider
This was missed as part of the transition to make_new a mp_obj_type_t slot. See also: 94beeabd2ee179d587942046555833e022241f24 Signed-off-by: David Schneider <schneidav81@gmail.com>
2025-08-22py/mpconfig: Enable the sys module at all feature levels by default.Damien George
This is a pretty fundamental module, and even minimal ports like unix and zephyr minimal have it enabled. So, enabled it by default at the lowest feature level. Most things in the `sys` module are configurable, and off by default, so it shouldn't add too much to ports that don't already have it enabled (which is just the minimal port). Also note that `sys` is still disabled on the bare-arm port, to keep that ultra minimal. It means we now have bare-arm without `sys` and the minimal port with `sys`. That will allow different code size comparisons if/when new `sys` features are added. Signed-off-by: Damien George <damien@micropython.org>
2025-08-22py/builtinimport: Guard code needing sys.path with MICROPY_PY_SYS_PATH.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2025-08-19py/mpconfig: Enable CRYPTOLIB, HASHLIB_MD5, HASHLIB_SHA1 if SSL enabled.Damien George
This commit unifies the configuration of MICROPY_PY_CRYPTOLIB, MICROPY_PY_HASHLIB_MD5 and MICROPY_PY_HASHLIB_SHA1, so they are enabled by default if MICROPY_PY_SSL is enabled. This matches the existing configuration of most of the ports. With this change, all ports remain the same except: - reneses-ra now enables MICROPY_PY_CRYPTOLIB, MICROPY_PY_HASHLIB_MD5 and MICROPY_PY_HASHLIB_SHA1. - rp2 now enables MICROPY_PY_HASHLIB_MD5. Signed-off-by: Damien George <damien@micropython.org>
2025-08-19py/mpconfig: Move MICROPY_MODULE___ALL__ option to other module options.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2025-08-19py/mpconfig: Rename MICROPY_PY___FILE__ to MICROPY_MODULE___FILE__.Damien George
For consistency with other module-related configuration options. Signed-off-by: Damien George <damien@micropython.org>
2025-08-19py/objtype: Use locals_ptr directly instead of getting it from the slot.Damien George
This is a very minor code simplification, which reduces code size by about -8 bytes. It should have no functional change. Signed-off-by: Damien George <damien@micropython.org>
2025-08-19py/asmthumb: Fix T3 encoding of conditional branches.Alessandro Gatti
This commit fixes the encoding of conditional branch opcodes emitted for ARMv7-M targets, when the emitter decides to use the T3 encoding for said operation. Fields J1 and J2 are now present in the generated opcode word, along with correcting some minor issues in bitmasks and shifts computation. This fixes #17940. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2025-08-19py/misc: Add a way to detect sanitizer builds.Angus Gratton
Clang and gcc>=14 can use __has_feature() to detect if a sanitizer is enabled, but older GCC has no mechanism - need to set a macro explicitly for this to be recognised. Necessary for increasing some resource limits in sanitizer builds. Important not to use to avoid real issues! This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2025-08-15py/objtype: Make mp_obj_new_type a static function.Damien George
It's only used once, in the same file it's defined, and making it static reduces code size. Along with this, the associated example code comment in `ports/unix/main.c` has been removed. Signed-off-by: Damien George <damien@micropython.org>
2025-08-15py/parse: Remove explicit checks for invalid folding operations.Damien George
They are instead checked by `binary_op_maybe()`, which catches exceptions for invalid int/float operations. This is a follow-up to 69ead7d98ef30df3b6bd4485633490e80fca1718 Signed-off-by: Damien George <damien@micropython.org>
2025-08-15py/mkrules.cmake: Clean genhdr and frozen_mpy dirs.Phil Howard
CMake builds relied upon the parent Makefile removing the entire build directory to successfully clean build artifacts. py/mkrules.cmake: Add ADDITIONAL_CLEAN_FILES properties to ensure a "make clean" from within the build directory removes the genhdr and frozen_mpy directories. Signed-off-by: Phil Howard <github@gadgetoid.com>
2025-08-15py/objringio: Detect incorrect constructor calls.Jeff Epler
ringbuffer.size must be at least 2, and is a 16-bit quantity. This fixes several cases including the one the fuzzer discovered, which would lead to a fatal signal when accessing the object. Fixes issue #17847. Signed-off-by: Jeff Epler <jepler@gmail.com>