Commit Graph

169 Commits

Author SHA1 Message Date
Scott Shawcroft b19c1a310e
mpy M -> C 2021-05-06 13:31:20 -07:00
Jeff Epler c174bc42fa Update to match our bytecode style 2021-05-05 09:21:42 -05:00
Jeff Epler d0f6b323bb Update bytecode 2021-05-05 09:05:44 -05:00
Scott Shawcroft f0bb26d70f
Merge MicroPython 1.13 into CircuitPython 2021-05-04 18:06:33 -07:00
Scott Shawcroft b35fa44c8a
Merge MicroPython 1.12 into CircuitPython 2021-05-03 14:01:18 -07:00
Scott Shawcroft 76033d5115
Merge MicroPython v1.11 into CircuitPython 2021-04-26 15:47:41 -07:00
Scott Shawcroft 09f7b43c64
Merge MicroPython 1.10 into CircuitPython 2021-04-21 15:59:17 -07:00
Scott Shawcroft b057fb8a4b
codeformat 2021-04-19 22:22:44 -07:00
microDev f9b4189b4c
update line numbering 2021-03-15 23:01:23 +05:30
microDev a52eb88031
run code formatting script 2021-03-15 19:27:36 +05:30
Damien George 7a97e4351b tests: Move native for test from pybnative to micropython.
And make it generic so it can be run on any target.

Signed-off-by: Damien George <damien@micropython.org>
2021-01-29 23:57:10 +11:00
Damien George ed7ddd4dd4 tests/micropython/extreme_exc.py: Unlink alloc'd lists earlier in chain.
To help the GC collect this memory that's no longer needed after the test.

Signed-off-by: Damien George <damien@micropython.org>
2020-10-29 23:35:36 +11:00
stijn 40ad8f1666 all: Rename "sys" module to "usys".
This is consistent with the other 'micro' modules and allows implementing
additional features in Python via e.g. micropython-lib's sys.

Note this is a breaking change (not backwards compatible) for ports which
do not enable weak links, as "import sys" must now be replaced with
"import usys".
2020-09-04 00:10:24 +10:00
Damien George 9883d8e818 py/persistentcode: Maintain root ptr list of imported native .mpy code.
On ports where normal heap memory can contain executable code (eg ARM-based
ports such as stm32), native code loaded from an .mpy file may be reclaimed
by the GC because there's no reference to the very start of the native
machine code block that is reachable from root pointers (only pointers to
internal parts of the machine code block are reachable, but that doesn't
help the GC find the memory).

This commit fixes this issue by maintaining an explicit list of root
pointers pointing to native code that is loaded from an .mpy file.  This
is not needed for all ports so is selectable by the new configuration
option MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE.  It's enabled by default
if a port does not specify any special functions to allocate or commit
executable memory.

A test is included to test that native code loaded from an .mpy file does
not get reclaimed by the GC.

Fixes #6045.

Signed-off-by: Damien George <damien@micropython.org>
2020-08-02 22:34:09 +10:00
Damien George 8da40baa47 tests/micropython: Improve .mpy import tests to run on more targets.
All imports are now tested to see if the test should be skipped,
UserFile.read is removed, and UserFile.readinto is made more efficient.

Signed-off-by: Damien George <damien@micropython.org>
2020-07-26 22:44:59 +10:00
Damien George 0c0cef9870 tests: Move .mpy import tests from import/ to micropython/ dir.
These tests are specific to MicroPython so have a better home in the
micropython/ test subdir, and putting them here allows them to be run by
all targets, not just those that have access to the local filesystem (eg
the unix port).

Signed-off-by: Damien George <damien@micropython.org>
2020-07-26 22:04:31 +10:00
Damien George 41fa8b5482 py/emitnative: Implement binary operations for viper uint operands.
uint types in viper mode can now be used for all binary operators except
floor-divide and modulo.

Fixes issue #1847 and issue #6177.

Signed-off-by: Damien George <damien@micropython.org>
2020-06-27 00:24:04 +10:00
Damien George 4ede703687 py/parse: Support constant folding of power operator for integers.
Constant expression like "2 ** 3" will now be folded, and the special form
"X = const(2 ** 3)" will now compile because the argument to the const is
now a constant.

Fixes issue #5865.
2020-05-03 16:23:19 +10:00
Damien George 4914731e58 py/parse: Remove unnecessary check in const folding for ** operator.
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-09 16:02:39 +10:00
Jim Mussared 85858e72df py/objexcept: Allow compression of exception message text.
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-05 15:02:06 +10:00
David Lechner 3dc324d3f1 tests: Format all Python code with black, except tests in basics subdir.
This adds the Python files in the tests/ directory to be formatted with
./tools/codeformat.py.  The basics/ subdirectory is excluded for now so we
aren't changing too much at once.

In a few places `# fmt: off`/`# fmt: on` was used where the code had
special formatting for readability or where the test was actually testing
the specific formatting.
2020-03-30 13:21:58 +11:00
David Lechner 488613bca6 tests/micropython/heapalloc_fail_set.py: Remove extra trailing comma.
Unlike tuples, sets do not need trailing comma when there is only one item.
2020-03-30 13:10:24 +11:00
Andrew Leech 86bfabec11 py/modmicropython: Add heap_locked function to test state of heap.
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-11 16:54:16 +11:00
Damien George de8c04317b tests/micropython: Add test for yield-from while heap is locked. 2019-12-27 12:34:22 +11:00
Damien George 30e25174bb tests: Rename "array" module to "uarray". 2019-10-22 19:16:54 +11:00
Damien George 095f90f04e tests/micropython: Add test for native generators. 2019-09-26 16:53:47 +10:00
Damien George b3152b2de7 tests: Split out test for optimisation level and line-no printing. 2019-08-28 12:47:58 +10:00
Damien George cd35dd9d9a py: Allow to pass in read-only buffers to viper and inline-asm funcs.
Fixes #4936.
2019-08-06 15:58:23 +10:00
Damien George 5ea38e4d74 py/native: Improve support for bool type in viper functions.
Variables with type bool now act more like an int, and there is proper
casting to/from Python objects.
2019-05-03 23:18:30 +10:00
Damien George eb1f81b209 tests/micropython: Add some tests for failed heap allocation.
This adds tests for some locations in the code where a memory allocation
should raise an exception.
2019-04-18 14:34:12 +10:00
Damien George ac81cee3fc tests/micropython: Test loading const objs in native and viper funcs. 2018-09-27 23:39:08 +10:00
Damien George b01f66c5f1 py: Shorten error messages by using contractions and some rewording. 2018-09-20 14:33:10 +10:00
Damien George 93d71c5436 py/emitnative: Make viper funcs run with their correct globals context.
Viper functions will now capture the globals at the point they were defined
and use these globals when executing.
2018-09-15 22:39:27 +10:00
Damien George a676b5acf6 py/emitnative: Support arbitrary number of arguments to viper functions. 2018-09-15 22:39:27 +10:00
Damien George 9f2067288a py/compile: Factor code that compiles viper type annotations. 2018-09-15 13:44:39 +10:00
Damien George f774614110 tests/micropython: Add tests for try and with blocks under native/viper. 2018-08-17 14:11:36 +10:00
Damien George 49529f22d4 tests/micropython/viper_cond: Add test for large int as bool. 2018-08-04 22:16:24 +10:00
Damien George e686c94052 py/emit: Combine yield value and yield-from emit funcs into one.
Reduces code size by:

   bare-arm:   -24
minimal x86:   -72
   unix x64:  -200
unix nanbox:   -72
      stm32:   -52
     cc3200:   -32
    esp8266:   -84
      esp32:   -24
2018-05-23 00:22:35 +10:00
Damien George b9c78425a6 tests/micropython/extreme_exc.py: Allow to run without any emg exc buf. 2018-04-05 03:03:16 +10:00
Damien George 4caadc3c01 tests/micropython/extreme_exc.py: Fix test to run on more ports/configs. 2018-04-05 02:33:48 +10:00
Damien George 5995a199a3 tests/micropython: Add set of tests for extreme cases of raising exc's. 2018-04-05 01:06:40 +10:00
Damien George c607b58efe tests: Move heap-realloc-while-locked test from C to Python.
This test for calling gc_realloc() while the GC is locked can be done in
pure Python, so better to do it that way since it can then be tested on
more ports.
2018-03-02 10:59:09 +11:00
Damien George 6031957473 tests: Automatically skip tests that require eval, exec or frozenset. 2018-02-14 16:46:44 +11:00
Paul Sokolovsky 016f830536 tests/heapalloc, heapalloc_super: Skip in strict stackless mode.
These tests involves testing allocation-free function calling, and in strict
stackless mode, it's not possible to make a function call with heap locked
(because function activation record aka frame is allocated on the heap).
2017-12-11 12:04:59 +02:00
Paul Sokolovsky e02cb9ec31 tests/heapalloc_*: Refactor some tests to work in strict stackless mode.
In strict stackless mode, it's not possible to make a function call with
heap locked (because function activation record aka frame is allocated on
heap). So, if the only purpose of function is to introduce local variable
scope, move heap lock/unlock calls inside the function.
2017-12-11 12:00:41 +02:00
Damien George 5e34a113ea py/runtime: Add MP_BINARY_OP_CONTAINS as reverse of MP_BINARY_OP_IN.
Before this patch MP_BINARY_OP_IN had two meanings: coming from bytecode it
meant that the args needed to be swapped, but coming from within the
runtime meant that the args were already in the correct order.  This lead
to some confusion in the code and comments stating how args were reversed.
It also lead to 2 bugs: 1) containment for a subclass of a native type
didn't work; 2) the expression "{True} in True" would illegally succeed and
return True.  In both of these cases it was because the args to
MP_BINARY_OP_IN ended up being reversed twice.

To fix these things this patch introduces MP_BINARY_OP_CONTAINS which
corresponds exactly to the __contains__ special method, and this is the
operator that built-in types should implement.  MP_BINARY_OP_IN is now only
emitted by the compiler and is converted to MP_BINARY_OP_CONTAINS by
swapping the arguments.
2017-11-24 14:48:23 +11:00
Damien George a3afa8cfc4 py/emitnative: Implement floor-division and modulo for viper emitter. 2017-10-11 18:54:34 +11:00
Damien George 96fd80db13 py/objexcept: Prevent infinite recursion when allocating exceptions.
The aim of this patch is to rewrite the functions that create exception
instances (mp_obj_exception_make_new and mp_obj_new_exception_msg_varg) so
that they do not call any functions that may raise an exception.  Otherwise
it's possible to create infinite recursion with an exception being raised
while trying to create an exception object.

The two main things that are done to accomplish this are:
1. Change mp_obj_new_exception_msg_varg to just format the string, then
   call mp_obj_exception_make_new to actually create the exception object.
2. In mp_obj_exception_make_new and mp_obj_new_exception_msg_varg try to
   allocate all memory first using functions that don't raise exceptions
   If any of the memory allocations fail (return NULL) then degrade
   gracefully by trying other options for memory allocation, eg using the
   emergency exception buffer.
3. Use a custom printer backend to conservatively format strings: if it
   can't allocate memory then it just truncates the string.

As part of this rewrite, raising an exception without a message, like
KeyError(123), will now use the emergency buffer to store the arg and
traceback data if there is no heap memory available.

Memory use with this patch is unchanged.  Code size is increased by:

   bare-arm:  +136
minimal x86:  +124
   unix x64:   +72
unix nanbox:   +96
      stm32:   +88
    esp8266:   +92
     cc3200:   +80
2017-09-21 15:24:57 +10:00
Paul Sokolovsky 85d809d1f4 tests: Convert remaining "sys.exit()" to "raise SystemExit". 2017-06-10 20:34:38 +03:00
Paul Sokolovsky 07241cd37a py/objstringio: If created from immutable object, follow copy on write policy.
Don't create copy of immutable object's contents until .write() is called
on BytesIO.
2017-06-09 17:33:01 +03:00
Damien George dce7dd4259 tests/micropython: Add test for int.from_bytes with many zero bytes. 2017-05-06 10:29:09 +10:00
Damien George 30badd1ce1 tests: Add tests for calling super and loading a method directly. 2017-04-22 23:39:38 +10:00
Damien George c7c14f1634 tests/micropython: Add test for micropython.kbd_intr(). 2017-04-18 17:24:30 +10:00
Paul Sokolovsky 806c07c898 tests/micropython/heapalloc_iter: Improve skippability. 2017-04-03 00:27:01 +03:00
Paul Sokolovsky 4a4bb84e92 tests/heapalloc_str: Test no-replacement case for str.replace(). 2017-03-22 22:17:52 +03:00
Damien George c772817dee tests/micropython: Add tests for micropython.schedule(). 2017-03-20 15:20:26 +11:00
Damien George e29f704b67 tests/micropython/viper_error: Add more tests to improve coverage. 2017-03-14 23:05:41 +11:00
Paul Sokolovsky bc5bffbf65 tests/micropython/opt_level: Clarify the expected output for opt_level == 3. 2017-03-09 23:22:31 +01:00
Paul Sokolovsky 53018d5ad2 tests/micropython/heapalloc_traceback: Fix backtrace line # after refactor. 2017-03-09 12:51:45 +01:00
Paul Sokolovsky 1a71d30fb8 tests/micropython: Make uio-using tests skippable. 2017-03-09 10:26:31 +01:00
Paul Sokolovsky c98d7461a1 tests/micropython/: Split off intbig tests. 2017-03-07 07:12:58 +01:00
Damien George f62503dc47 tests/micropython: Add test for consts that are bignums. 2017-02-24 13:08:18 +11:00
Paul Sokolovsky 6fc6f10b1e tests/heapalloc_exc_raise.py: Heap alloc test for raising/catching exc. 2017-02-20 04:22:32 +03:00
Damien George 7839b8b827 tests/micropython/heapalloc_iter: Add tests for contains and unpack. 2017-02-16 19:11:34 +11:00
Damien George 019048a6dc tests/micropython: Add test for iterating with the heap locked. 2017-02-16 19:11:34 +11:00
Paul Sokolovsky b32880bd51 tests/heapalloc_bytesio: Test for BytesIO with preallocates space. 2017-02-02 00:38:38 +03:00
Paul Sokolovsky 9ffc3ae0e7 tests/heapalloc_str: Test for alloc-free string operations.
Starts with concatenation with an empty string.
2017-01-27 00:49:39 +03:00
Paul Sokolovsky bd3dd9296b tests/heapalloc_int_from_bytes: Test that int.from_bytes() can work w/o alloc.
For a small number of bytes, it's expected to return a small int without
allocation.
2017-01-21 20:15:56 +03:00
Damien George fb5838041b tests/micropython/opt_level: Add test for opt_level 3. 2017-01-19 23:38:11 +11:00
Rami Ali eae819c0ed tests/micropython: Add test for micropython.stack_use() function. 2016-12-28 17:46:52 +11:00
Paul Sokolovsky 05aebb9206 tests/heapalloc_inst_call: Test for no alloc for simple object calls. 2016-12-25 00:50:27 +03:00
Damien George 7081ea4119 tests/micropython: Get heapalloc_traceback test running on baremetal.
When printing exceptions from files sent to a target by pyboard.py the
filename in the exception is <stdin>, which differs to when running the
script on the PC.  So we strip out the filename to make the outputs the
same on all targets (see also misc/print_exception.py test).
2016-12-15 11:11:57 +11:00
Damien George 1f43d49f9e tests/micropython: Move alloc-less traceback test to separate test file.
The native emitter doesn't provide proper traceback info so this test
should not be run in that case.
2016-11-21 17:39:23 +11:00
Damien George d70f87aaa2 tests/micropython: Add test for creating traceback without allocation. 2016-11-21 17:10:17 +11:00
Damien George 679c0c4c83 tests/micropython: Add test for import from within viper function. 2016-11-10 22:54:55 +11:00
Damien George 34d0b3f85c tests/micropython: Add tests for heap_lock, and emergency exceptions. 2016-10-14 00:32:34 +11:00
Damien George 5e22afce41 tests: Improve test coverage of py/compile.c. 2016-10-11 12:30:32 +11:00
Damien George 7f0e563de3 tests/micropython: Add test for micropython.opt_level() function. 2016-10-11 11:01:22 +11:00
Damien George 791b65f4b2 py/modmicropython: Add micropython.const, alias for identity function.
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.
2016-09-27 13:34:21 +10:00
Damien George 38b54b65d4 tests/micropython: Add tests for const names being replaced in parser. 2016-09-27 13:03:53 +10:00
Paul Sokolovsky 92a9078a6f tests/meminfo: Update for addition of "max free sz" output. 2016-07-01 00:39:26 +03:00
Damien George b1533c4366 py/parse: Treat constants that start with underscore as private.
Assignments of the form "_id = const(value)" are treated as private
(following a similar CPython convention) and code is no longer emitted
for the assignment to a global variable.

See issue #2111.
2016-06-06 17:28:32 +01:00
Damien George 3e02b1d19a py/viper: Allow casting of Python integers to viper pointers.
This allows you to pass a number (being an address) to a viper function
that expects a pointer, and also allows casting of integers to pointers
within viper functions.

This was actually the original behaviour, but it regressed due to native
type identifiers being promoted to 4 bits in width.
2016-02-09 13:29:20 +00:00
Damien George 2ec835f572 tests: Add more tests for viper 16/32-bit load/store, and ellipsis. 2015-10-13 18:24:36 +01:00
Damien George b8f9ac5411 py: Implement ptr32 load and store in viper emitter. 2015-10-13 00:50:17 +01:00
Damien George 9f5f156b9d py/emitnative: Raise ViperTypeError for unsupported unary ops. 2015-10-08 13:08:59 +01:00
Paul Sokolovsky a3fe307400 tests: Consolidate all feature check snippets under feature_check/. 2015-08-30 11:36:42 +03:00
Damien George e45c1dbd6f py: Allow viper functions to take up to 4 arguments.
Addresses issue #1380.
2015-07-23 14:11:29 +01:00
Damien George 6e56bb623c py: Fallback to stack alloca for Python-stack if heap alloc fails.
If heap allocation for the Python-stack of a function fails then we may
as well allocate the Python-stack on the C stack.  This will allow to
run more code without using the heap.
2015-06-08 22:07:27 +01:00
Damien George 567b349c2b py: Implement native multiply operation in viper emitter. 2015-06-04 14:00:29 +00:00
Damien George 4d9cad180d py: Implement implicit cast to obj for viper load/store index/value.
This allows to do "ar[i]" and "ar[i] = val" in viper when ar is a Python
object and i and/or val are native viper types (eg ints).

Patch also includes tests for this feature.
2015-06-04 11:52:16 +01:00
Damien George 40d43ea88d tests: Add more tests for viper, including tests for ViperTypeError's. 2015-04-22 23:18:28 +01:00
Damien George 044c473de2 py: Add %q format support to mp_[v]printf, and use it. 2015-04-16 14:30:16 +00:00
Damien George 9dd3640464 tests: Add missing tests for builtins, and many other things. 2015-04-04 22:05:30 +01:00
Damien George 82f37bf0d1 tests: Add specific test for closures in native emitter. 2015-04-03 16:14:25 +01:00
Damien George 214179b430 tests: Add tests for SyntaxError, TypeError, and other missing things.
This is intended to improve coverage of the test suite.
2015-03-25 23:10:09 +00:00
Damien George af43565322 tests: Add tests for things that are not already tested.
The aim here is to improve coverage of the code.
2015-03-12 22:48:45 +00:00
Damien George 47538cc880 tests: Add test for micropython const feature when it has a SyntaxError. 2015-03-01 12:06:24 +00:00
Damien George ed8b4da0db tests: Remove obsolete test; don't use fp in micropython/ tests. 2015-02-15 01:57:39 +00:00
Damien George d1c3788375 py: Fix loading of immediate pointer in Thumb assembler.
Addresses issue #1117.
2015-02-15 00:45:28 +00:00