summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2021-08-20stm32: Disable computed goto on constrained boards.Jim Mussared
Saves ~1kiB. Add comment to this effect to mpconfig.h. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19py/mkrules.mk: Do submodule sync in "make submodules".Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19stm32: Add implementation of machine.bitstream.Jim Mussared
Hand-written version for M0, and cycle-counter version for everything else. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19extmod: Add machine.bitstream.Jim Mussared
This is a generic API for synchronously bit-banging data on a pin. Initially this adds a single supported encoding, which supports controlling WS2812 LEDs. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19py/lexer: Clear fstring_args vstr on lexer free.Jim Mussared
This was missed in 692d36d779192f32371f7f9daa845b566f26968d. It's not strictly necessary as the GC will clean it anyway, but it's good to pre-emptively gc_free() all the blocks used in lexing/parsing. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-14py: Implement partial PEP-498 (f-string) support.Jim Mussared
This implements (most of) the PEP-498 spec for f-strings and is based on https://github.com/micropython/micropython/pull/4998 by @klardotsh. It is implemented in the lexer as a syntax translation to `str.format`: f"{a}" --> "{}".format(a) It also supports: f"{a=}" --> "a={}".format(a) This is done by extracting the arguments into a temporary vstr buffer, then after the string has been tokenized, the lexer input queue is saved and the contents of the temporary vstr buffer are injected into the lexer instead. There are four main limitations: - raw f-strings (`fr` or `rf` prefixes) are not supported and will raise `SyntaxError: raw f-strings are not supported`. - literal concatenation of f-strings with adjacent strings will fail "{}" f"{a}" --> "{}{}".format(a) (str.format will incorrectly use the braces from the non-f-string) f"{a}" f"{a}" --> "{}".format(a) "{}".format(a) (cannot concatenate) - PEP-498 requires the full parser to understand the interpolated argument, however because this entirely runs in the lexer it cannot resolve nested braces in expressions like f"{'}'}" - The !r, !s, and !a conversions are not supported. Includes tests and cpydiffs. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-07py/mkrules: Automatically build mpy-cross if it doesn't exist.Damien George
Commit 41739506589ec8397613c86d8f682fb7f86c0a9f removed automatic building of mpy-cross, which rebuilt it whenever any of its dependent source files changed. But needing to build mpy-cross, and not knowing how, is a frequent issue. This commit aims to help by automatically building mpy-cross only if it doesn't exist. For Makefiles it uses an order-only prerequisite, while for CMake it uses a custom command. If MICROPY_MPYCROSS (which is what makemanifest.py uses to locate the mpy-cross executable) is defined in the environment then automatic build will not be attempted, allowing a way to prevent this auto-build if needed. Thanks to Trammell Hudson aka @osresearch for the original idea; see #5760. Signed-off-by: Damien George <damien@micropython.org>
2021-08-07extmod/modujson: Add support for dump/dumps separators keyword-argument.Peter Züger
Optionally enabled via MICROPY_PY_UJSON_SEPARATORS. Enabled by default. For dump, make sure mp_get_stream_raise is called after mod_ujson_separators since CPython does it in this order (if both separators and stream are invalid, separators will raise an exception first). Add separators argument in the docs as well. Signed-off-by: Peter Züger <zueger.peter@icloud.com> Signed-off-by: Damien George <damien@micropython.org>
2021-07-31py/builtinimport: Fix condition for including do_execute_raw_code().David Lechner
Commit e33bc597 ("py: Remove calls to file reader functions when these are disabled.") changed the condition for one caller of do_execute_raw_code() from MICROPY_PERSISTENT_CODE_LOAD to MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD The condition that enables compiling the function itself needs to be changed to match. Signed-off-by: David Lechner <david@pybricks.com>
2021-07-23py/runtime: Fix bool unary op for subclasses of native types.Jim Mussared
Previously a subclass of a type that didn't implement unary_op, or didn't handle MP_UNARY_OP_BOOL, would raise TypeError on bool conversion. Fixes #5677.
2021-07-19py/emitnative: Ensure stack settling is safe mid-branch.Jim Mussared
And add a test for the case where REG_RET could be in use. Fixes #7523. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-07-19py/emitnative: Reuse need_reg_all func in need_stack_settled.Damien George
To reduce code size and code duplication. Signed-off-by: Damien George <damien@micropython.org>
2021-07-17extmod/moduselect: Conditionally compile select().David Lechner
This adds #if MICROPY_PY_USELECT_SELECT around the uselect.select() function. According to the docs, this function is only for CPython compatibility and should not normally be used. So we can disable it and save a few bytes of flash space where possible. Signed-off-by: David Lechner <david@pybricks.com>
2021-07-15py/obj: Fix formatting of comment for mp_obj_is_integer.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/objexcept: Make mp_obj_new_exception_arg1 inline.Damien George
This function is rarely used so making it inline reduces code size. Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/modsys: Optimise sys.exit for code size by using exception helpers.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py: Introduce and use mp_raise_type_arg helper.Damien George
To reduce code size. Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py: Support single argument to optimised MP_OBJ_STOP_ITERATION.Damien George
The MP_OBJ_STOP_ITERATION optimisation is a shortcut for creating a StopIteration() exception object, and means that heap memory does not need to be allocated for the exception (in cases where it can be used). This commit allows this optimised object to take an optional argument (before, it could only have no argument). The commit also adds some new tests to cover corner cases with StopIteration and generators that previously did not work. Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/objexcept: Make mp_obj_exception_get_value support subclassed excs.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/vm: Simplify handling of MP_OBJ_STOP_ITERATION in yield-from opcode.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-12all: Update to point to files in new shared/ directory.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-08windows/Makefile: Add .exe extension to executables name.Bryan Tong Minh
Uses the same logic applied in 5b57ae985ff7064dd7b09b0ce891697bfaa5dae2 to determine when to add .exe. See related: #3310, #3361, #3370, #4143, #5727.
2021-07-05py/makeversionhdr: Add --tags arg to git describe.David Lechner
This adds the --tags argument to the git describe command that is used to define the MICROPY_GIT_TAG macro. This makes it match non-annotated tags. This is useful for MicroPython derivatives that don't use annotated tags. Signed-off-by: David Lechner <david@pybricks.com>
2021-07-01py/objexcept: Pretty print OSError also when it has 2 arguments.David Lechner
This extends pretty-printing of OSError's to handle two arguments when the exception name is known. Signed-off-by: David Lechner <david@pybricks.com>
2021-06-28tools/makemanifest.py: Allow passing flags to mpy-tool.py.Yonatan Goldschmidt
2021-06-25py: Mark unused arguments from bytecode decoding macros.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-06-25py/makeqstrdefs.py: Don't include .h files explicitly in preprocessing.Damien George
Only include .c and .cpp files explicitly in the list of files passed to the preprocessor for QSTR extraction. All relevant .h files will be included in this process by "#include" from the .c(pp) files. In particular for moduledefs.h, this is included by py/objmodule.c (and doesn't actually contain any extractable MP_QSTR_xxx, but rather defines macros with MP_QSTR_xxx's in them which are then part of py/objmodule.c). The main reason for this change is to simplify the preprocessing step on the javascript port, which tries to compile .h files as C++ precompiled headers if they are passed with -E to clang. Signed-off-by: Damien George <damien@micropython.org>
2021-06-24py/mperrno: Add MP_ECANCELED error code.David Lechner
This is useful when binding asynchronous functions in C. Signed-off-by: David Lechner <david@pybricks.com>
2021-06-24all: Fix signed shifts and NULL access errors from -fsanitize=undefined.Jeff Epler
Fixes the following (the line numbers match commit 0e87459e2bfd07): ../../extmod/crypto-algorithms/sha256.c:49:19: runtime error: left shif... ../../extmod/moduasyncio.c:106:35: runtime error: member access within ... ../../py/binary.c:210:13: runtime error: left shift of negative value -... ../../py/mpz.c:744:16: runtime error: negation of -9223372036854775808 ... ../../py/objint.c:109:22: runtime error: left shift of 1 by 31 places c... ../../py/objint_mpz.c:374:9: runtime error: left shift of 4611686018427... ../../py/objint_mpz.c:374:9: runtime error: left shift of negative valu... ../../py/parsenum.c:106:14: runtime error: left shift of 46116860184273... ../../py/runtime.c:395:33: runtime error: left shift of negative value ... ../../py/showbc.c:177:28: runtime error: left shift of negative value -... ../../py/vm.c:321:36: runtime error: left shift of negative value -1``` Testing was done on an amd64 Debian Buster system using gcc-8.3 and these settings: CFLAGS += -g3 -Og -fsanitize=undefined LDFLAGS += -fsanitize=undefined The introduced TASK_PAIRHEAP macro's conditional (x ? &x->i : NULL) assembles (under amd64 gcc 8.3 -Os) to the same as &x->i, since i is the initial field of the struct. However, for the purposes of undefined behavior analysis the conditional is needed. Signed-off-by: Jeff Epler <jepler@gmail.com>
2021-06-19py/mpstate: Schedule KeyboardInterrupt on main thread.David Lechner
This introduces a new macro to get the main thread and uses it to ensure that asynchronous exceptions such as KeyboardInterrupt (CTRL+C) are only scheduled on the main thread. This is more deterministic than being scheduled on a random thread and is more in line with CPython that only allow signal handlers to run on the main thread. Fixes issue #7026. Signed-off-by: David Lechner <david@pybricks.com>
2021-06-19py/mpstate: Make exceptions thread-local.David Lechner
This moves mp_pending_exception from mp_state_vm_t to mp_state_thread_t. This allows exceptions to be scheduled on a specific thread. Signed-off-by: David Lechner <david@pybricks.com>
2021-06-18all: Bump version to 1.16.v1.16Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-06-18py/gc: Only use no_sanitize_address attribute for GCC 4.8 and above.Damien George
It's not supported on older GCC versions. Signed-off-by: Damien George <damien@micropython.org>
2021-06-05py/stackctrl: Prevent unused-var warning when stack checking disabled.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-06-05py/emitglue: Always flush caches when assigning native ARM code.Damien George
Prior to this commit, cache flushing for ARM native code was done only in the assembler code asm_thumb_end_pass()/asm_arm_end_pass(), at the last pass of the assembler. But this misses flushing the cache when loading native code from an .mpy file, ie in persistentcode.c. The change here makes sure the cache is always flushed/cleaned/invalidated when assigning native code on ARM architectures. This problem was found running tests/micropython/import_mpy_native_gc.py on the mimxrt port. Signed-off-by: Damien George <damien@micropython.org>
2021-05-30py/builtinimport: Change relative import's ValueError to ImportError.Damien George
Following CPython change, see https://bugs.python.org/issue37444. Signed-off-by: Damien George <damien@micropython.org>
2021-05-30py/repl: Don't read past the end of import_str.Jeff Epler
asan considers that memcmp(p, q, N) is permitted to access N bytes at each of p and q, even for values of p and q that have a difference earlier. Accessing additional values is frequently done in practice, reading 4 or more bytes from each input at a time for efficiency, so when completing "non_exist<TAB>" in the repl, this causes a diagnostic: ==16938==ERROR: AddressSanitizer: global-buffer-overflow on address 0x555555cd8dc8 at pc 0x7ffff726457b bp 0x7fffffffda20 sp 0x7fff READ of size 9 at 0x555555cd8dc8 thread T0 #0 0x7ffff726457a (/usr/lib/x86_64-linux-gnu/libasan.so.5+0xb857a) #1 0x555555b0e82a in mp_repl_autocomplete ../../py/repl.c:301 #2 0x555555c89585 in readline_process_char ../../lib/mp-readline/re #3 0x555555c8ac6e in readline ../../lib/mp-readline/readline.c:513 #4 0x555555b8dcbd in do_repl /home/jepler/src/micropython/ports/uni #5 0x555555b90859 in main_ /home/jepler/src/micropython/ports/unix/ #6 0x555555b90a3a in main /home/jepler/src/micropython/ports/unix/m #7 0x7ffff619a09a in __libc_start_main ../csu/libc-start.c:308 #8 0x55555595fd69 in _start (/home/jepler/src/micropython/ports/uni 0x555555cd8dc8 is located 0 bytes to the right of global variable 'import_str' defined in '../../py/repl.c:285:23' (0x555555cd8dc0) of size 8 'import_str' is ascii string 'import ' Signed-off-by: Jeff Epler <jepler@gmail.com>
2021-05-30py/gc: Access the list of root pointers in an asan-compatible way.Jeff Epler
Signed-off-by: Jeff Epler <jepler@gmail.com>
2021-05-30py/compile: Raise an error on async with/for outside an async function.Jeff Epler
A simple reproducer is: async for x in (): x Before this change, it would cause an assertion error in mpy-cross and micropython-coverage.
2021-05-26py/asmarm: Use builtin func to flush I- and D-cache on ARM 7 archs.Damien George
The inline assembler code does not work for __ARM_ARCH == 7. Signed-off-by: Damien George <damien@micropython.org>
2021-05-23py/mkrules.cmake: Add MPY_LIB_DIR and BOARD_DIR to makemanifest call.Damien George
So that the FROZEN_MANIFEST option in cmake works the same as make. Signed-off-by: Damien George <damien@micropython.org>
2021-05-20py/emitnative: Fix x86-64 emitter to generate correct 8/16-bit stores.Damien George
Fixes issue #6643. Signed-off-by: Damien George <damien@micropython.org>
2021-05-20py/asmx64: Support use of top 8 regs in src_r64 argument.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-05-18py/nlrx64: Correct the detection of Darwin ABI.Bob Abeles
__APPLE__ tests for an Apple OS and __MACH__ tests that it is based on CMU Mach. Using both tests ensures that just Darwin is recognized.
2021-05-18py/nlraarch64: Add underscore prefix to function symbols for Darwin ABI.Bob Abeles
The proper way to do this is to test for __APPLE__ and __MACH__, where __APPLE__ tests for an Apple OS and __MACH__ tests that it is based on CMU Mach. Using both tests ensures that just Darwin (Apple's open source base for MacOS, iOS, etc.) is recognized. __APPLE__ by itself will test for any Apple OS, which can include older OS 7-9 and any future Apple OS. __MACH__ tests for any OS based on CMU Mach, including Darwin and GNU Hurd. Fixes #7232.
2021-05-18py/objarray: Fix constructing a memoryview from a memoryview.Damien George
Fixes issue #7261. Signed-off-by: Damien George <damien@micropython.org>
2021-05-18py/objarray: Use mp_obj_memoryview_init helper in mp_obj_new_memoryview.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-05-13py/objarray: Implement more/less comparisons for array.stijn
2021-05-13py/objarray: Prohibit comparison of mismatching types.stijn
Array equality is defined as each element being equal but to keep code size down MicroPython implements a binary comparison. This can only be used correctly for elements with the same binary layout though so turn it into an NotImplementedError when comparing types for which the binary comparison yielded incorrect results: types with different sizes, and floating point numbers because nan != nan.
2021-05-12py/mkenv.mk: Don't emit info about BUILD_VERBOSE if it's set.Damien George
If the user sets V or BUILD_VERBOSE then they don't need to see this message. Signed-off-by: Damien George <damien@micropython.org>