summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2019-03-05py/persistentcode: Define static qstr set to reduce size of mpy files.Damien George
When encoded in the mpy file, if qstr <= QSTR_LAST_STATIC then store two bytes: 0, static_qstr_id. Otherwise encode the qstr as usual (either with string data or a reference into the qstr window). Reduces mpy file size by about 5%.
2019-03-05py/persistentcode: Pack qstrs directly in bytecode to reduce mpy size.Damien George
Instead of emitting two bytes in the bytecode for where the linked qstr should be written to, it is now replaced by the actual qstr data, or a reference into the qstr window. Reduces mpy file size by about 10%.
2019-03-05py/persistentcode: Add a qstr window to save mpy files more efficiently.Damien George
This is an implementation of a sliding qstr window used to reduce the number of qstrs stored in a .mpy file. The window size is configured to 32 entries which takes a fixed 64 bytes (16-bits each) on the C stack when loading/saving a .mpy file. It allows to remember the most recent 32 qstrs so they don't need to be stored again in the .mpy file. The qstr window uses a simple least-recently-used mechanism to discard the least recently used qstr when the window overflows (similar to dictionary compression). This scheme only needs a single pass to save/load the .mpy file. Reduces mpy file size by about 25% with a window size of 32.
2019-03-05py: Replace POP_BLOCK and POP_EXCEPT opcodes with POP_EXCEPT_JUMP.Damien George
POP_BLOCK and POP_EXCEPT are now the same, and are always followed by a JUMP. So this optimisation reduces code size, and RAM usage of bytecode by two bytes for each try-except handler.
2019-03-05py/vm: Remove currently_in_except_block variable.Damien George
After the previous commit it is no longer needed.
2019-03-05py: Fix VM crash with unwinding jump out of a finally block.Damien George
This patch fixes a bug in the VM when breaking within a try-finally. The bug has to do with executing a break within the finally block of a try-finally statement. For example: def f(): for x in (1,): print('a', x) try: raise Exception finally: print(1) break print('b', x) f() Currently in uPy the above code will print: a 1 1 1 segmentation fault (core dumped) micropython Not only is there a seg fault, but the "1" in the finally block is printed twice. This is because when the VM executes a finally block it doesn't really know if that block was executed due to a fall-through of the try (no exception raised), or because an exception is active. In particular, for nested finallys the VM has no idea which of the nested ones have active exceptions and which are just fall-throughs. So when a break (or continue) is executed it tries to unwind all of the finallys, when in fact only some may be active. It's questionable whether break (or return or continue) should be allowed within a finally block, because they implicitly swallow any active exception, but nevertheless it's allowed by CPython (although almost never used in the standard library). And uPy should at least not crash in such a case. The solution here relies on the fact that exception and finally handlers always appear in the bytecode after the try body. Note: there was a similar bug with a return in a finally block, but that was previously fixed in b735208403a54774f9fd3d966f7c1a194c41870f
2019-03-04py/py.mk: Update lwip build config to work with latest lwip version.Damien George
Also, to make it possible for ports to provide their own lwipopts.h, the default include directory of extmod/lwip-include is no longer added and instead a port should now make sure the correct include directory is included in the list (can still use extmod/lwip-include).
2019-03-04py/objexcept: Fix hash of exc str created in mp_obj_new_exception_msg.Tom Collins
2019-03-01py/compile: Add optimisation to compile OrderedDict inplace.Damien George
This optimisation eliminates the need to create a temporary normal dict. The optimisation is enabled via MICROPY_COMP_CONST_LITERAL which is enabled by default (although only has an effect if OrderdDict is enabled). Thanks to @pfalcon for the initial idea and implementation.
2019-02-26py/compile: Fix handling of unwinding BaseException in async with.Damien George
All exceptions that unwind through the async-with must be caught and BaseException is the top-level class, which includes Exception and others. Fixes issue #4552.
2019-02-25py: Eliminate warnings about unused arguments when debugging disabled.Damien George
2019-02-20py/objfun: Make fun_data arg of mp_obj_new_fun_asm() a const pointer.Damien George
2019-02-20py/obj.h: Remove obsolete mp_obj_new_fun_viper() declaration.Damien George
2019-02-19py/qstr: Evaluate find_qstr only once then pass to Q_GET_HASH macro.Damien George
Q_GET_HASH may evaluate its argument more than once.
2019-02-14extmod/moduwebsocket: Refactor `websocket` to `uwebsocket`.Yonatan Goldschmidt
As mentioned in #4450, `websocket` was experimental with a single intended user, `webrepl`. Therefore, we'll make this change without a weak link `websocket` -> `uwebsocket`.
2019-02-12py/mkenv.mk: Change default PYTHON variable from "python" to "python3".Damien George
This change makes it so that python3 is required by default to build MicroPython. Python 2 can be used by specifying make PYTHON=python2. This comes about due to a recent-ish change to PEP 394 that makes the python command more optional than before (even with Python 2 installed); see https://github.com/python/peps/commit/cd59ec03c8ff1e75089d5872520cd0706774b35b#diff-1d22f7bd72cbc900670f058b1107d426 Since the command python is no longer required to be provided by a distribution we need to use either python2 or python3 as commands. And python3 seems the obvious choice.
2019-02-12py: Downcase MP_xxx_SLOT_IS_FILLED inline functions.Damien George
2019-02-12py: Downcase all MP_OBJ_IS_xxx macros to make a more consistent C API.Damien George
These macros could in principle be (inline) functions so it makes sense to have them lower case, to match the other C API functions. The remaining macros that are upper case are: - MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR - MP_OBJ_NEW_SMALL_INT, MP_OBJ_SMALL_INT_VALUE - MP_OBJ_NEW_QSTR, MP_OBJ_QSTR_VALUE - MP_OBJ_FUN_MAKE_SIG - MP_DECLARE_CONST_xxx - MP_DEFINE_CONST_xxx These must remain macros because they are used when defining const data (at least, MP_OBJ_NEW_SMALL_INT is so it makes sense to have MP_OBJ_SMALL_INT_VALUE also a macro). For those macros that have been made lower case, compatibility macros are provided for the old names so that users do not need to change their code immediately.
2019-02-06py/mpconfig.h: Fix comments mentioning dangling file and variable names.Yonatan Goldschmidt
2019-02-06py/builtinhelp: Only print help re FS modules if external import enabledYonatan Goldschmidt
2019-02-06py: Update my copyright info on some files.Paul Sokolovsky
Based on git history.
2019-01-31py/warning: Support categories for warnings.Paul Sokolovsky
Python defines warnings as belonging to categories, where category is a warning type (descending from exception type). This is useful, as e.g. allows to disable warnings selectively and provide user-defined warning types. So, implement this in MicroPython, except that categories are represented just with strings. However, enough hooks are left to implement categories differently per-port (e.g. as types), without need to patch each and every usage.
2019-01-27py/compile: Swap order of pop_block/pop_except in "except as" handler.Damien George
To make the try-finally block self contained.
2019-01-27py: Add optional support for 2-argument version of built-in next().stijn
Configurable via MICROPY_PY_BUILTINS_NEXT2, disabled by default.
2019-01-27py: Remove calls to file reader functions when these are disabled.Sean Burton
If MICROPY_PERSISTENT_CODE_LOAD or MICROPY_ENABLE_COMPILER are enabled then code gets enabled that calls file reading functions which may be disabled if no readers have been implemented. To fix this, introduce a MICROPY_HAS_FILE_READER variable, which is automatically set if MICROPY_READER_POSIX or MICROPY_READER_VFS is set but can also be manually set if a custom reader is being implemented. Then disable the file reading calls if this is not set.
2019-01-26all: Bump version to 1.10.v1.10Damien George
2019-01-26py/mpconfig.h: Remove parentheses from MICROPY_VERSION_xxx macros.Damien George
Otherwise MICROPY_VERSION_STRING includes these parentheses in the string.
2019-01-25py/obj.h: Explicitly cast args to uint32_t in MP_OBJ_FUN_MAKE_SIG.Damien George
For architectures where size_t is less than 32 bits (eg 16 bits) the args must be casted to uint32_t so the left shift will work. For architectures where size_t is greater than 32 bits (eg 64 bits) this new casting will not lose any bits because the end result must anyway fit in a uint32_t.
2019-01-10py/modio: Make iobase_singleton object const so it goes in ROM.Damien George
2019-01-04py: Fix location of VM returned exception in invalid opcode and commentsDamien George
The location for a returned exception was changed to state[0] in d95947b48a30f818638c3619b92110ce6d07f5e3
2019-01-04py: Get optional VM stack overflow check compiling and working again.Damien George
Changes to the layout of the bytecode header meant that this debug code was no longer compiling. This is now fixed and a new compile-time option is introduced, MICROPY_DEBUG_VM_STACK_OVERFLOW, to turn on this feature (which is disabled by default). This option is needed because more than one file needs to cooperate to make this check work.
2018-12-27py/runtime: Unlock the GIL in mp_deinit function.Damien George
This mirrors what is done in mp_init. Some RTOSs require this symmetry to get back to a clean state (when doing a soft reset, for example).
2018-12-22py/mpconfig: Move MICROPY_VERSION macros to static ones in mpconfig.h.Damien George
It's more robust to have the version defined statically in a header file, rather than dynamically generating it via git using a git tag. In case git doesn't exist, or a different source control tool is used, it's important to still have the uPy version number available.
2018-12-20py/gc: Adjust gc_alloc() signature to be able to accept multiple flags.Paul Sokolovsky
The older "bool has_finaliser" gets recast as GC_ALLOC_FLAG_HAS_FINALISER=1 so this is a backwards compatible change to the signature. Since bool gets implicitly converted to 1 this patch doesn't include conversion of all calls.
2018-12-20py/objarray: Introduce "memview_offset" alias for "free" field of objectPaul Sokolovsky
Both mp_type_array and mp_type_memoryview use the same object structure, mp_obj_array_t, but for the case of memoryview, some fields, e.g. "free", have different meaning. As the "free" field is also a bitfield, assume that (anonymous) union can't be used here (for the concerns of possible compatibility issues with wide array of toolchains), and just add a field alias using a #define. As it's a define, it should be a selective identifier, so use verbose "memview_offset" to avoid any clashes.
2018-12-15py/qstr: Put a lower bound on new qstr pool allocation.Damien George
2018-12-13py/bc: Fix calculation of opcode size for opcodes with map caching.Damien George
All 4 opcodes that can have caching bytes also have qstrs, so the test for them must go in the qstr part of the code. The reason this incorrect calculation of the opcode size did not lead to a bug is because the caching byte is at the end of the opcode (byte, qstr, qstr, cache) and is always 0x00 when saving/loading, so was just treated as a single byte no-op opcode. Hence these opcodes were being saved/loaded/decoded correctly. Thanks to @malinah for finding the problem and providing the initial patch.
2018-12-13py/objdict: Make .fromkeys() method configurable.Paul Sokolovsky
On by default, turned off for minimal/bare-arm. Saves 144 bytes on x86.
2018-12-10py/objexcept: Make sure mp_obj_new_exception_msg doesn't copy/format msgDamien George
mp_obj_new_exception_msg() assumes that the message passed to it is in ROM and so can use its data directly to create the string object for the argument of the exception, saving RAM. At the same time, this approach also makes sure that there is no attempt to format the message with printf, which could lead to faults if the message contained % characters. Fixes issue #3004.
2018-12-10py/objexcept: Use macros to make offsets in emergency exc buf clearer.Damien George
2018-12-10extmod/moductypes: Add aliases for native C types.Paul Sokolovsky
SHORT, INT, LONG, LONGLONG, and unsigned (U*) variants are being defined. This is done at compile using GCC-style predefined macros like __SIZEOF_INT__. If the compiler doesn't have such defines, no such types will be defined.
2018-12-07py/obj: Add support for __int__ special method.Paul Sokolovsky
Based on the discussion, this special method is available unconditionally, as converting to int is a common operation.
2018-12-06py/objboundmeth: Support loading generic attrs from the method.Damien George
Instead of assuming that the method is a bytecode object, and only supporting load of __name__, make the operation generic by delegating the load to the method object itself. Saves a bit of code size and fixes the case of attempting to load __name__ on a native method, see issue #4028.
2018-12-04py: Add option to reduce GC stack integer size to save RAM.Ayke van Laethem
A new option MICROPY_GC_STACK_ENTRY_TYPE is added to select a custom type instead of size_t for the gc_stack array items. This can be beneficial for small devices, especially those that are low on memory anyway. If a device has 1MB or less of heap (and 16-byte GC blocks) then this type can be uint16_t, saving 128 bytes of RAM.
2018-12-04py/py.mk: Fix broken Gmane URL.Craig Younkins
2018-11-26py/unicode: Fix check for valid utf8 being stricter about contn chars.Damien George
2018-11-01py/runtime: Fix qstr assumptions when handling "import *".Paul Sokolovsky
There was an assumption that all names in a module dict are qstr's. However, they can be dynamically generated (by assigning to globals()), and in case of a long name, it won't be a qstr. Handle this situation properly, including taking care of not creating superfluous qstr's for names starting with "_" (which aren't imported by "import *").
2018-10-28py/scope: Optimise scope_find_or_add_id to not need "added" arg.Damien George
Taking the address of a local variable is mildly expensive, in code size and stack usage. So optimise scope_find_or_add_id() to not need to take a pointer to the "added" variable, and instead take the kind to use for newly added identifiers.
2018-10-28py/compile: Remove unneeded variable from global/nonlocal stmt helpers.Damien George
2018-10-28py/compile: Fix case of eager implicit conversion of local to nonlocal.Damien George
This ensures that implicit variables are only converted to implicit closed-over variables (nonlocals) at the very end of the function scope. If variables are closed-over when first used (read from, as was done prior to this commit) then this can be incorrect because the variable may be assigned to later on in the function which means they are just a plain local, not closed over. Fixes issue #4272.