summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
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>
2021-05-10py/gc: Make gc_lock_depth have a count per thread.Damien George
This commit makes gc_lock_depth have one counter per thread, instead of one global counter. This makes threads properly independent with respect to the GC, in particular threads can now independently lock the GC for themselves without locking it for other threads. It also means a given thread can run a hard IRQ without temporarily locking the GC for all other threads and potentially making them have MemoryError exceptions at random locations (this really only occurs on MCUs with multiple cores and no GIL, eg on the rp2 port). The commit also removes protection of the GC lock/unlock functions, which is no longer needed when the counter is per thread (and this also fixes the cas where a hard IRQ calling gc_lock() may stall waiting for the mutex). It also puts the check for `gc_lock_depth > 0` outside the GC mutex in gc_alloc, gc_realloc and gc_free, to potentially prevent a hard IRQ from waiting on a mutex if it does attempt to allocate heap memory (and putting the check outside the GC mutex is now safe now that there is a gc_lock_depth per thread). Signed-off-by: Damien George <damien@micropython.org>
2021-05-02py/repl: Autocomplete builtin modules.Artyom Skrobov
Doing "import <tab>" will now complete/list built-in modules. Originally at adafruit#4548 and adafruit#4608 Signed-off-by: Artyom Skrobov <tyomitch@gmail.com>
2021-05-02py/repl: Refactor autocomplete, extracting reusable parts.Artyom Skrobov
Originally at adafruit#4548 Signed-off-by: Artyom Skrobov <tyomitch@gmail.com>
2021-05-02py/repl: Refactor autocomplete to reduce nesting.Artyom Skrobov
Originally at adafruit#4548 Signed-off-by: Artyom Skrobov <tyomitch@gmail.com>
2021-05-02py/repl: Enter four spaces when there are no matches.scottbelden
Originally at adafruit#1859 Signed-off-by: scottbelden <scottabelden@gmail.com>
2021-05-02py/repl: Filter private methods from tab completion.Kathryn Lingel
Anything beginning with "_" will now only be tab-completed if there is already a partial match for such an entry. In other words, entering foo.<tab> will no longer complete/list anything beginning with "_". Originally at adafruit#1850 Signed-off-by: Kathryn Lingel <kathryn@lingel.net>
2021-05-01py/scheduler: Add missing MICROPY_WRAP_MP_SCHED_EXCEPTION usage.Damien George
This was missed in commit 7cbf826a9575e18ce1b7fe11b0f0997509153260. Signed-off-by: Damien George <damien@micropython.org>
2021-04-30py/runtime: Remove commented-out code from mp_deinit().Damien George
These commented-out lines of code have been unused for a long time, so remove them to avoid confusion as to why they are there. mp_obj_dict_free() never existed, this line was converted from mp_map_deinit() and commented out as soon as it was added. The call to mp_map_deinit(mp_loaded_modules_map) was commented in 1a1d11fa32ba043d22995d28cbc039cfa5f3cc46. Fixes issue #3507. Signed-off-by: Damien George <damien@micropython.org>
2021-04-30py/scheduler: Add optional port hook for when something is scheduled.Damien George
So that a port can "wake up" when there is work to do. Signed-off-by: Damien George <damien@micropython.org>
2021-04-30all: Rename mp_keyboard_interrupt to mp_sched_keyboard_interrupt.Damien George
To match mp_sched_exception() and mp_sched_schedule(). Signed-off-by: Damien George <damien@micropython.org>
2021-04-30py/scheduler: Add mp_sched_exception() to schedule a pending exception.Damien George
This helper is added to properly set a pending exception, to mirror mp_sched_schedule(), which schedules a function. Signed-off-by: Damien George <damien@micropython.org>
2021-04-30py/profile: Use mp_handle_pending() to raise pending exception.Damien George
If MICROPY_ENABLE_SCHEDULER is enabled then MP_STATE_VM(sched_state) must be updated after handling the pending exception, which is done by the mp_handle_pending() function. Signed-off-by: Damien George <damien@micropython.org>
2021-04-28py/dynruntime.h: Add mp_obj_get_array() function.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-04-27py: Add option to compile without any error messages at all.Damien George
This introduces a new option, MICROPY_ERROR_REPORTING_NONE, which completely disables all error messages. To be used in cases where MicroPython needs to fit in very limited systems. Signed-off-by: Damien George <damien@micropython.org>
2021-04-23py/objexcept: Support errno attribute on OSError exceptions.Damien George
This commit adds the errno attribute to exceptions, so code can retrieve errno codes from an OSError using exc.errno. The implementation here simply lets `errno` (and the existing `value`) attributes work on any exception instance (they both alias args[0]). This is for efficiency and to keep code size down. The pros and cons of this are: Pros: - more compatible with CPython, less difference to document and learn - OSError().errno will correctly return None, whereas the current way of doing it via OSError().args[0] will raise an IndexError - it reduces code size on most bare-metal ports (because they already have the errno qstr) - for Python code that uses exc.errno the generated bytecode is 2 bytes smaller and more efficient to execute (compared with exc.args[0]); so bytecode loaded to RAM saves 2 bytes RAM for each use of this attribute, and bytecode that is frozen saves 2 bytes flash/ROM for each use - it's easier/shorter to type, and saves 2 bytes of space in .py files that use it (for each use) Cons: - increases code size by 4-8 bytes on minimal ports that don't already have the `errno` qstr - all exceptions now have .errno and .value attributes (a cpydiff test is added to address this) See also #2407. Signed-off-by: Damien George <damien@micropython.org>
2021-04-19all: Bump version to 1.15.v1.15Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-04-12py/runtime: Make sys.modules preallocate to a configurable size.matejcik
This allows configuring the pre-allocated size of sys.modules dict, in order to prevent unwanted reallocations at run-time (3 sys-modules is really not quite enough for a larger project).
2021-04-12py/profile: Resolve name collision with STATIC unset.matejcik
When building with STATIC undefined (e.g., -DSTATIC=), there are two instances of mp_type_code that collide at link time: in profile.c and in builtinevex.c. This patch resolves the collision by renaming one of them.
2021-04-09py/py.cmake: Introduce MICROPY_INC_CORE as a list with core includes.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-04-09py/py.cmake: Move qstr helper code to micropy_gather_target_properties.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-03-31rp2: Add support for USER_C_MODULES to CMake build system.Phil Howard
The parts that are generic are added to py/ so they can be used by other ports that use CMake. py/usermod.cmake: * Creates a usermod target to hang user C/CXX modules from. * Gathers sources from user C/CXX modules and libs for QSTR scan. ports/rp2/CMakeLists.txt: * Includes py/usermod.cmake. * Links the resulting usermod library to the MicroPython target. py/mkrules.cmake: Add cxxflags to qstr.i.last custom command for CXX modules: * MICROPY_CPP_FLAGS so CXX modules will find includes. * -DNO_QSTR to fix fatal error missing "genhdr/qstrdefs.generated.h". Usage: The rp2 port can be linked against user C modules by running: make USER_C_MODULES=/path/to/module/micropython.cmake CMake will print a list of included modules. Co-authored-by: Graham Sanderson <graham.sanderson@raspberrypi.org> Co-authored-by: Michael O'Cleirigh <michael.ocleirigh@rivulet.ca> Signed-off-by: Phil Howard <phil@pimoroni.com>
2021-03-26py: Rename remaining object types to be of the form mp_type_xxx.Damien George
For consistency with all other object types in the core. Signed-off-by: Damien George <damien@micropython.org>
2021-03-17py/vm: For tracing use mp_printf, and print state when thread enabled.Damien George
mp_printf should be used to print the prefix because it's also used in mp_bytecode_print2 (otherwise, depending on the system, different output streams may be used). Also print the current thread state when threading is enabled to easily see which thread executes what opcode. Signed-off-by: Damien George <damien@micropython.org>
2021-03-11py/nlrx64: Fix typo in comment.Yonatan Goldschmidt
2021-03-11py/nlr: Implement NLR for AArch64.Yonatan Goldschmidt
2021-02-21py/mkrules.cmake: Add MICROPY_QSTRDEFS_PORT to qstr build process.Damien George
This allows a port to specify a custom qstrdefsport.h file, the same as the QSTR_DEFS variable in a Makefile. Signed-off-by: Damien George <damien@micropython.org>
2021-02-21py/mkrules.cmake: Rename QSTR_DEFS variables to QSTRDEFS.Damien George
And also MICROPY_PY_QSTRDEFS to MICROPY_QSTRDEFS_PY. These variables are all related. Signed-off-by: Damien George <damien@micropython.org>