summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2023-04-26extmod/btstack: Fix indicate/notify queuing.Jim Mussared
This adds a mechanism to track a pending notify/indicate operation that is deferred due to the send buffer being full. This uses a tracked alloc that is passed as the content arg to the callback. This replaces the previous mechanism that did this via the global pending op queue, shared with client read/write ops. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-04-11py/makeqstrdefs.py: Fix handling GreenHills C/C++ preprocessor output.Alex Riesen
The GreenHills preprocessor produces #line directives without a file name, which the regular expression used to distiguish between "# <number> file..." (GCC and similar) and "#line <number> file..." (Microsoft C and similar) does not match, aborting processing. Besides, the regular expression was unnecessarily wide, matching lines containing a "#", followed by any number of 'l','i','n', and 'e' characters. Signed-off-by: Alex Riesen <alexander.riesen@cetitec.com>
2023-03-28py/obj: Fix spelling of staticmethod.David Lechner
Signed-off-by: David Lechner <david@pybricks.com>
2023-03-21py/scheduler: Implement VM abort flag and mp_sched_vm_abort().Damien George
This is intended to be used by the very outer caller of the VM/runtime. It allows setting a top-level NLR handler that can be jumped to directly, in order to forcefully abort the VM/runtime. Enable using: #define MICROPY_ENABLE_VM_ABORT (1) Set up the handler at the top level using: nlr_buf_t nlr; nlr.ret_val = NULL; if (nlr_push(&nlr) == 0) { nlr_set_abort(&nlr); // call into the VM/runtime ... nlr_pop(); } else { if (nlr.ret_val == NULL) { // handle abort ... } else { // handle other exception that propagated to the top level ... } } nlr_set_abort(NULL); Schedule an abort, eg from an interrupt handler, using: mp_sched_vm_abort(); Signed-off-by: Damien George <damien@micropython.org>
2023-03-21py/mpstate: Add mp_thread_is_main_thread() helper macro.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-03-17py/obj: Add MP_NOINLINE to mp_obj_malloc_helper.David Lechner
As the comment in py/obj.h says: > Implementing this as a call rather than inline saves 8 bytes per usage. So in order to get this savings, we need to tell the compiler to never inline the function. Signed-off-by: David Lechner <david@pybricks.com>
2023-03-10py/makeversionhdr.py: Always add micro to version string even if it's 0.Damien George
Moving forward, tags in this repository will always have three components. Signed-off-by: Damien George <damien@micropython.org>
2023-03-10py/builtinimport: Fix unix port build with external imports disabled.Laurens Valk
Without this, building the unix port variants gives: ports/unix/main.c:667: undefined reference to `mp_obj_is_package', when MICROPY_ENABLE_EXTERNAL_IMPORT is 0. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2023-03-10py/mpconfig: Provide config option for internal printf printer.Damien George
The C-level printf is usually used for internal debugging prints, and a port/board may want to redirect this somewhere other than stdout. Signed-off-by: Damien George <damien@micropython.org>
2023-03-10py/gc: Make gc_dump_info/gc_dump_alloc_table take a printer as argument.Damien George
So that callers can redirect the output if needed. Signed-off-by: Damien George <damien@micropython.org>
2023-03-09py/compile: Fix scope of assignment expression target in comprehensions.Damien George
When := is used in a comprehension the target variable is bound to the parent scope, so it's either a global or a nonlocal. Prior to this commit that was handled by simply using the parent scope's id_info for the target variable. That's completely wrong because it uses the slot number for the parent's Python stack to store the variable, rather than the slot number for the comprehension. This will in most cases lead to incorrect behaviour or memory faults. This commit fixes the scoping of the target variable by explicitly declaring it a global or nonlocal, depending on whether the parent is the global scope or not. Then the id_info of the comprehension can be used to access the target variable. This fixes a lot of cases of using := in a comprehension. Code size change for this commit: bare-arm: +0 +0.000% minimal x86: +0 +0.000% unix x64: +152 +0.019% standard stm32: +96 +0.024% PYBV10 cc3200: +96 +0.052% esp8266: +196 +0.028% GENERIC esp32: +156 +0.010% GENERIC[incl +8(data)] mimxrt: +96 +0.027% TEENSY40 renesas-ra: +88 +0.014% RA6M2_EK nrf: +88 +0.048% pca10040 rp2: +104 +0.020% PICO samd: +88 +0.033% ADAFRUIT_ITSYBITSY_M4_EXPRESS Fixes issue #10895. Signed-off-by: Damien George <damien@micropython.org>
2023-03-02py/makeversionhdr.py: Optionally get git tag and git hash from env vars.David Grayson
This is handy when you are doing builds outside of the Git repository but still want to record that information. Signed-off-by: David Grayson <davidegrayson@gmail.com>
2023-02-27py/emitnative: Explicitly compare comparison ops in binary_op emitter.Pepijn de Vos
Without this it's possible to get a compiler error about the comparison always being true, because MP_BINARY_OP_LESS is 0. And it seems that gcc optimises these 6 equality comparisons into the same size machine code as before.
2023-02-24py/modmath: Fix two-argument math function domain check.Damien George
Prior to this fix, pow(1.5, inf) and pow(0.5, -inf) (among other things) would incorrectly raise a ValueError, because the result is inf with the first argument being finite. This commit fixes this by allowing the result to be infinite if the first or second (or both) argument is infinite. This fix doesn't affect the other three math functions that have two arguments: - atan2 never returns inf, so always fails isinf(ans) - copysign returns inf only if the first argument x is inf, so will never reach the isinf(y) check - fmod never returns inf, so always fails isinf(ans) Signed-off-by: Damien George <damien@micropython.org>
2023-02-08py/mkrules.cmake: Force build mpversion.h and frozen_content.c.Jim Mussared
This ensures that all builds unconditionally run makeversionhdr.py and makemanifest.py to generate mpversion.h and frozen_content.c respectively. This now matches the Makefile behavior, and in particular this fixes the issue on ESP32 builds that changes in code-to-be-frozen will cause the build to update. Both these already tools know not to touch their output if there is no change. 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>
2023-01-24py/mkrules: Support mpy-tool-flags in cmake frozen code generation.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-01-23py/objint_mpz: Catch and reject @ and @= operating on big integers.Damien George
This will also catch / and /= when float support is disabled. Fixes issue #10544. Signed-off-by: Damien George <damien@micropython.org>
2023-01-20py: Add parenthesis to default impl of MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR.Alex Riesen
Unless MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D, these macros only work with values and "->"/"." expressions as their sole argument. In other words, the macros are broken with expressions which contain operations of lower precedence than the cast operator. Depending on situation, the old code either results in compiler error: MP_OBJ_TO_PTR(flag ? o1 : o2) expands into "(void *)flag ? o1 : o2", which some compiler configurations will reject (e.g. GCC -Wint-conversion -Wint-to-pointer-cast -Werror) Or in an incorrect address calculation: For ptr declared as "uint8_t *" the MP_OBJ_FROM_PTR(ptr + off) expands into ((mp_obj_t)ptr) + off, resulting in an obviously wrong address. Signed-off-by: Alex Riesen <alexander.riesen@cetitec.com>
2023-01-20py/lexer: Wrap in parenthesis all f-string arguments passed to format.Jim Mussared
This is important for literal tuples, e.g. f"{a,b,}, {c}" --> "{}".format((a,b), (c),) which would otherwise result in either a syntax error or the wrong result. Fixes issue #9635. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-01-20py/objarray: Raise error on out-of-bound memoryview slice start.Andrew Leech
32-bit platforms only support a slice offset start of 24 bit max due to the limited size of the mp_obj_array_t.free member. Similarly on 64-bit platforms the limit is 56 bits. This commit adds an OverflowError if the user attempts to slice a memoryview beyond this limit. Signed-off-by: Damien George <damien@micropython.org>
2023-01-16py/gc: Increase the address length in gc_dump_alloc_table().robert-hh
Showing 8 digits instead of 5, supporting devices with more than 1 MByte of RAM (which is common these days). The masking was never needed, and the related commented-out line can go.
2023-01-13py/map: Clear value when re-using slot with ordered dictionaries.Philip Peitsch
To adhere to the contract of mp_map_lookup, namely: MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour: - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
2022-12-16py/emitnative: Initialise locals as Python object type for native code.Damien George
In @micropython.native code the types of variables and expressions are always Python objects, so they can be initialised as such. This prevents problems with compiling optimised code like while-loops where a local may be referenced before it is assigned to. Signed-off-by: Damien George <damien@micropython.org>
2022-12-15py/obj: Remove unused MP_DEFINE_CONST_OBJ_FULL_TYPE macro.Jim Mussared
This was previously used for the definition of NIC types, but they have been updated to use a protocol instead. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-12-08py/gc: Fix debug printing of GC layout.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2022-12-08unix/coverage: Add extra GC coverage test for ATB gap byte.Jeff Epler
The assertion that is added here (to gc.c) fails when running this new test if ALLOC_TABLE_GAP_BYTE is set to 0. Signed-off-by: Jeff Epler <jepler@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
2022-12-08py/gc: Ensure a gap of one byte after the ATB.Jeff Epler
Prior to this fix the follow crash occurred. With a GC layout of: GC layout: alloc table at 0x3fd80428, length 32001 bytes, 128004 blocks finaliser table at 0x3fd88129, length 16001 bytes, 128008 blocks pool at 0x3fd8bfc0, length 2048064 bytes, 128004 blocks Block 128003 is an AT_HEAD and eventually is passed to gc_mark_subtree. This causes gc_mark_subtree to call ATB_GET_KIND(128004). When block 1 is created with a finaliser, the first byte of the finaliser table becomes 0x2, but ATB_GET_KIND(128004) reads these bits as AT_TAIL, and then gc_mark_subtree references past the end of the heap, which happened to be past the end of PSRAM on the esp32-s2. The fix in this commit is to ensure there is a one-byte gap after the ATB filled permanently with AT_FREE. Fixes issue #7116. See also https://github.com/adafruit/circuitpython/issues/5021 Signed-off-by: Jeff Epler <jepler@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
2022-12-08py/gc: Avoid valgrind false positives.Jeff Epler
When you want to use the valgrind memory analysis tool on MicroPython, you can arrange to define MICROPY_DEBUG_VALGRIND to enable use of special valgrind macros. For now, this only fixes `gc_get_ptr` so that it never emits the diagnostic "Conditional jump or move depends on uninitialised value(s)". Signed-off-by: Jeff Epler <jepler@gmail.com>
2022-12-08py: Pass in address to compiled module instead of returning it.Damien George
This change makes it so the compiler and persistent code loader take a mp_compiled_module_t* as their last argument, instead of returning this struct. This eliminates a duplicate context variable for all callers of these functions (because the context is now stored in the mp_compiled_module_t by the caller), and also eliminates any confusion about which context to use after the mp_compile_to_raw_code or mp_raw_code_load function returns (because there is now only one context, that stored in mp_compiled_module_t.context). Reduces code size by 16 bytes on ARM Cortex-based ports. Signed-off-by: Damien George <damien@micropython.org>
2022-12-06py: Remove the word "yet" from exception messages.Damien George
These unimplemented features may never be implemented, and having the word "yet" there takes up space. Signed-off-by: Damien George <damien@micropython.org>
2022-12-06py/mpconfig: Include micropython module in core features.Laurens Valk
This excludes it from the minimal builds. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-12-06py/modmicropython: Make module optional.Laurens Valk
This module is useful, but it is not always needed. Disabling it saves several kilobytes of build size, depending on other config options. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-11-28py/bc: Fix checking for duplicate **kwargs.David Lechner
The code was already checking for duplicate kwargs for named parameters but if `**kwargs` was given as a parameter, it did not check for multiples of the same argument name. This fixes the issue by adding an addition test to catch duplicates and adds a test to exercise the code. Fixes issue #10083. Signed-off-by: David Lechner <david@pybricks.com>
2022-11-25py/objdict: Implement dictionary union (PEP 584).Rayane Chatrieux
Implements dictionary union according to PEP 584's specifications, minus the fact that dictionary entries are not guaranteed to be in insertion order. This feature is enabled with MICROPY_CPYTHON_COMPAT. Includes a new test. With the assistance of Fangrui Qin <qinf@purdue.edu> Signed-off-by: Rayane Chatrieux <rayane.chatrieux@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
2022-11-22py/makeversionhdr.py: Allow running outside of repo.Jim Mussared
If a CMake-build is run with `make BUILD=/outside/path` then makeversionheader.py is run with the CWD set to the build directory, which means the git version lookup will fail and silently fall back to the mpconfig.h mode (giving the wrong result). This commit: - Uses the location of makeversionheader.py to find the repo path. - Allows overriding this path via --repo-path. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-11-15py/nlrmips: Add native NLR support for MIPS architecture.Jan Willeke
This can be tested using ports/minimal and qemu: make CC=mips-linux-gnu-gcc-8 Then run with qemu-mips: stty raw opost -echo; QEMU_LD_PREFIX=/usr/mips-linux-gnu/ qemu-mips build/firmware.elf; sleep 1; reset Signed-off-by: Jan Willeke <willeke@smartmote.de>
2022-11-11py/emitnative: Ensure load_subscr does not clobber existing REG_ARG_2.Damien George
Follow up from a similar fix in 426785a19eeb12aef7383fbda4693575d8c4dddf Fixes issue #6314. Signed-off-by: Damien George <damien@micropython.org>
2022-11-08py/objarray: Detect bytearray(str) without an encoding.Jim Mussared
This prevents a very subtle bug caused by writing e.g. `bytearray('\xfd')` which gives you `(0xc3, 0xbd)`. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-11-08py/builtinimport: Fix crash handling "weak link" module names.Simon Arlott
There are two calls to mp_builtin___import__(): 1. ports/unix/main.c:main_() which provides a str in args[0] 2. py/runtime.c:mp_import_name() which provides a qstr in args[0] The default implementation of mp_builtin___import__() is mp_builtin___import___default() which has a different implementation based on MICROPY_ENABLE_EXTERNAL_IMPORT. If MICROPY_ENABLE_EXTERNAL_IMPORT is disabled then the handling of weak links assumes that args[0] is a `const char *`, when it is either a str or qstr object. Use the existing qstr of the module name instead, and also use a vstr instead of strcpy() to ensure no overflow occurs.
2022-10-27extmod/machine_timer: Move stm32's implementation of machine.Timer here.Damien George
So it can be reused by other ports. Signed-off-by: Damien George <damien@micropython.org>
2022-10-27py/makeversionhdr: Fall back to py/mpconfig.h instead of docs/conf.py.Damien George
Commit 64af916c111b61bce82c00f356a6b1cb81946d87 removed the version string from docs/conf.py. py/mpconfig.h is a better place to get the version from, so use that (when there is no git repository). Signed-off-by: Damien George <damien@micropython.org>
2022-10-26py/obj: Add comments explaining the slot index scheme.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-25py/persistentcode: Only emit sub-version if generated code has native.Jim Mussared
In order for v1.19.1 to load a .mpy, the formerly-feature-flags which are now used for the sub-version must be zero. The sub-version is only used to indicate a native version change, so it should be zero when emitting bytecode-only .mpy files. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-25py/obj: Verify floating point type is correct for repr C.Jim Mussared
Prevents double-precision floats being enabled on 32-bit architectures where they will not fit into the mp_obj_t encoding. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-12py/misc: Remove use of bitfield from vstr_t.David Lechner
Since there is only one flag, we don't need to use a bitfield in vstr_t. Compilers emit extra instructions to access a bitfield, so this should reduce the binary size a small amount. Signed-off-by: David Lechner <david@pybricks.com>
2022-10-11extmod: Make extmod.mk self-contained.Jim Mussared
This makes it so that all a port needs to do is set the relevant variables and "include extmod.mk" and doesn't need to worry about adding anything to OBJ, CFLAGS, SRC_QSTR, etc. Make all extmod variables (src, flags, etc) private to extmod.mk. Also move common/shared, extmod-related fragments (e.g. wiznet, cyw43, bluetooth) into extmod.mk. Now that SRC_MOD, CFLAGS_MOD, CXXFLAGS_MOD are unused by both extmod.mk (and user-C-modules in a previous commit), remove all uses of them from port makefiles. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-11py/py.mk: Make user-C-module handling self-contained in py.mk.Jim Mussared
Removes the need for the port to add anything to OBJS or SRC_QSTR. Also makes it possible for user-C-modules to differentiate between code that should be processed for QSTR vs other files (e.g. helpers and libraries). Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-11py/modsys: Add support for sys.executable.Jim Mussared
Only intended to be used on Unix and other "OS" ports. Matches CPython. This should give the absolute path to the executing binary. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com> Signed-off-by: Damien George <damien@micropython.org>
2022-10-11py/objstr: Add a helper to set mp_obj_str_t data.Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>