summaryrefslogtreecommitdiffstatshomepage
path: root/py/modmicropython.c
Commit message (Collapse)AuthorAge
* py/gc: Allow gc_free from inside a gc_sweep finalizer.Angus Gratton2025-02-03
| | | | | | | | | | | | | | | | | | Do this by tracking being inside gc collection with a separate flag, GC_COLLECT_FLAG. In gc_free(), ignore this flag when determining if the heap is locked. * For finalisers calling gc_free() when heap is otherwise unlocked, this allows memory to be immediately freed (potentially avoiding a MemoryError). * Hard IRQs still can't call gc_free(), as heap will be locked via gc_lock(). * If finalisers are disabled then all of this code can be compiled out to save some code size. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/objringio: Add micropython.RingIO() interface for general use.Andrew Leech2024-09-19
| | | | | | | | | | | | This commit adds a new `RingIO` type which exposes the internal ring-buffer code for general use in Python programs. It has the stream interface making it similar to `StringIO` and `BytesIO`, except `RingIO` has a fixed buffer size and is automatically safe when reads and writes are in different threads or an IRQ. This new type is enabled at the "extra features" ROM level. Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
* py: Add new cstack API for stack checking, with limit margin macro.Angus Gratton2024-08-14
| | | | | | | | | | | | | | | | | | | | Currently the stack limit margin is hard-coded in each port's call to `mp_stack_set_limit()`, but on threaded ports it's fiddlier and can lead to bugs (such as incorrect thread stack margin on esp32). This commit provides a new API to initialise the C Stack in one function call, with a config macro to set the margin. Where possible the new call is inlined to reduce code size in thread-free ports. Intended replacement for `MP_TASK_STACK_LIMIT_MARGIN` on esp32. The previous `stackctrl.h` API is still present and unmodified apart from a deprecation comment. However it's not available when the `MICROPY_PREVIEW_VERSION_2` macro is set. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* all: Remove the "STATIC" macro and just use "static" instead.Angus Gratton2024-03-07
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The STATIC macro was introduced a very long time ago in commit d5df6cd44a433d6253a61cb0f987835fbc06b2de. The original reason for this was to have the option to define it to nothing so that all static functions become global functions and therefore visible to certain debug tools, so one could do function size comparison and other things. This STATIC feature is rarely (if ever) used. And with the use of LTO and heavy inline optimisation, analysing the size of individual functions when they are not static is not a good representation of the size of code when fully optimised. So the macro does not have much use and it's simpler to just remove it. Then you know exactly what it's doing. For example, newcomers don't have to learn what the STATIC macro is and why it exists. Reading the code is also less "loud" with a lowercase static. One other minor point in favour of removing it, is that it stops bugs with `STATIC inline`, which should always be `static inline`. Methodology for this commit was: 1) git ls-files | egrep '\.[ch]$' | \ xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/" 2) Do some manual cleanup in the diff by searching for the word STATIC in comments and changing those back. 3) "git-grep STATIC docs/", manually fixed those cases. 4) "rg -t python STATIC", manually fixed codegen lines that used STATIC. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
* py/gc: Make gc_dump_info/gc_dump_alloc_table take a printer as argument.Damien George2023-03-10
| | | | | | So that callers can redirect the output if needed. Signed-off-by: Damien George <damien@micropython.org>
* py/modmicropython: Make module optional.Laurens Valk2022-12-06
| | | | | | | 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>
* all: Remove third argument to MP_REGISTER_MODULE.Damien George2022-06-02
| | | | | | | | It's no longer needed because this macro is now processed after preprocessing the source code via cpp (in the qstr extraction stage), which means unused MP_REGISTER_MODULE's are filtered out by the preprocessor. Signed-off-by: Damien George <damien@micropython.org>
* py: Make builtin modules use MP_REGISTER_MODULE.Jim Mussared2022-05-18
| | | | Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
* py/gc: Make gc_lock_depth have a count per thread.Damien George2021-05-10
| | | | | | | | | | | | | | | | | | | | | | | 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>
* all: Use MP_ERROR_TEXT for all error messages.Jim Mussared2020-04-05
|
* py/modmicropython: Add heap_locked function to test state of heap.Andrew Leech2020-03-11
| | | | | | | | | | This commit adds micropython.heap_locked() which returns the current lock-depth of the heap, and can be used by Python code to check if the heap is locked or not. This new function is configured via MICROPY_PY_MICROPYTHON_HEAP_LOCKED and is disabled by default. This commit also changes the return value of micropython.heap_unlock() so it returns the current lock-depth as well.
* all: Reformat C and Python source code with tools/codeformat.py.Damien George2020-02-28
| | | | This is run with uncrustify 0.70.1, and black 19.10b0.
* py/scheduler: Rename sched_stack to sched_queue.Jim Mussared2019-07-17
| | | | | | Behaviour was changed from stack to queue in 8977c7eb581f5d06500edb1ea29aea5cbda04f28, and this updates variable names to match. Also updates other references (docs, error messages).
* py: Don't include mp_optimise_value or opt_level() if compiler disabled.Damien George2018-04-04
| | | | | | Without the compiler enabled the mp_optimise_value is unused, and the micropython.opt_level() function is not useful, so exclude these from the build to save RAM and code size.
* py/modmicropython: Allow to have stack_use() func without mem_info().Damien George2018-02-20
| | | | | | | | | | | The micropython.stack_use() function is useful to query the current C stack usage, and it's inclusion in the micropython module doesn't need to be tied to the inclusion of mem_info()/qstr_info() because it doesn't rely on any of the code from these functions. So this patch introduces the config option MICROPY_PY_MICROPYTHON_STACK_USE which can be used to independently control the inclusion of stack_use(). By default it is enabled if MICROPY_PY_MICROPYTHON_MEM_INFO is enabled (thus not changing any of the existing ports).
* py: Introduce a Python stack for scoped allocation.Damien George2017-12-11
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This patch introduces the MICROPY_ENABLE_PYSTACK option (disabled by default) which enables a "Python stack" that allows to allocate and free memory in a scoped, or Last-In-First-Out (LIFO) way, similar to alloca(). A new memory allocation API is introduced along with this Py-stack. It includes both "local" and "nonlocal" LIFO allocation. Local allocation is intended to be equivalent to using alloca(), whereby the same function must free the memory. Nonlocal allocation is where another function may free the memory, so long as it's still LIFO. Follow-up patches will convert all uses of alloca() and VLA to the new scoped allocation API. The old behaviour (using alloca()) will still be available, but when MICROPY_ENABLE_PYSTACK is enabled then alloca() is no longer required or used. The benefits of enabling this option are (or will be once subsequent patches are made to convert alloca()/VLA): - Toolchains without alloca() can use this feature to obtain correct and efficient scoped memory allocation (compared to using the heap instead of alloca(), which is slower). - Even if alloca() is available, enabling the Py-stack gives slightly more efficient use of stack space when calling nested Python functions, due to the way that compilers implement alloca(). - Enabling the Py-stack with the stackless mode allows for even more efficient stack usage, as well as retaining high performance (because the heap is no longer used to build and destroy stackless code states). - With Py-stack and stackless enabled, Python-calling-Python is no longer recursive in the C mp_execute_bytecode function. The micropython.pystack_use() function is included to measure usage of the Python stack.
* all: Remove inclusion of internal py header files.Damien George2017-10-04
| | | | | | | | | | | | | | | | 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.
* all: Use the name MicroPython consistently in commentsAlexander Steffen2017-07-31
| | | | | There were several different spellings of MicroPython present in comments, when there should be only one.
* py/modmicropython: Cast stack_limit value so it prints correctly.Damien George2017-07-18
| | | | Without this cast the print will give a wrong result on nan-boxing builds.
* py/modmicropython: Add micropython.kbd_intr() function.Damien George2017-04-18
| | | | | | | It controls the character that's used to (asynchronously) raise a KeyboardInterrupt exception. Passing "-1" allows to disable the interception of the interrupt character (as long as a port allows such a behaviour).
* py: Add micropython.schedule() function and associated runtime code.Damien George2017-03-20
|
* py/modmicropython: Add micropython.opt_level([value]) function.Damien George2016-10-11
| | | | This allows to get/set at runtime the optimisation level of the compiler.
* py/modmicropython: Add micropython.const, alias for identity function.Damien George2016-09-27
| | | | | | | | | Having a micropython.const identity function, and writing "from micropython import const" at the start of scripts that use the const feature, allows to write scripts which are compatible with CPython, and with uPy builds that don't include const optimisation. This patch adds such a function and updates the tests to do the import.
* all: Remove 'name' member from mp_obj_module_t struct.Damien George2016-09-22
| | | | One can instead lookup __name__ in the modules dict to get the value.
* py: Add MP_STATE_THREAD to hold state specific to a given thread.Damien George2016-06-28
|
* py/modmicropython: Add stack_use, heap_lock and heap_unlock functions.Damien George2016-01-24
| | | | | | | | | micropython.stack_use() returns an integer being the number of bytes used on the stack. micropython.heap_lock() and heap_unlock() can be used to prevent the memory manager from allocating anything on the heap. Calls to these are allowed to be nested.
* py: Change type signature of builtin funs that take variable or kw args.Damien George2016-01-11
| | | | | With this patch the n_args parameter is changed type from mp_uint_t to size_t.
* py/qstr: Use size_t instead of mp_uint_t when counting allocated bytes.Damien George2015-12-17
|
* py: Add MP_ROM_* macros and mp_rom_* types and use them.Damien George2015-11-29
|
* py: Convert occurrences of non-debug printf to mp_printf.Damien George2015-04-16
|
* py: Use TextIOWrapper only if PY_IO_FILEIO def'd; cast size_t for print.Damien George2015-02-15
|
* py: Add option to micropython.qstr_info() to dump actual qstrs.Damien George2015-02-10
|
* py, unix: Allow to compile with -Wunused-parameter.Damien George2015-01-20
| | | | See issue #699.
* py, unix, lib: Allow to compile with -Wold-style-definition.Damien George2015-01-12
|
* py: Make mem_info print correct remaining stack bytes.Damien George2015-01-09
|
* py: Add MICROPY_PY_MICROPYTHON_MEM_INFO to enable mem-info funcs.Damien George2015-01-09
| | | | | | This allows to enable mem-info functions in micropython module, even if MICROPY_MEM_STATS is not enabled. In this case, you get mem_info and qstr_info but not mem_{total,current,peak}.
* py: Move to guarded includes, everywhere in py/ core.Damien George2015-01-01
| | | | Addresses issue #1022.
* modmicropython: Move mem_info() and qstr_info() functions from unix port.Paul Sokolovsky2014-12-01
| | | | TODO: Merge useful functionality from modpyb too.
* Use MP_DEFINE_CONST_DICT macro to define module dicts.Damien George2014-11-29
| | | | | This is just a clean-up of the code. Generated code is exactly the same.
* py: Make MP_OBJ_NEW_SMALL_INT cast arg to mp_int_t itself.Damien George2014-07-31
| | | | Addresses issue #724.
* Add support for storing args during an exception raised by an irq.Dave Hylands2014-07-25
| | | | | | | | | | | | | | | | | The user code should call micropython.alloc_emergency_exception_buf(size) where size is the size of the buffer used to print the argument passed to the exception. With the test code from #732, and a call to micropython.alloc_emergenncy_exception_buf(100) the following error is now printed: ```python >>> import heartbeat_irq Uncaught exception in Timer(4) interrupt handler Traceback (most recent call last): File "0://heartbeat_irq.py", line 14, in heartbeat_cb NameError: name 'led' is not defined ```
* Rename machine_(u)int_t to mp_(u)int_t.Damien George2014-07-03
| | | | See discussion in issue #50.
* py: Include mpconfig.h before all other includes.Paul Sokolovsky2014-06-21
| | | | | | It defines types used by all other headers. Fixes #691.
* Prefix ARRAY_SIZE with micropython prefix MP_Emmanuel Blot2014-06-19
|
* Add license header to (almost) all files.Damien George2014-05-03
| | | | | | | Blanket wide to all .c and .h files. Some files originating from ST are difficult to deal with (license wise) so it was left out of those. Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.
* Add ARRAY_SIZE macro, and use it where possible.Damien George2014-04-26
|
* py: Change module globals from mp_map_t* to mp_obj_dict_t*.Damien George2014-04-05
| | | | | | Towards addressing issue #424. Had a small increase to ROM usage (order 60 bytes).
* Merge map.h into obj.h.Damien George2014-03-30
| | | | | | Pretty much everyone needs to include map.h, since it's such an integral part of the Micro Python object implementation. Thus, the definitions are now in obj.h instead. map.h is removed.
* Proper support for registering builtin modules in ROM.Damien George2014-03-25
Comes with some refactoring of code and renaming of files. All modules are now named mod*.[ch].