summaryrefslogtreecommitdiff
path: root/py
AgeCommit message (Collapse)Author
2021-12-17all: Remove support for FROZEN_DIR and FROZEN_MPY_DIR.Jim Mussared
These have been deprecated for over two years in favour of FROZEN_MANIFEST and manifest.py. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-12-17py/modio: Remove io.resource_stream function.Jim Mussared
This feature is not enabled on any port, it's not in CPython's io module, and functionality is better suited to the micropython-lib implementation of pkg_resources.
2021-12-15py/showbc: Fix printing of raw bytecode header on nanbox builds.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-12-01py/builtinimport: Refactor module importing.Jim Mussared
Simplify and document/comment the handling of builtin import for: - already-loaded modules - built-in modules - built-in umodules (formerly weak links) - filesystem modules Retains existing functionality with smaller code size but should also facilitate potential new features (built-in packages, controlling the frozen path). Also makes the (unix-only) -m behavior a bit more obvious and configurable. Code size change with this commit: bare-arm: +0 +0.000% minimal x86: -64 -0.039% unix x64: -32 -0.006% unix nanbox: -4 -0.001% stm32: -184 -0.047% PYBV10 cc3200: -120 -0.065% esp8266: -228 -0.033% GENERIC esp32: -268 -0.018% GENERIC[incl +16(data)] nrf: -152 -0.087% pca10040 rp2: -256 -0.052% PICO samd: -80 -0.057% ADAFRUIT_ITSYBITSY_M4_EXPRESS
2021-11-25py/objfun.h: Remove obsolete comments about entries in extra_args.Damien George
These two entries were removed in 049a7a81531a67e068d926ad50260578fb79f94c Signed-off-by: Damien George <damien@micropython.org>
2021-11-25py/lexer: Support nested [] and {} characters within f-string params.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-11-22py/runtime: Allow types to use both .attr and .locals_dict.Laurens Valk
Make it possible to proceed to a regular lookup in locals_dict if the custom type->attr fails. This allows type->attr to extend rather than completely replace the lookup in locals_dict. This is useful for custom builtin classes that have mostly regular methods but just a few special attributes/properties. This way, type->attr needs to deal with the special cases only and the default lookup will be used for generic methods. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2021-11-19py/modsys: Replace non-ASCII quote char with ASCII char.Damien George
The source code should stay 7-bit ASCII clean. Signed-off-by: Damien George <damien@micropython.org>
2021-11-19py/showbc: Print unary-op string when dumping bytecode.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-11-01py/gc: Add hook to run code during time consuming GC operations.Laurens Valk
This makes it possible for cooperative multitasking systems to keep running event loops during garbage collector operations. For example, this can be used to ensure that a motor control loop runs approximately each 5 ms. Without this hook, the loop time can jump to about 15 ms. Addresses #3475. Signed-off-by: Laurens Valk <laurens@pybricks.com>
2021-11-01py/mpconfig.h: Revert MICROPY_REPL_INFO to disabled at all levels.Damien George
This is an stm32-specific feature that's accessed via the pyb module, so not something that will be widely enabled. Signed-off-by: Damien George <damien@micropython.org>
2021-11-01py/mpconfig.h: Define the "extra" feature level.Jim Mussared
Some of these will later be moved to CORE or BASIC, but EXTRA is a good starting point based on what stm32 uses. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-10-21py/runtime: Fix crash when exc __new__ doesn't return an exc instance.Mike Wadsten
See CPython bug https://bugs.python.org/issue39091 for more details.
2021-10-15py: Add wrapper macros so hot VM functions can go in fast code location.Damien George
For example, on esp32 they can go in iRAM to improve performance. Signed-off-by: Damien George <damien@micropython.org>
2021-09-24py/builtinimport: Forward all debug printing to MICROPY_DEBUG_PRINTER.stijn
2021-09-19extmod: Add platform module.iabdalkader
It contains the compiler version, and underlying system HAL/SDK version.
2021-09-16all: Remove MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE.Jim Mussared
This commit removes all parts of code associated with the existing MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE optimisation option, including the -mcache-lookup-bc option to mpy-cross. This feature originally provided a significant performance boost for Unix, but wasn't able to be enabled for MCU targets (due to frozen bytecode), and added significant extra complexity to generating and distributing .mpy files. The equivalent performance gain is now provided by the combination of MICROPY_OPT_LOAD_ATTR_FAST_PATH and MICROPY_OPT_MAP_LOOKUP_CACHE (which has been enabled on the unix port in the previous commit). It's hard to provide precise performance numbers, but tests have been run on a wide variety of architectures (x86-64, ARM Cortex, Aarch64, RISC-V, xtensa) and they all generally agree on the qualitative improvements seen by the combination of MICROPY_OPT_LOAD_ATTR_FAST_PATH and MICROPY_OPT_MAP_LOOKUP_CACHE. For example, on a "quiet" Linux x64 environment (i3-5010U @ 2.10GHz) the change from CACHE_MAP_LOOKUP_IN_BYTECODE, to LOAD_ATTR_FAST_PATH combined with MAP_LOOKUP_CACHE is: diff of scores (higher is better) N=2000 M=2000 bccache -> attrmapcache diff diff% (error%) bm_chaos.py 13742.56 -> 13905.67 : +163.11 = +1.187% (+/-3.75%) bm_fannkuch.py 60.13 -> 61.34 : +1.21 = +2.012% (+/-2.11%) bm_fft.py 113083.20 -> 114793.68 : +1710.48 = +1.513% (+/-1.57%) bm_float.py 256552.80 -> 243908.29 : -12644.51 = -4.929% (+/-1.90%) bm_hexiom.py 521.93 -> 625.41 : +103.48 = +19.826% (+/-0.40%) bm_nqueens.py 197544.25 -> 217713.12 : +20168.87 = +10.210% (+/-3.01%) bm_pidigits.py 8072.98 -> 8198.75 : +125.77 = +1.558% (+/-3.22%) misc_aes.py 17283.45 -> 16480.52 : -802.93 = -4.646% (+/-0.82%) misc_mandel.py 99083.99 -> 128939.84 : +29855.85 = +30.132% (+/-5.88%) misc_pystone.py 83860.10 -> 82592.56 : -1267.54 = -1.511% (+/-2.27%) misc_raytrace.py 21490.40 -> 22227.23 : +736.83 = +3.429% (+/-1.88%) This shows that the new optimisations are at least as good as the existing inline-bytecode-caching, and are sometimes much better (because the new ones apply caching to a wider variety of map lookups). The new optimisations can also benefit code generated by the native emitter, because they apply to the runtime rather than the generated code. The improvement for the native emitter when LOAD_ATTR_FAST_PATH and MAP_LOOKUP_CACHE are enabled is (same Linux environment as above): diff of scores (higher is better) N=2000 M=2000 native -> nat-attrmapcache diff diff% (error%) bm_chaos.py 14130.62 -> 15464.68 : +1334.06 = +9.441% (+/-7.11%) bm_fannkuch.py 74.96 -> 76.16 : +1.20 = +1.601% (+/-1.80%) bm_fft.py 166682.99 -> 168221.86 : +1538.87 = +0.923% (+/-4.20%) bm_float.py 233415.23 -> 265524.90 : +32109.67 = +13.756% (+/-2.57%) bm_hexiom.py 628.59 -> 734.17 : +105.58 = +16.796% (+/-1.39%) bm_nqueens.py 225418.44 -> 232926.45 : +7508.01 = +3.331% (+/-3.10%) bm_pidigits.py 6322.00 -> 6379.52 : +57.52 = +0.910% (+/-5.62%) misc_aes.py 20670.10 -> 27223.18 : +6553.08 = +31.703% (+/-1.56%) misc_mandel.py 138221.11 -> 152014.01 : +13792.90 = +9.979% (+/-2.46%) misc_pystone.py 85032.14 -> 105681.44 : +20649.30 = +24.284% (+/-2.25%) misc_raytrace.py 19800.01 -> 23350.73 : +3550.72 = +17.933% (+/-2.79%) In summary, compared to MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE, the new MICROPY_OPT_LOAD_ATTR_FAST_PATH and MICROPY_OPT_MAP_LOOKUP_CACHE options: - are simpler; - take less code size; - are faster (generally); - work with code generated by the native emitter; - can be used on embedded targets with a small and constant RAM overhead; - allow the same .mpy bytecode to run on all targets. See #7680 for further discussion. And see also #7653 for a discussion about simplifying mpy-cross options. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-09-16py/map: Add an optional cache of (map+index) to speed up map lookups.Jim Mussared
The existing inline bytecode caching optimisation, selected by MICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE, reserves an extra byte in the bytecode after certain opcodes, which at runtime stores a map index of the likely location of this field when looking up the qstr. This scheme is incompatible with bytecode-in-ROM, and doesn't work with native generated code. It also stores bytecode in .mpy files which is of a different format to when the feature is disabled, making generation of .mpy files more complex. This commit provides an alternative optimisation via an approach that adds a global cache for map offsets, then all mp_map_lookup operations use it. It's less precise than bytecode caching, but allows the cache to be independent and external to the bytecode that is executing. It also works for the native emitter and adds a similar performance boost on top of the gain already provided by the native emitter. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-09-16py/vm: Add a fast path for LOAD_ATTR on instance types.Jim Mussared
When the LOAD_ATTR opcode is executed there are quite a few different cases that have to be handled, but the common case is accessing a member on an instance type. Typically, built-in types provide methods which is why this is common. Fortunately, for this specific case, if the member is found in the member map then there's no further processing. This optimisation does a relatively cheap check (type is instance) and then forwards directly to the member map lookup, falling back to the regular path if necessary. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-09-16py/mpconfig.h: Define initial templates for "feature levels".Jim Mussared
This is the beginning of a set of changes to simplify enabling/disabling features. The goals are: - Remove redundancy from mpconfigport.h (never set a value to the default -- make it clear exactly what's being enabled). - Improve consistency between ports. All "similar" ports (i.e. approx same flash size) should get the same features. - Simplify mpconfigport.h -- just get default/sensible options for the size of the port. - Make it easy for defining constrained boards (e.g. STM32F0/L0), they can just set a lower level. This commit makes a step towards this and defines the "core" level as the current default feature set, and a "minimal" level to turn off everything. And a few placeholder levels are added for where the other ports will roughly land. This is a no-op change for all ports. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-09-13py/emitnative: Ensure load_subscr does not clobber existing REG_RET.Damien George
Fixes issue #7782, and part of issue #6314. Signed-off-by: Damien George <damien@micropython.org>
2021-09-10py/parse: Simplify parse nodes representing a list.Damien George
This commit simplifies and optimises the parse tree in-memory representation of lists of expressions, for tuples and lists, and when tuples are used on the left-hand-side of assignments and within del statements. This reduces memory usage of the parse tree when such code is compiled, and also reduces the size of the compiler. For example, (1,) was previously the following parse tree: expr_stmt(5) (n=2) atom_paren(45) (n=1) testlist_comp(146) (n=2) int(1) testlist_comp_3b(149) (n=1) NULL NULL and with this commit is now: expr_stmt(5) (n=2) atom_paren(45) (n=1) testlist_comp(146) (n=1) int(1) NULL Similarly, (1, 2, 3) was previously: expr_stmt(5) (n=2) atom_paren(45) (n=1) testlist_comp(146) (n=2) int(1) testlist_comp_3c(150) (n=2) int(2) int(3) NULL and is now: expr_stmt(5) (n=2) atom_paren(45) (n=1) testlist_comp(146) (n=3) int(1) int(2) int(3) NULL Signed-off-by: Damien George <damien@micropython.org>
2021-09-04extmod/machine_pwm: Factor out machine.PWM bindings to common code.Damien George
This commit refactors machine.PWM and creates extmod/machine_pwm.c. The esp8266, esp32 and rp2 ports all use this and provide implementations of the required PWM functionality. This helps to reduce code duplication and keep the same Python API across ports. This commit does not make any functional changes. Signed-off-by: Damien George <damien@micropython.org>
2021-09-02extmod/modonewire: Make _onewire module configurable via macro option.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-09-02extmod/machine_spi: Make SoftSPI configurable via macro option.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-09-02extmod/machine_i2c: Make SoftI2C configurable via macro option.Damien George
The zephyr port doesn't support SoftI2C so it's not enabled, and the legacy I2C constructor check can be removed. Signed-off-by: Damien George <damien@micropython.org>
2021-09-02all: Bump version to 1.17.v1.17Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-08-20stm32: Disable computed goto on constrained boards.Jim Mussared
Saves ~1kiB. Add comment to this effect to mpconfig.h. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19py/mkrules.mk: Do submodule sync in "make submodules".Jim Mussared
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19stm32: Add implementation of machine.bitstream.Jim Mussared
Hand-written version for M0, and cycle-counter version for everything else. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19extmod: Add machine.bitstream.Jim Mussared
This is a generic API for synchronously bit-banging data on a pin. Initially this adds a single supported encoding, which supports controlling WS2812 LEDs. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-19py/lexer: Clear fstring_args vstr on lexer free.Jim Mussared
This was missed in 692d36d779192f32371f7f9daa845b566f26968d. It's not strictly necessary as the GC will clean it anyway, but it's good to pre-emptively gc_free() all the blocks used in lexing/parsing. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-14py: Implement partial PEP-498 (f-string) support.Jim Mussared
This implements (most of) the PEP-498 spec for f-strings and is based on https://github.com/micropython/micropython/pull/4998 by @klardotsh. It is implemented in the lexer as a syntax translation to `str.format`: f"{a}" --> "{}".format(a) It also supports: f"{a=}" --> "a={}".format(a) This is done by extracting the arguments into a temporary vstr buffer, then after the string has been tokenized, the lexer input queue is saved and the contents of the temporary vstr buffer are injected into the lexer instead. There are four main limitations: - raw f-strings (`fr` or `rf` prefixes) are not supported and will raise `SyntaxError: raw f-strings are not supported`. - literal concatenation of f-strings with adjacent strings will fail "{}" f"{a}" --> "{}{}".format(a) (str.format will incorrectly use the braces from the non-f-string) f"{a}" f"{a}" --> "{}".format(a) "{}".format(a) (cannot concatenate) - PEP-498 requires the full parser to understand the interpolated argument, however because this entirely runs in the lexer it cannot resolve nested braces in expressions like f"{'}'}" - The !r, !s, and !a conversions are not supported. Includes tests and cpydiffs. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-08-07py/mkrules: Automatically build mpy-cross if it doesn't exist.Damien George
Commit 41739506589ec8397613c86d8f682fb7f86c0a9f removed automatic building of mpy-cross, which rebuilt it whenever any of its dependent source files changed. But needing to build mpy-cross, and not knowing how, is a frequent issue. This commit aims to help by automatically building mpy-cross only if it doesn't exist. For Makefiles it uses an order-only prerequisite, while for CMake it uses a custom command. If MICROPY_MPYCROSS (which is what makemanifest.py uses to locate the mpy-cross executable) is defined in the environment then automatic build will not be attempted, allowing a way to prevent this auto-build if needed. Thanks to Trammell Hudson aka @osresearch for the original idea; see #5760. Signed-off-by: Damien George <damien@micropython.org>
2021-08-07extmod/modujson: Add support for dump/dumps separators keyword-argument.Peter Züger
Optionally enabled via MICROPY_PY_UJSON_SEPARATORS. Enabled by default. For dump, make sure mp_get_stream_raise is called after mod_ujson_separators since CPython does it in this order (if both separators and stream are invalid, separators will raise an exception first). Add separators argument in the docs as well. Signed-off-by: Peter Züger <zueger.peter@icloud.com> Signed-off-by: Damien George <damien@micropython.org>
2021-07-31py/builtinimport: Fix condition for including do_execute_raw_code().David Lechner
Commit e33bc597 ("py: Remove calls to file reader functions when these are disabled.") changed the condition for one caller of do_execute_raw_code() from MICROPY_PERSISTENT_CODE_LOAD to MICROPY_HAS_FILE_READER && MICROPY_PERSISTENT_CODE_LOAD The condition that enables compiling the function itself needs to be changed to match. Signed-off-by: David Lechner <david@pybricks.com>
2021-07-23py/runtime: Fix bool unary op for subclasses of native types.Jim Mussared
Previously a subclass of a type that didn't implement unary_op, or didn't handle MP_UNARY_OP_BOOL, would raise TypeError on bool conversion. Fixes #5677.
2021-07-19py/emitnative: Ensure stack settling is safe mid-branch.Jim Mussared
And add a test for the case where REG_RET could be in use. Fixes #7523. Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2021-07-19py/emitnative: Reuse need_reg_all func in need_stack_settled.Damien George
To reduce code size and code duplication. Signed-off-by: Damien George <damien@micropython.org>
2021-07-17extmod/moduselect: Conditionally compile select().David Lechner
This adds #if MICROPY_PY_USELECT_SELECT around the uselect.select() function. According to the docs, this function is only for CPython compatibility and should not normally be used. So we can disable it and save a few bytes of flash space where possible. Signed-off-by: David Lechner <david@pybricks.com>
2021-07-15py/obj: Fix formatting of comment for mp_obj_is_integer.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/objexcept: Make mp_obj_new_exception_arg1 inline.Damien George
This function is rarely used so making it inline reduces code size. Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/modsys: Optimise sys.exit for code size by using exception helpers.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py: Introduce and use mp_raise_type_arg helper.Damien George
To reduce code size. Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py: Support single argument to optimised MP_OBJ_STOP_ITERATION.Damien George
The MP_OBJ_STOP_ITERATION optimisation is a shortcut for creating a StopIteration() exception object, and means that heap memory does not need to be allocated for the exception (in cases where it can be used). This commit allows this optimised object to take an optional argument (before, it could only have no argument). The commit also adds some new tests to cover corner cases with StopIteration and generators that previously did not work. Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/objexcept: Make mp_obj_exception_get_value support subclassed excs.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-15py/vm: Simplify handling of MP_OBJ_STOP_ITERATION in yield-from opcode.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-12all: Update to point to files in new shared/ directory.Damien George
Signed-off-by: Damien George <damien@micropython.org>
2021-07-08windows/Makefile: Add .exe extension to executables name.Bryan Tong Minh
Uses the same logic applied in 5b57ae985ff7064dd7b09b0ce891697bfaa5dae2 to determine when to add .exe. See related: #3310, #3361, #3370, #4143, #5727.
2021-07-05py/makeversionhdr: Add --tags arg to git describe.David Lechner
This adds the --tags argument to the git describe command that is used to define the MICROPY_GIT_TAG macro. This makes it match non-annotated tags. This is useful for MicroPython derivatives that don't use annotated tags. Signed-off-by: David Lechner <david@pybricks.com>