summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2020-04-13py/scheduler: Add assert that scheduler is locked when unlocking.Jim Mussared
And add a test that shows how this can happen when multiple threads are accessing the scheduler, which fails if atomic sections are not used.
2020-04-13py/scheduler: Fix race in checking scheduler pending state.Jim Mussared
Because the atomic section starts after checking whether the scheduler state is pending, it's possible it can become a different state by the time the atomic section starts. This is especially likely on ports where MICROPY_BEGIN_ATOMIC_SECTION is implemented with a mutex (i.e. it might block), but the race exists regardless, i.e. if a context switch occurs between those two lines.
2020-04-09py/objexcept: Remove optional TimeoutError exception.Damien George
TimeoutError was added back in 077812b2abe3f5e5325194f9694dad7eb38186dd for the cc3200 port. In f522849a4d5a978ac3d322d71a755f75d07e8ce6 the cc3200 port enabled use of it in the socket module aliased to socket.timeout. So it was never added to the builtins. Then it was replaced by OSError(ETIMEDOUT) in 047af9b10bfc6b0ec412f8450c6bec10ab95254b. The esp32 port enables this exception, since the very beginning of that port, but it could never be accessed because it's not in builtins. It's being removed: 1) to not encourage its use; 2) because there are a lot of other OSError subclasses which are not defined at all, and having TimeoutError is a bit inconsistent. Note that ports can add anything to the builtins via MICROPY_PORT_BUILTINS. And they can also define their own exceptions using the MP_DEFINE_EXCEPTION() macro.
2020-04-09py/parse: Remove unnecessary check in const folding for ** operator.Damien George
In this part of the code there is no way to get the ** operator, so no need to check for it. This commit also adds tests for this, and other related, invalid const operations.
2020-04-05all: Use MP_ERROR_TEXT for all error messages.Jim Mussared
2020-04-05py/objexcept: Allow compression of exception message text.Jim Mussared
The decompression of error-strings is only done if the string is accessed via printing or via er.args. Tests are added for this feature to ensure the decompression works.
2020-04-05py: Implement "common word" compression scheme for error messages.Jim Mussared
The idea here is that there's a moderate amount of ROM used up by exception text. Obviously we try to keep the messages short, and the code can enable terse errors, but it still adds up. Listed below is the total string data size for various ports: bare-arm 2860 minimal 2876 stm32 8926 (PYBV11) cc3200 3751 esp32 5721 This commit implements compression of these strings. It takes advantage of the fact that these strings are all 7-bit ascii and extracts the top 128 frequently used words from the messages and stores them packed (dropping their null-terminator), then uses (0x80 | index) inside strings to refer to these common words. Spaces are automatically added around words, saving more bytes. This happens transparently in the build process, mirroring the steps that are used to generate the QSTR data. The MP_COMPRESSED_ROM_TEXT macro wraps any literal string that should compressed, and it's automatically decompressed in mp_decompress_rom_string. There are many schemes that could be used for the compression, and some are included in py/makecompresseddata.py for reference (space, Huffman, ngram, common word). Results showed that the common-word compression gets better results. This is before counting the increased cost of the Huffman decoder. This might be slightly counter-intuitive, but this data is extremely repetitive at a word-level, and the byte-level entropy coder can't quite exploit that as efficiently. Ideally one would combine both approaches, but for now the common-word approach is the one that is used. For additional comparison, the size of the raw data compressed with gzip and zlib is calculated, as a sort of proxy for a lower entropy bound. With this scheme we come within 15% on stm32, and 30% on bare-arm (i.e. we use x% more bytes than the data compressed with gzip -- not counting the code overhead of a decoder, and how this would be hypothetically implemented). The feature is disabled by default and can be enabled by setting MICROPY_ROM_TEXT_COMPRESSION at the Makefile-level.
2020-04-05py/dynruntime.mk: Set MICROPY_ENABLE_DYNRUNTIME instead of per module.Jim Mussared
So this setting could be used by other source files if needed.
2020-04-05py: Use preprocessor to detect error reporting level (terse/detailed).Jim Mussared
Instead of compiler-level if-logic. This is necessary to know what error strings are included in the build at the preprocessor stage, so that string compression can be implemented.
2020-03-30all: Fix implicit casts of float/double, and signed comparison.David Lechner
These were found by buiding the unix coverage variant on macOS (so clang compiler). Mostly, these are fixing implicit cast of float/double to mp_float_t which is one of those two and one mp_int_t to size_t fix for good measure.
2020-03-28all: Remove spaces inside and around parenthesis.Damien George
Using new options enabled in the uncrustify configuration.
2020-03-26extmod/uasyncio: Add optional implementation of core uasyncio in C.Damien George
Implements Task and TaskQueue classes in C, using a pairing-heap data structure. Using this reduces RAM use of each Task, and improves overall performance of the uasyncio scheduler.
2020-03-26py/pairheap: Add helper function to initialise a new node.Damien George
2020-03-26py/pairheap: Properly unlink node on pop and delete.Damien George
This fixes a bug in the pairing-heap implementation when nodes are deleted with mp_pairheap_delete and then reinserted later on.
2020-03-26py/mpconfig.h: Enable MICROPY_MODULE_GETATTR by default.Damien George
To enable lazy loading of submodules (among other things), which is very useful for MicroPython libraries that want to have optional subcomponents. Disabled explicitly on minimal ports.
2020-03-25py/stream.h: Include sys/types.h to get size_t and off_t for POSIX API.David Lechner
2020-03-25all: Remove spaces between nested paren and inside function arg paren.Damien George
Using new options enabled in the uncrustify configuration.
2020-03-18all: Convert exceptions to use mp_raise_XXX helpers in remaining places.Damien George
2020-03-11py/modmicropython: Add heap_locked function to test state of heap.Andrew Leech
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.
2020-03-11py/objstringio: Expose tell() on StringIO and BytesIO objects.Andrew Leech
To match file objects. Fixes issue #5581.
2020-03-11tools/codeformat.py: Eliminate need for sizeof fixup.David Lechner
This eliminates the need for the sizeof regex fixup by rearranging things a bit. All other bitfields already use the parentheses around expressions with sizeof, so one case is fixed by following this convention. VM_MAX_STATE_ON_STACK is the only remaining problem and it can be worked around by changing the order of the operands.
2020-03-11py/objstr: Remove duplicate % in error string.Tom Collins
The double-% was added in 11de8399fe5f9ef54589b14470faf8d4fcc5ccaa (Jun 2014) when such errors were formatted with printf. But then 55830dd9bf4fee87c0a6d3f38c51614fea0eb483 (Dec 2018) changed mp_obj_new_exception_msg() to not format the message, as discussed in #3004. So such error strings are no longer formatted and a % is just that.
2020-02-28py/builtinevex: Support passing in a bytearray/buffer to eval/exec.Damien George
CPython allows this and it's a simple generalisation of the existing code which just supported str/bytes. Fixes issue #5704.
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-28all: Add *FORMAT-OFF* in various places.Damien George
This string is recognised by uncrustify, to disable formatting in the region marked by these comments. This is necessary in the qstrdef*.h files to prevent modification of the strings within the Q(...). In other places it is used to prevent excessive reformatting that would make the code less readable.
2020-02-28py/parse: Add parenthesis around calculated bit-width in struct.Damien George
To improve interaction with uncrustify formatter.
2020-02-28py: Un-nest configuration #if/#endif's for selection of complex code.Damien George
Because un-nested #if's are simpler to handle with formatting tools.
2020-02-28py/malloc: Put { on separate line for funcs that have selective sigs.Damien George
To ensure there are balanced {}'s in the file, and to help with formatting.
2020-02-28py/builtinimport: Adjust if-block order in find_file to clean up #if's.Damien George
2020-02-28py/bc0.h: Shift comment to start of line to improve format consistency.Damien George
2020-02-28py: Removing dangling "else" to improve code format consistency.Damien George
2020-02-21py/objarray: Turn on MP_TYPE_FLAG_EQ_CHECKS_OTHER_TYPE for memoryview.Jim Mussared
And add corresponding tests. Fixes #5674 (comparison of memoryview against bytes).
2020-02-21py/dynruntime.h: Add implementation of mp_obj_cast_to_native_base.Damien George
2020-02-21py/objtype: Allow mp_instance_cast_to_native_base to take native obj.Damien George
And rename it to mp_obj_cast_to_native_base() to indicate this. This allows users of this function to easily support native and native-subclass objects in the same way (by just passing the object through this function).
2020-02-20py/objtuple: Remove code that handles tuple-subclass equality test.Damien George
Since commit 3aab54bf434e7f025a91ea05052f1bac439fad8c this piece of code is no longer needed because the top-level function mp_obj_equal_not_equal() now handles the case of user types, and will never call tuple's binary_op function with MP_BINARY_OP_EQUAL and a non-tuple on the RHS.
2020-02-18py/objexcept: Rename mp_obj_new_exception_msg_varg2 to ..._vlist.Damien George
Follow up to recent commit ad7213d3c31bccb26a3f54f7492ccf4b0cc920f3, the name "varg2" is misleading, vlist describes better that the argument is a va_list. This name also matches CircuitPython, which already has such helper functions.
2020-02-18py: Factor out definition of mp_float_union_t to one location.Damien George
2020-02-13py/obj.h: Remove TODO idea comment about truncated mp_map_t.David Lechner
It was suggested to move this to a GitHub issue rather than keep it in the code, which isn't really sustainable for all ideas.
2020-02-13py: Add mp_raise_msg_varg helper and use it where appropriate.Damien George
This commit adds mp_raise_msg_varg(type, fmt, ...) as a helper for nlr_raise(mp_obj_new_exception_msg_varg(type, fmt, ...)). It makes the C-level API for raising exceptions more consistent, and reduces code size on most ports: bare-arm: +28 +0.042% minimal x86: +100 +0.067% unix x64: -56 -0.011% unix nanbox: -300 -0.068% stm32: -204 -0.054% PYBV10 cc3200: +0 +0.000% esp8266: -64 -0.010% GENERIC esp32: -104 -0.007% GENERIC nrf: -136 -0.094% pca10040 samd: +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
2020-02-13py: Add mp_raise_type helper macro and use it where appropriate.Damien George
This provides a more consistent C-level API to raise exceptions, ie moving away from nlr_raise towards mp_raise_XXX. It also reduces code size by a small amount on some ports.
2020-02-11py/objmodule.h: Remove obsolete mp_builtin_module_weak_links_map decl.Damien George
It was made obsolete in d2384efa809953152c57cbda4c339dfbaa64cf29
2020-02-11py: Expand type equality flags to 3 separate ones, fix bool/namedtuple.Damien George
Both bool and namedtuple will check against other types for equality; int, float and complex for bool, and tuple for namedtuple. So to make them work after the recent commit 3aab54bf434e7f025a91ea05052f1bac439fad8c they would need MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST set. But that makes all bool and namedtuple equality checks less efficient because mp_obj_equal_not_equal() could no longer short-cut x==x, and would need to try __ne__. To improve this, this commit splits the MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST flags into 3 separate flags to give types more fine-grained control over how their equality behaves. These new flags are then used to fix bool and namedtuple equality. Fixes issue #5615 and #5620.
2020-02-07py/scheduler: Move clearing of kbd traceback to mp_keyboard_interrupt.Damien George
This is a more logical place to clear the KeyboardInterrupt traceback, right before it is set as a pending exception. The clearing is also optimised from a function call to a simple store of NULL.
2020-02-07py/scheduler: Allow a port to specify attrs for mp_keyboard_interrupt.Damien George
Functions like mp_keyboard_interrupt() may need to be called from an IRQ handler and may need to be in a special memory section, so provide a generic wrapping macro for a port to do this. The macro name is chosen to be MICROPY_WRAP_<function name in uppercase> so that (in the future with more wrappers) each function could potentially be handled separately.
2020-02-07py/scheduler: Move mp_keyboard_interrupt from lib/utils to py core.Damien George
This function is tightly coupled to the state and behaviour of the scheduler, and is a core part of the runtime: to schedule a pending exception. So move it there.
2020-02-07py/scheduler: Add "raise_exc" argument to mp_handle_pending.Damien George
Previous behaviour is when this argument is set to "true", in which case the function will raise any pending exception. Setting it to "false" will cancel any pending exception.
2020-02-06py/compile: Allow 'return' outside function in minimal builds.Petr Viktorin
A 'return' statement on module/class level is not correct Python, but nothing terribly bad happens when it's allowed. So remove the check unless MICROPY_CPYTHON_COMPAT is on. This is similar to MicroPython's treatment of 'import *' in functions (except 'return' has unsurprising behavior if it's allowed).
2020-01-31py/modthread: Fix spelling error in comment.David Lechner
2020-01-30py: Support non-boolean results for equality and inequality tests.Nicko van Someren
This commit implements a more complete replication of CPython's behaviour for equality and inequality testing of objects. This addresses the issues discussed in #5382 and a few other inconsistencies. Improvements over the old code include: - Support for returning non-boolean results from comparisons (as used by numpy and others). - Support for non-reflexive equality tests. - Preferential use of __ne__ methods and MP_BINARY_OP_NOT_EQUAL binary operators for inequality tests, when available. - Fallback to op2 == op1 or op2 != op1 when op1 does not implement the (in)equality operators. The scheme here makes use of a new flag, MP_TYPE_FLAG_NEEDS_FULL_EQ_TEST, in the flags word of mp_obj_type_t to indicate if various shortcuts can or cannot be used when performing equality and inequality tests. Currently four built-in classes have the flag set: float and complex are non-reflexive (since nan != nan) while bytearray and frozenszet instances can equal other builtin class instances (bytes and set respectively). The flag is also set for any new class defined by the user. This commit also includes a more comprehensive set of tests for the behaviour of (in)equality operators implemented in special methods.
2020-01-30py/objtype: Make mp_obj_type_t.flags constants public, moved to obj.h.Damien George