summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2024-02-16py/emitglue: Provide a truncated mp_raw_code_t for non-asm code.Damien George
The `asm_n_pos_args` and `asm_type_sig` members of `mp_raw_code_t` are only used for raw codes of type MP_CODE_NATIVE_ASM, which are rare, for example in frozen code. So using a truncated `mp_raw_code_t` in these cases helps to reduce frozen code size on targets that have MICROPY_EMIT_INLINE_ASM enabled. With this, change in firmware size of RPI_PICO builds is -648. Signed-off-by: Damien George <damien@micropython.org>
2024-02-16py/emitglue: Reorder and resize members of mp_raw_code_t.Damien George
The mp_raw_code_t struct has been reordered and some members resized. The `n_pos_args` member is renamed to `asm_n_pos_args`, and `type_sig` renamed to `asm_type_sig` to indicate that these are used only for the inline-asm emitters. These two members are also grouped together in the struct. The justifications for resizing the members are: - `fun_data_len` can be 32-bits without issue - `n_children` is already limited to 16-bits by `mp_emit_common_t::ct_cur_child` - `scope_flags` is already limited to 16-bits by `scope_t::scope_flags` - `prelude_offset` is already limited to 16-bits by the argument to `mp_emit_glue_assign_native()` - it's reasonable to limit the maximim number of inline-asm arguments to 12 (24 bits for `asm_type_sig` divided by 2) This change helps to reduce frozen code size (and in some cases RAM usage) in the following cases: - 64-bit targets - builds with MICROPY_PY_SYS_SETTRACE enabled - builds with MICROPY_EMIT_MACHINE_CODE enabled but MICROPY_EMIT_INLINE_ASM disabled With this change, unix 64-bit builds are -4080 bytes in size. Bare-metal ports like rp2 are unchanged (because mp_raw_code_t is still 32 bytes on those 32-bit targets). Signed-off-by: Damien George <damien@micropython.org>
2024-02-07extmod/modvfs: Add new "vfs" module with mount/umount and Vfs classes.Damien George
They have been moved from the "os" module. Signed-off-by: Damien George <damien@micropython.org>
2024-01-31py/compile: Fix potential Py-stack overflow in try-finally with return.Damien George
If a return is executed within the try block of a try-finally then the return value is stored on the top of the Python stack during the execution of the finally block. In this case the Python stack is one larger than it normally would be in the finally block. Prior to this commit, the compiler was not taking this case into account and could have a Python stack overflow if the Python stack used by the finally block was more than that used elsewhere in the function. In such a scenario the last argument of the function would be clobbered by the top-most temporary value used in the deepest Python expression/statement. This commit fixes that case by making sure enough Python stack is allocated to the function. Fixes issue #13562. Signed-off-by: Damien George <damien@micropython.org>
2024-01-30py/builtinimport: Simplify calls to stat_path().Matthias Urlichs
stat_path is only called with stringified vstr_t objects. Thus, pulling the stringification into the function replaces three function calls with one, saving a few bytes. Signed-off-by: Matthias Urlichs <matthias@urlichs.de>
2024-01-25py/mpconfig: Disable qstr hashing at minimum feature level.Jim Mussared
This will apply to bare-arm and minimal, as well as the minimal unix variant. Change the default to MICROPY_QSTR_BYTES_IN_HASH=1 for the CORE,BASIC levels, 2 for >=EXTRA. Removes explicit setting of MICROPY_QSTR_BYTES_IN_HASH==1 in ports that don't set the feature level (because 1 is implied by the default level, CORE). Applies to cc3200, pic16bt, powerpc. Removes explicit setting for nRF (which sets feature level). Also for samd, which sets CORE for d21 and FULL for d51. This means that d21 is unchanged with MICROPY_QSTR_BYTES_IN_HASH==1, but d51 now moves from 1 to 2 (roughly adds 1kiB). The only remaining port which explicitly set bytes-in-hash is rp2 because it's high-flash (hence CORE level) but lowish-SRAM, so it's worthwhile saving the RAM for runtime qstrs. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2024-01-25py/qstr: Add support for MICROPY_QSTR_BYTES_IN_HASH=0.Jim Mussared
This disables using qstr hashes altogether, which saves RAM and flash (two bytes per interned string on a typical build) as well as code size. On PYBV11 this is worth over 3k flash. qstr comparison will now be done just by length then data. This affects qstr_find_strn although this has a negligible performance impact as, for a given comparison, the length and first character will ~usually be different anyway. String hashing (e.g. builtin `hash()` and map.c) now need to compute the hash dynamically, and for the map case this does come at a performance cost. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2024-01-24ports: Fix handling of paths containing spaces in Makefiles.Iksas
Make can't handle paths with spaces, see https://savannah.gnu.org/bugs/?712 The following workarounds exist: - When using make's built-in functions: - Use relative paths wherever possible to avoid spaces in the first place. - All spaces in paths can be escaped with backslashes; quotes don't work. - Some users use the shell to temporarily rename directories, or to create symlinks without spaces. - When using make to pass commands to the system's shell, enclose paths in quotes. While make will still interpret quoted strings with spaces as multiple words, the system's shell will correctly parse the resulting command. This commit contains the following fixes: - In ports/stm32/mboot/Makefile: Use relative paths to avoid spaces when using built-in functions. - In all other files: Use quotes to enclose paths when make is used to call shell functions. All changes have been tested with a directory containing spaces. Signed-off-by: Iksas <iksas@mailbox.org>
2024-01-17py/py.mk: Remove extra build dir created for frozen_content.iabdalkader
This was originally needed because the .c --> .o rule is: $(BUILD)/%.o: %.c and because the generated frozen_content.c is inside build-FOO, it must therefore generate build-FOO/build-FOO/frozen_content.o. But 2eda5138701d6a7d36f8d8e3700d136b3c1161b7 added a new build rule for pins.c that can also be used for frozen_content.c. Signed-off-by: iabdalkader <i.abdalkader@gmail.com>
2024-01-02all: Bump version to 1.23.0-preview.v1.23.0-previewDamien George
Signed-off-by: Damien George <damien@micropython.org>
2023-12-27all: Bump version to 1.22.0.v1.22.0Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-12-22py/mkrules.mk: Fix dependency file generation for compiler wrappers.Peter Züger
When compiling with distcc, it does not understand the -MD flag on its own. This fixes the interaction by explicitly adding the -MF option. The error in distcc is described here under "Problems with gcc -MD": https://www.distcc.org/faq.html Signed-off-by: Peter Züger <zueger.peter@icloud.com>
2023-12-22ports: Fix sys.stdout.buffer.write() return value.Maarten van der Schrieck
MicroPython code may rely on the return value of sys.stdout.buffer.write() to reflect the number of bytes actually written. While in most scenarios a write() operation is successful, there are cases where it fails, leading to data loss. This problem arises because, currently, write() merely returns the number of bytes it was supposed to write, without indication of failure. One scenario where write() might fail, is where USB is used and the receiving end doesn't read quickly enough to empty the receive buffer. In that case, write() on the MicroPython side can timeout, resulting in the loss of data without any indication, a behavior observed notably in communication between a Pi Pico as a client and a Linux host using the ACM driver. A complex issue arises with mp_hal_stdout_tx_strn() when it involves multiple outputs, such as USB, dupterm and hardware UART. The challenge is in handling cases where writing to one output is successful, but another fails, either fully or partially. This patch implements the following solution: mp_hal_stdout_tx_strn() attempts to write len bytes to all of the possible destinations for that data, and returns the minimum successful write length. The implementation of this is complicated by several factors: - multiple outputs may be enabled or disabled at compiled time - multiple outputs may be enabled or disabled at runtime - mp_os_dupterm_tx_strn() is one such output, optionally containing multiple additional outputs - each of these outputs may or may not be able to report success - each of these outputs may or may not be able to report partial writes As a result, there's no single strategy that fits all ports, necessitating unique logic for each instance of mp_hal_stdout_tx_strn(). Note that addressing sys.stdout.write() is more complex due to its data modification process ("cooked" output), and it remains unchanged in this patch. Developers who are concerned about accurate return values from write operations should use sys.stdout.buffer.write(). This patch might disrupt some existing code, but it's also expected to resolve issues, considering that the peculiar return value behavior of sys.stdout.buffer.write() is not well-documented and likely not widely known. Therefore, it's improbable that much existing code relies on the previous behavior. Signed-off-by: Maarten van der Schrieck <maarten@thingsconnected.nl>
2023-12-19py/gc: Improve calculation of new heap size in split-heap-auto mode.Damien George
There are two main changes here to improve the calculation of the size of the next heap area when automatically expanding the heap: - Compute the existing total size by counting the total number of GC blocks, and then using that to compute the corresponding number of bytes. - Round the bytes value up to the nearest multiple of BYTES_PER_BLOCK. This makes the calculation slightly simpler and more accurate, and makes sure that, in the case of growing from one area to two areas, the number of bytes allocated from the system for the second area is the same as the first. For example on esp32 with an initial area size of 65536 bytes, the subsequent allocation is also 65536 bytes. Previously it was a number that was not even a multiple of 2. Signed-off-by: Damien George <damien@micropython.org>
2023-12-14py/makeqstrdefs.py: Stop generating temporary intermediate file.Trent Piepho
In "cat" mode, output was written to a file named "out", then moved to the location of the real output file. There was no reason for this. While makeqstrdefs.py does make an effort to not update the timestamp on an existing output file that has not changed, the intermediate "out" file isn't part of the that process. Signed-off-by: Trent Piepho <tpiepho@gmail.com>
2023-12-15py/makeqstrdefs.py: Don't skip output for stale hash file.Trent Piepho
In "cat" mode a "$output_file.hash" file is checked to see if the hash of the new output is the same as the existing, and if so the output file isn't updated. However, it's possible that the output file has been deleted but the hash file has not. In this case the output file is not created. Change the logic so that a hash file is considered stale if there is no output file and still create the output. Signed-off-by: Trent Piepho <tpiepho@gmail.com>
2023-12-15py/mkrules.mk: List hash files as byproducts.Trent Piepho
These are produced by the "cat" command to makeqstrdefs.py, to allow it to not update unchanged files. cmake doesn't know about them and so they are not removed on a "clean". This triggered a bug in makeqstrdefs.py where it would not recreate a deleted output file (which is removed by clean) if a stale hash file with a valid hash still existed. Listing them as byproducts will cause them to be deleted on clean. Signed-off-by: Trent Piepho <tpiepho@gmail.com>
2023-12-15py/modsys: Implement optional sys.intern.stijn
Signed-off-by: stijn <stijn@ignitron.net>
2023-12-08py: Add port-agnostic inline functions for event handling.Angus Gratton
These are intended to replace MICROPY_EVENT_POLL_HOOK and MICROPY_EVENT_POLL_HOOK_FAST, which are insufficient for tickless ports. This implementation is along the lines suggested here: https://github.com/micropython/micropython/issues/12925#issuecomment-1803038430 Currently any usage of these functions expands to use the existing hook macros, but this can be switched over port by port. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2023-12-01py/mphal: Move configuration of ATOMIC_SECTION macros to mphal.h.Damien George
MICROPY_BEGIN_ATOMIC_SECTION/MICROPY_END_ATOMIC_SECTION belong more to the MicroPython HAL rather than build configuration settings, so move their default configuration to py/mphal.h, and require all users of these macros to include py/mphal.h (here, py/objexcept.c and py/scheduler.c). This helps ports separate configuration from their HAL implementations, and can improve build times (because mpconfig.h is included everywhere, whereas mphal.h is not). Signed-off-by: Damien George <damien@micropython.org>
2023-11-28py/modbuiltins: Share vstr_add_char's implementation of utf8 encoding.Jeff Epler
This saves ~84 bytes on trinket m0, and saves 112 bytes on PYBV10. Signed-off-by: Jeff Epler <jepler@gmail.com>
2023-11-28py/mkrules: Add support for custom manifest variables.Jim Mussared
This allows e.g. a board (or make command line) to set MICROPY_MANIFEST_MY_VARIABLE = path/to/somewhere set(MICROPY_MANIFEST_MY_VARIABLE path/to/somewhere) and then in the manifest.py they can query this, e.g. via include("$(MY_VARIABLE)/path/manifest.py") Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-21py/objslice: Validate that the argument to indices() is an integer.Damien George
Otherwise passing in a non-integer can lead to an invalid memory access. Thanks to Junwha Hong and Wonil Jang @S2Lab, UNIST for finding the issue. Fixes issue #13007. Signed-off-by: Damien George <damien@micropython.org>
2023-11-17py/obj: Fix mp_obj_is_type compilation with C++.stijn
Fixes issue #12951. Signed-off-by: stijn <stijn@ignitron.net>
2023-11-09extmod/vfs_reader: Add file ioctl to set read buffer size.Andrew Leech
Can be used to speed up importing a file from a vfs based filesystem. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
2023-11-07windows: Use the MicroPython logo as application icon.stijn
Add a .ico file with common icon image size, created from vector-logo-2.png, and embed it into the resulting executable. Signed-off-by: stijn <stijn@ignitron.net>
2023-11-07py/qstr: Special case qstr_find_strn for empty string.Jim Mussared
This handles the case where an empty bytes/bytearray/str could pass in NULL as the str argument (with length zero). This would result in UB in strncmp. Even though our bare-metal implementation of strncmp handles this, best to avoid it for when we're using system strncmp. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-03py/misc: Change sizeof to offsetof for variable-length alloc.Jim Mussared
This fixes the case where e.g. struct foo_t { mp_obj_t x; uint16_t y; char buf[]; }; will have `sizeof(struct foo_t)==8`, but `offsetof(struct foo_t, buf)==6`. When computing the size to allocate for `m_new_obj_var` we need to use offsetof to avoid over-allocating. This is important especially when it might cause it to spill over into another GC block. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-03py/lexer: Change token position for new lines.Mathieu Serandour
Set the position of new line tokens as the end of the preceding line instead of the beginning of the next line. This is done by first moving the pointer to the end of the current line to skip any whitespace, record the position for the token, then finaly skip any other line and whitespace. The previous behavior was to skip every new line and whitespace, including the indent of the next line, before recording the token position. (Note that both lex->emit_dent and lex->nested_bracket_level equal 0 if had_physical_newline == true, which allows simplifying the if-logic for MP_TOKEN_NEWLINE.) And update the cmd_parsetree.py test expected output, because the position of the new-line token has changed. Fixes issue #12792. Signed-off-by: Mathieu Serandour <mathieu.serandour@numworks.fr>
2023-11-03py/runtime: Remove declaration of function from inside function.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-11-03py/mkrules.mk: Add rule for compiling auto-generated source files.Jim Mussared
This prevents each port Makefile from having to add an explicit rule for `build-BOARD/pins_BOARD.c`. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-11-01esp32: Use better build settings for ESP32-C3.Alessandro Gatti
ESP32-C3 is not Xtensa-based, so build settings are now tailored a bit better following that fact. ESP-IDF 5.x already adds architecture-specific modules by itself so there is no need to specify either the `xtensa` or the `riscv` module in the build settings. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2023-10-30py/qstr: Add support for sorted qstr pools.Jim Mussared
This provides a significant performance boost for qstr_find_strn, which is called a lot during parsing and loading of .mpy files, as well as interning of string objects (which happens in most string methods that return new strings). Also adds comments to explain the "static" qstrs. These are part of the .mpy ABI and avoid needing to duplicate string data for QSTRs known to already be in the firmware. The static pool isn't currently sorted, but in the future we could either split the static pool into the sorted regions, or in the next .mpy version just sort them. Based on initial work done by @amirgon in #6896. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-27py/asm{arm,thumb,x64,x86,xtensa}: Remove unused macros.Alessandro Gatti
`ASM_MOV_REG_IMM_FIX_U16` and `ASM_MOV_REG_IMM_FIX_WORD` are no longer used anywhere in the code. See discussion in #12771. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
2023-10-27py/mkrules.mk: Add MICROPY_PREVIEW_VERSION_2.Jim Mussared
This provides a way to enable features and changes slated for MicroPython 2.x, by running `make MICROPY_PREVIEW_VERSION_2=1`. Also supported for the cmake ports (except Zephyr). This is an alternative to having a 2.x development branch (or equivalently, keeping a 1.x release branch). Any feature or change that needs to be "hidden" until 2.x can use this flag (either in the Makefile or the preprocessor). A good example is changing function arguments or other public API features, in particular to aid in improving consistency between ports. When `MICROPY_PREVIEW_VERSION_2` is enabled, the REPL banner is amended to say "MicroPython (with v2.0 preview) vX.Y.Z", and sys.implementation gets a new field `_v2` set to `True`. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-23py/makeqstrdefs.py: Print a nicer error when preprocessing stage fails.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-10-18py/modthread: Initialise nlr_jump_callback_top on threads.Jim Mussared
The main thread gets this because the thread state is in bss, but subsequent threads need this field to be initialised. Also added a note to mpstate.h to help avoid missing this in the future. Fixes issue #12695. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-16py/persistentcode: Bump .mpy sub-version.Damien George
This is required because the previous commit changed the .mpy native ABI. Signed-off-by: Damien George <damien@micropython.org>
2023-10-16py/dynruntime: Add mp_get_buffer.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-10-16py/obj: Generalise mp_get_buffer so it can raise if a flag is set.Damien George
This allows mp_get_buffer_raise() to be changed to a simple inline function that in the majority of cases costs the same (in code size) to call as the original mp_get_buffer_raise(), because the flags argument is a constant. Signed-off-by: Damien George <damien@micropython.org>
2023-10-13py/objboundmeth: Optimise check for types in binary_op.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-10-13py/objboundmeth: Support comparing and hashing bound methods.Daniël van de Giessen
This behaviour matches CPython. It's useful to be able to store bound method objects in dicts/sets, and compare for equality, eg when storing them in a list and using list.remove(). Signed-off-by: Daniël van de Giessen <daniel@dvdgiessen.nl>
2023-10-12py/builtinevex: Handle invalid filenames for execfile.Jim Mussared
If a non-string buffer was passed to execfile, then it would be passed as a non-null-terminated char* to mp_lexer_new_from_file. This changes mp_lexer_new_from_file to take a qstr instead (as in almost all cases a qstr will be created from this input anyway to set the `__file__` attribute on the module). This now makes execfile require a string (not generic buffer) argument, which is probably a good fix to make anyway. Fixes issue #12522. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-09py/vm: Don't emit warning when using "raise ... from None".Matthias Urlichs
"Raise SomeException() from None" is a common Python idiom to suppress chained exceptions and thus shouldn't trigger a warning on a version of Python that doesn't support them in the first place.
2023-10-06all: Switch to new preview build versioning scheme.v1.22.0-previewJim Mussared
See https://github.com/micropython/micropython/issues/12127 for details. Previously at the point when a release is made, we update mpconfig.h and set a git tag. i.e. the version increments at the release. Now the version increments immediately after the release. The workflow is: 1. Final commit in the cycle updates mpconfig.h to set (X, Y, 0, 0) (i.e. clear the pre-release state). 2. This commit is tagged "vX.Y.0". 3. First commit for the new cycle updates mpconfig.h to set (X, Y+1, 0, 1) (i.e. increment the minor version, set the pre-release state). 4. This commit is tagged "vX.Y+1.0-preview". The idea is that a nightly build is actually a "preview" of the _next_ release. i.e. any documentation describing the current release may not actually match the nightly build. So we use "preview" as our semver pre-release identifier. Changes in this commit: - Add MICROPY_VERSION_PRERELEASE to mpconfig.h to allow indicating that this is not a release version. - Remove unused MICROPY_VERSION integer. - Append "-preview" to MICROPY_VERSION_STRING when the pre-release state is set. - Update py/makeversionhdr.py to no longer generate MICROPY_GIT_HASH. - Remove the one place MICROPY_GIT_HASH was used (it can use MICROPY_GIT_TAG instead). - Update py/makeversionhdr.py to also understand MICROPY_VERSION_PRERELEASE in mpconfig.h. - Update py/makeversionhdr.py to convert the git-describe output into semver-compatible "X.Y.Z-preview.N.gHASH". - Update autobuild.sh to generate filenames using the new scheme. - Update remove_old_firmware.py to match new scheme. - Update mpremote's pyproject.toml to handle the "-preview" suffix in the tag. setuptools_scm maps to this "rc0" to match PEP440. - Fix docs heading where it incorrectly said "vvX.Y.Z" for release docs. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-10-06all: Bump version to 1.21.0.v1.21.0Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-10-03all: Fix various spelling mistakes found by codespell 2.2.6.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2023-09-29py/lexer: Add missing initialisation for fstring_args_idx.Jim Mussared
This was missed in 692d36d779192f32371f7f9daa845b566f26968d. Probably never noticed because everything enables `MICROPY_GC_CONSERVATIVE_CLEAR`, but found via ASAN thanks to @gwangmu & @chibinz. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-09-29py: Change ifdef DEBUG_PRINT to if DEBUG_PRINT.Ihor Nehrutsa
Signed-off-by: Ihor Nehrutsa <Ihor.Nehrutsa@gmail.com>
2023-09-29py/mkrules.mk: Don't strip binary if STRIP variable is unset.Angus Gratton
This provides a way to build a non-DEBUG host binary that still has symbols and debug information. Document this for the unix port, and update a comment in the unix port Makefile. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>