summaryrefslogtreecommitdiff
path: root/lib/utils/pyexec.c
AgeCommit message (Collapse)Author
2021-07-12shared: Introduce new top-level dir and move 1st party lib code there.Damien George
This commit moves all first-party code developed for this project from lib/ to shared/, so that lib/ now only contains third-party code. The following directories are moved as-is from lib to shared: lib/libc -> shared/libc lib/memzip -> shared/memzip lib/netutils -> shared/netutils lib/timeutils -> shared/timeutils lib/upytesthelper -> shared/upytesthelper All files in lib/embed/ have been moved to shared/libc/. lib/mp-readline has been moved to shared/readline. lib/utils has been moved to shared/runtime, with the exception of lib/utils/printf.c which has been moved to shared/libc/printf.c. 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-01-30lib/utils/pyexec: Remove obsolete LCD initialization.stijn
This was added a long time ago in 75abee206d1a575aa98a486d043c94d64df432c1 when USB host support was added to the stm (now stm32) port, and when this pyexec code was actually part of the stm port. It's unlikely to work as intended anymore. If it is needed in the future then generic hook macros can be added in pyexec.
2020-11-30lib/utils/pyexec: Add stdin-reader on raw REPL with flow control.Damien George
Background: the friendly/normal REPL is intended for human use whereas the raw REPL is for computer use/automation. Raw REPL is used for things like pyboard.py script_to_run.py. The normal REPL has built-in flow control because it echos back the characters. That's not so with raw REPL and flow control is just implemented by rate limiting the amount of data that goes in. Currently it's fixed at 256 byte chunks every 10ms. This is sometimes too fast for slow MCUs or systems with small stdin buffers. It's also too slow for a lot of higher-end MCUs, ie it could be a lot faster. This commit adds a new raw REPL mode which includes flow control: the device will echo back a character after a certain number of bytes are sent to the host, and the host can use this to regulate the data going out to the device. The amount of characters is controlled by the device and sent to the host before communication starts. This flow control allows getting the maximum speed out of a serial link, regardless of the link or the device at the other end. Also, this new raw REPL mode parses and compiles the incoming data as it comes in. It does this by creating a "stdin reader" object which is then passed to the lexer. The lexer requests bytes from this "stdin reader" which retrieves bytes from the host, and does flow control. What this means is that no memory is used to store the script (in the existing raw REPL mode the device needs a big buffer to read in the script before it can pass it on to the lexer/parser/compiler). The only memory needed on the device is enough to parse and compile. Finally, it would be possible to extend this new raw REPL to allow bytecode (.mpy files) to be sent as well as text mode scripts (but that's not done in this commit). Some results follow. The test was to send a large 33k script that contains mostly comments and then prints out the heap, run via pyboard.py large.py. On PYBD-SF6, prior to this PR: $ ./pyboard.py large.py stack: 524 out of 23552 GC: total: 392192, used: 34464, free: 357728 No. of 1-blocks: 12, 2-blocks: 2, max blk sz: 2075, max free sz: 22345 GC memory layout; from 2001a3f0: 00000: h=hhhh=======================================hhBShShh==h=======h 00400: =====hh=B........h==h=========================================== 00800: ================================================================ 00c00: ================================================================ 01000: ================================================================ 01400: ================================================================ 01800: ================================================================ 01c00: ================================================================ 02000: ================================================================ 02400: ================================================================ 02800: ================================================================ 02c00: ================================================================ 03000: ================================================================ 03400: ================================================================ 03800: ================================================================ 03c00: ================================================================ 04000: ================================================================ 04400: ================================================================ 04800: ================================================================ 04c00: ================================================================ 05000: ================================================================ 05400: ================================================================ 05800: ================================================================ 05c00: ================================================================ 06000: ================================================================ 06400: ================================================================ 06800: ================================================================ 06c00: ================================================================ 07000: ================================================================ 07400: ================================================================ 07800: ================================================================ 07c00: ================================================================ 08000: ================================================================ 08400: ===============================================.....h==......... (349 lines all free) (the big blob of used memory is the large script). Same but with this PR: $ ./pyboard.py large.py stack: 524 out of 23552 GC: total: 392192, used: 1296, free: 390896 No. of 1-blocks: 12, 2-blocks: 3, max blk sz: 40, max free sz: 24420 GC memory layout; from 2001a3f0: 00000: h=hhhh=======================================hhBShShh==h=======h 00400: =====hh=h=B......h==.....h==.................................... (381 lines all free) The only thing in RAM is the compiled script (and some other unrelated items). Time to download before this PR: 1438ms, data rate: 230,799 bits/sec. Time to download with this PR: 119ms, data rate: 2,788,991 bits/sec. So it's more than 10 times faster, and uses significantly less RAM. Results are similar on other boards. On an stm32 board that connects via UART only at 115200 baud, the data rate goes from 80kbit/sec to 113kbit/sec, so gets close to saturating the UART link without loss of data. The new raw REPL mode also supports a single ctrl-C to break out of this flow-control mode, so that a ctrl-C can always get back to a known state. It's also backwards compatible with the original raw REPL mode, which is still supported with the same sequence of commands. The new raw REPL mode is activated by ctrl-E, which gives an error on devices that do not support the new mode. Signed-off-by: Damien George <damien@micropython.org>
2020-11-11lib/utils/pyexec: Add MICROPY_BOARD hooks before/after executing code.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2020-06-02lib/utils/pyexec: Add missing MP_ERROR_TEXT when compiler disabled.Damien George
2020-04-23all: Format code to add space after C++-style comment start.stijn
Note: the uncrustify configuration is explicitly set to 'add' instead of 'force' in order not to alter the comments which use extra spaces after // as a means of indenting text for clarity.
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-07lib/utils/pyexec: Handle pending exceptions after disabling kbd intrs.Damien George
Pending exceptions would otherwise be handled later on where there may not be an NLR handler in place. A similar fix is also made to the unix port's REPL handler. Fixes issues #4921 and #5488.
2019-12-28lib/utils/pyexec: Introduce MICROPY_REPL_INFO, wrap debug prints in it.Yonatan Goldschmidt
For the 3 ports that already make use of this feature (stm32, nrf and teensy) this doesn't make any difference, it just allows to disable it from now on. For other ports that use pyexec, this decreases code size because the debug printing code is dead (it can't be enabled) but the compiler can't deduce that, so code is still emitted.
2019-08-28py: Add global default_emit_opt variable to make emit kind persistent.Damien George
mp_compile no longer takes an emit_opt argument, rather this setting is now provided by the global default_emit_opt variable. Now, when -X emit=native is passed as a command-line option, the emitter will be set for all compiled modules (included imports), not just the top-level script. In the future there could be a way to also set this variable from a script. Fixes issue #4267.
2019-05-01lib/utils: Make pyexec_file_if_exists run frozen scripts if they exist.Andrew Leech
So that boot.py and/or main.py can be frozen (either as STR or MPY) in the same way that other scripts are frozen. Frozen scripts have preference to scripts in the VFS.
2019-04-26lib/utils/pyexec: Add pyexec_file_if_exists() helper function.Damien George
It will only execute the script if it can be stat'd and is a file.
2019-02-12all: Change PYB message prefix to MPY.Mike Causer
Replaces "PYB: soft reboot" with "MPY: soft reboot", etc. Having a consistent prefix across ports reduces the difference between ports, which is a general goal. And this change won't break pyboard.py because that tool only looks for "soft reboot".
2019-01-28lib/utils/pyexec: Implement paste mode with event driven REPL.Damien George
2018-10-13lib/utils/pyexec: Forcefully unlock the heap if locked and REPL active.Damien George
Otherwise there is really nothing that can be done, it can't be unlocked by the user because there is no way to allocate memory to execute the unlock. See issue #4205 and #4209.
2018-07-08lib/utils: Fix to support compiling with object representation D.Damien George
2018-02-13lib/utils/pyexec: Update to work with new MICROPY_HW_ENABLE_USB option.Damien George
2017-10-04all: Remove inclusion of internal py header files.Damien George
Header files that are considered internal to the py core and should not normally be included directly are: py/nlr.h - internal nlr configuration and declarations py/bc0.h - contains bytecode macro definitions py/runtime0.h - contains basic runtime enums Instead, the top-level header files to include are one of: py/obj.h - includes runtime0.h and defines everything to use the mp_obj_t type py/runtime.h - includes mpstate.h and hence nlr.h, obj.h, runtime0.h, and defines everything to use the general runtime support functions Additional, specific headers (eg py/objlist.h) can be included if needed.
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2017-05-05lib/utils/pyexec: Update event-driven REPL to match non-event REPL.Tom Collins
Don't print dupe ">>> " prompt when starting event-driven REPL. Clear incomplete line in transition from raw to friendly REPL.
2017-03-14lib/utils/pyexec: Fix bug with pyexec_file not setting flag for source.Damien George
2017-03-14lib/utils/pyexec: Refactor to put lexer constructors all in one place.Damien George
The lexer can now raise an exception on construction so it must go within an nlr handler block.
2017-02-27lib/utils/pyexec: Allow to compile when the uPy compiler is disabled.Damien George
2017-01-22lib/utils/pyexec: Only print help prompt if HELP feature is enabled.Damien George
2016-10-26zephyr: Fix mp_hal_set_interrupt_char() declaration to be compatible.Paul Sokolovsky
With other ports. Other ports declare it in mphalport.h, it can be inline or macro.
2016-10-26lib/utils/pyexec: Fix compilation warning of type vs format mismatch.Paul Sokolovsky
This happens with some compilers on some architectures, which don't define size_t as unsigned int. MicroPython's printf() dooesn't support obscure format specifiers for size_t, so the obvious choice is to explicitly cast to unsigned, to match %u used in printf().
2016-10-26lib/utils/pyexec: Add mp_hal_set_interrupt_char() prototype.Vincenzo Frascino
This patch removes a compilation warning in pyexec. Signed-off-by: Vincenzo Frascino <vincenzo.frascino@linaro.org>
2016-10-17lib/utils/pyexec: Allow behaviour of SystemExit to be configurable.Damien George
Setting the pyexec_system_exit variable to PYEXEC_FORCED_EXT allows SystemExit exceptions to terminate the pyexec functions.
2016-10-14py/vstr: Combine vstr_new_size with vstr_new since they are rarely used.Damien George
Now there is just one function to allocate a new vstr, namely vstr_new (in addition to vstr_init etc). The caller of this function should know what initial size to allocate for the buffer, or at least have some policy or config option, instead of leaving it to a default (as it was before).
2016-10-12lib/utils/pyexec: Don't treat SystemExit as "forced exit".Paul Sokolovsky
"Forced exit" is treated as soft-reboot (Ctrl+D). But expected effect of calling sys.exit() is termination of the current script, not any further and more serious actions like mentioned soft reboot.
2016-08-30lib/utils/pyexec: qstr_pool_info() requires size_t* parameters.Paul Sokolovsky
2016-07-21lib/utils/pyexec: Use full path to readline.h header, per latest conventions.Paul Sokolovsky
2016-04-19lib/utils/pyexec: Condition-out GC calls from pyexec.Colin Hogben
A port which uses lib/utils/pyexec.c but which does not enable garbage collection should not need to implement the gc_collect function. This patch also moves the gc_collect call to after printing the qstr info. Since qstrs cannot be collected it should not make any difference to the printed statistics.
2016-04-13py: Add ability to have frozen persistent bytecode from .mpy files.Damien George
The config variable MICROPY_MODULE_FROZEN is now made of two separate parts: MICROPY_MODULE_FROZEN_STR and MICROPY_MODULE_FROZEN_MPY. This allows to have none, either or both of frozen strings and frozen mpy files (aka frozen bytecode).
2016-04-01esp8266: Disallow recursive calls to REPL.Paul Sokolovsky
Before this change, if REPL blocked executing some code, it was possible to still input new statememts and excuting them, all leading to weird, and portentially dangerous interaction. TODO: Current implementation may have issues processing input accumulated while REPL was blocked.
2016-03-30esp8266: Fix issue when current repl line was garbage-collected.Paul Sokolovsky
Reference it from root pointers section.
2015-12-26lib/utils: Add pyexec_frozen_module to load and execute frozen module.Damien George
This is a convenience function similar to pyexec_file. It should be used instead of raw mp_parse_compile_execute because the latter does not catch and report exceptions.
2015-11-09lib/pyexec: Move header pyexec.h from stmhal directory.Damien George
2015-11-03lib/pyexec: For paste mode use "Ctrl" as the name of the key, not "CTRL".Damien George
2015-10-31stmhal: pyexec.c is common module, move to lib/utils/ .Paul Sokolovsky