Commit Graph

5633 Commits

Author SHA1 Message Date
Damien George 24aa81e1da py/nlrsetjmp: Use MP_NLR_JUMP_HEAD macro to simplify code.
Signed-off-by: Damien George <damien@micropython.org>
2023-06-02 21:47:34 +10:00
Jim Mussared 6a8114eee8 py/objmodule: Don't use sys.modules to track a builtin __init__.
This can lead to duplicate initialisations if a module can be imported
via multiple names, so the module must track this itself anyway.

This reduces code size (diff is -40 bytes), and avoids special treatment of
builtin-modules-with-init with respect to sys.modules. No other builtin
modules get put into sys.modules.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01 16:21:37 +10:00
Jim Mussared ed90f30dd5 py/builtinimport: Allow builtin modules to be packages.
To use this:
 - Create a built-in module, and add the module object as a member of the
   parent module's globals dict.
 - The submodule can set its `__name__` to either `QSTR_foo_dot_bar` or
   `QSTR_bar`. The former requires using qstrdefs(port).h to make the qstr.

Because `bar` is a member of `foo`'s globals, it is possible to write
`import foo` and then immediately use `foo.bar` without importing it
explicitly. This means that if `bar` has an `__init__`, it will not be
called in this situation, and for that reason, sub-modules should not have
`__init__` methods. If this is required, then all initalisation for
sub-modules should be done by the top-level module's (i.e. `foo`'s)
`__init__` method.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01 16:21:37 +10:00
Jim Mussared 525557738c py/builtinimport: Optimise sub-package loading.
This makes it so that sub-packages are resolved relative to their parent's
`__path__`, rather than re-resolving each parent's filesystem path.

The previous behavior was that `import foo.bar` would first re-search
`sys.path` for `foo`, then use the resulting path to find `bar`.

For already-loaded and u-prefixed modules, because we no longer need to
build the path from level to level, we no longer unnecessarily search
the filesystem. This should improve startup time.

Explicitly makes the resolving process clear:
 - Loaded modules are returned immediately without touching the filesystem.
 - Exact-match of builtins are also returned immediately.
 - Then the filesystem search happens.
 - If that fails, then the weak-link handling is applied.

This maintains the existing behavior: if a user writes `import time` they
will get time.py if it exits, otherwise the built-in utime. Whereas `import
utime` will always return the built-in.

This also fixes a regression from a7fa18c203
where we search the filesystem for built-ins. It is now only possible to
override u-prefixed builtins. This will remove a lot of filesystem stats
at startup, as micropython-specific modules (e.g. `pyb`) will no longer
attempt to look at the filesystem.

Added several improvements to the comments and some minor renaming and
refactoring to make it clearer how the import mechanism works. Overall
code size diff is +56 bytes on STM32.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01 16:21:21 +10:00
Jim Mussared 42f3f66431 py/builtinimport: Handle empty sys.path correctly.
If sys.path is enabled, but empty, this will now no longer search the
filesystem. Previously an empty sys.path was equivalent to having
`sys.path=[""]`. This is a breaking change, but this behavior now matches
CPython.

This also provides an alternative mechanism to the u-prefix to force an
import of a builtin module:

```
import sys
_path = sys.path[:]
sys.path.clear()
import foo  # Forces the built-in foo.
sys.path.extend(_path)
del _path
```

Code size diff is -32 bytes on PYBV11.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-06-01 16:03:21 +10:00
Damien George 69dd013919 py/objint: Allow int() to parse anything with the buffer protocol.
This generalises and simplifies the code and follows CPython behaviour.

See similar change for floats in a07fc5b640.

Signed-off-by: Damien George <damien@micropython.org>
2023-06-01 15:11:06 +10:00
Damien George 66dc1397c9 py/obj: Accept user types in mp_obj_get_int_maybe.
This is possible now that MP_UNARY_OP_INT_MAYBE exists.

As a consequence mp_obj_get_int now also supports user types, which was
previously possible with MP_UNARY_OP_INT but no tests existed for it.

Signed-off-by: Damien George <damien@micropython.org>
2023-06-01 14:18:54 +10:00
Damien George 48ffd6596e py: Change MP_UNARY_OP_INT to MP_UNARY_OP_INT_MAYBE.
To be consistent with MP_UNARY_OP_INT_FLOAT and MP_UNARY_OP_INT_COMPLEX,
and allow int() to first check if a type supports __int__ before trying
other things (as per CPython).

Signed-off-by: Damien George <damien@micropython.org>
2023-06-01 13:01:07 +10:00
Jeff Epler fed8d5825b
synthio: add biquad filter type & basic filter calculations
the filter cannot be applied as yet.
2023-05-29 09:50:18 -05:00
Jeff Epler 76101c035e
core: Allow `enum` types to specify additional fields in the object
this will be used to make MathOperation enum values callable to
construct a Math object with that function
2023-05-21 13:22:48 -05:00
Jeff Epler 4da32a7b86
Merge remote-tracking branch 'origin/main' into synthio-lfo-dag 2023-05-19 11:56:13 -05:00
Damien George ea7031faff py/runtime: If inplace binop fails then try corresponding normal binop.
The code that handles inplace-operator to normal-binary-operator fallback
is moved in this commit from py/objtype.c to py/runtime.c, making it apply
to all types, not just user classes.

Signed-off-by: Damien George <damien@micropython.org>
2023-05-19 13:44:00 +10:00
Damien George 4b57330465 py/objstr: Return unsupported binop instead of raising TypeError.
So that user types can implement reverse operators and have them work with
str on the left-hand-side, eg `"a" + UserType()`.

Signed-off-by: Damien George <damien@micropython.org>
2023-05-19 13:42:35 +10:00
Damien George ca9068e0ef py/objarray: Disallow memoryview addition.
Following CPython.  This is important for subsequent commits to work
correctly.

Signed-off-by: Damien George <damien@micropython.org>
2023-05-19 13:33:54 +10:00
David Lechner 2fe6d4eb86 py/objdict: Fix __hash__ for dict_view types.
This adds a unary_op implementation for the dict_view type that makes
the implementation of `hash()` for these types compatible with CPython.

Signed-off-by: David Lechner <david@pybricks.com>
2023-05-19 12:06:17 +10:00
David Lechner 8491eb190f py/objslice: Ensure slice is not hashable.
As per https://bugs.python.org/issue408326, the slice object should not be
hashable.  Since MicroPython has an implicit fallback when the unary_op
slot is empty, we need to fill this slot.

Signed-off-by: David Lechner <david@pybricks.com>
2023-05-19 12:06:06 +10:00
David Lechner eaccaa3677 py/obj: Remove mp_generic_unary_op().
Since converting to variable sized slots in mp_obj_type_t, we can now
reduce the code size a bit by removing mp_generic_unary_op() and the
corresponding slots where it is used. Instead we just implement the
generic `__hash__` operation in the runtime.

Signed-off-by: David Lechner <david@pybricks.com>
2023-05-19 12:04:44 +10:00
Scott Shawcroft 9d11bda9e8
Fix memoryview.cast over sliced memoryview
Fixes #4758
2023-05-18 13:26:50 -07:00
Jeff Epler 391438102b
synthio: Finish implementing Math blocks 2023-05-17 16:32:20 -05:00
Jeff Epler f83212314e
synthio: Add LFOs 2023-05-15 21:15:39 -05:00
Phil Underwood 0f96711ccd Fix for #2204 2023-05-15 21:35:16 +01:00
David Lechner 468ed218c9 py/gc: Make improvements to MICROPY_GC_HOOK_LOOP.
Changes in this commit:
- Add MICROPY_GC_HOOK_LOOP to gc_info() and gc_alloc().  Both of these can
  be long running (many milliseconds) which is too long to be blocking in
  some applications.
- Pass loop variable to MICROPY_GC_HOOK_LOOP(i) macro so that implementers
  can use it, e.g. to improve performance by only calling a function every
  X number of iterations.
- Drop outer call to MICROPY_GC_HOOK_LOOP in gc_mark_subtree().
2023-05-09 12:44:14 +10:00
Jeff Epler a7da245ad0
synthio: Add synthio.Note
This class allows much more expressive sound synthesis:
 * tremolo & vibrato
 * arbitrary frequency
 * different evelope & waveform per note
 * all properties dynamically settable from Python code
2023-05-04 07:23:49 -05:00
Jeff Epler c22fd2a18e
runtime: Add mp_arg_validate_float_range 2023-05-04 07:23:48 -05:00
Damien George f1c6cb7725 py/stackctrl: Add gcc pragmas to ignore dangling-pointer warning.
This warning became apparent in gcc 13.

Signed-off-by: Damien George <damien@micropython.org>
2023-05-04 10:08:12 +10:00
Jeff Epler bd9aca2526
Merge pull request #7862 from jepler/synthio-envelope
Synthio envelope
2023-05-03 12:42:56 -05:00
Damien George 1b980c9dbe py/compile: Remove over-eager optimisation of tuples as if condition.
When a tuple is the condition of an if statement, it's only possible to
optimise that tuple away when it is a constant tuple (ie all its elements
are constants), because if it's not constant then the elements must be
evaluated in case they have side effects (even though the resulting tuple
will always be "true").

The code before this change handled the empty tuple OK (because it doesn't
need to be evaluated), but it discarded non-empty tuples without evaluating
them, which is incorrect behaviour (as show by the updated test).

This optimisation is anyway rarely applied because it's not common Python
coding practice to write things like `if (): ...` and `if (1, 2): ...`, so
removing this optimisation completely won't affect much code, if any.

Furthermore, when MICROPY_COMP_CONST_TUPLE is enabled, constant tuples are
already optimised by the parser, so expression with constant tuples like
`if (): ...` and `if (1, 2): ...` will continue to be optimised properly
(and so when this option is enabled the code that's deleted in this commit
is actually unreachable when the if condition is a constant tuple).

Signed-off-by: Damien George <damien@micropython.org>
2023-05-03 13:21:18 +10:00
Damien George 957bd51184 py/parse: Fix build when COMP_CONST_FOLDING=0 and COMP_MODULE_CONST=1.
Signed-off-by: Damien George <damien@micropython.org>
2023-05-03 13:12:54 +10:00
glenn20 9d735d1be7 py/ringbuf: Implement put_bytes/get_bytes functions. 2023-05-01 16:47:03 +10:00
Scott Shawcroft 18207cd547
Guard GCC13 pragma 2023-04-28 11:28:48 -07:00
Scott Shawcroft a9dc31a881
Add additional iMX RT support
This adds a script to generate the peripherals files (except clock).

It adds support for the 1015, 1020, 1040, and 1050 EVKs.

Some work was started on 1176 but it isn't working. So, the board
def is in a separate branch.

Fixes #3521. Fixes #2477.
2023-04-28 11:01:13 -07:00
Damien George b1229efbd1 all: Fix spelling mistakes based on codespell check.
Signed-off-by: Damien George <damien@micropython.org>
2023-04-27 18:03:06 +10:00
Damien George 9955553001 extmod/modutime: Provide a generic time module.
Based on extmod/utime_mphal.c, with:
- a globals dict added
- time.localtime wrapper added
- time.time wrapper added
- time.time_ns function added

New configuration options are added for this module:
- MICROPY_PY_UTIME (enabled at basic features level)
- MICROPY_PY_UTIME_GMTIME_LOCALTIME_MKTIME
- MICROPY_PY_UTIME_TIME_TIME_NS

Signed-off-by: Damien George <damien@micropython.org>
2023-04-27 15:09:56 +10:00
Damien George 294baf52b3 all: Bump version to 1.20.0.
Signed-off-by: Damien George <damien@micropython.org>
2023-04-26 15:42:28 +10:00
Jim Mussared 256f47e2f8 extmod/btstack: Fix indicate/notify queuing.
This adds a mechanism to track a pending notify/indicate operation that
is deferred due to the send buffer being full. This uses a tracked alloc
that is passed as the content arg to the callback.

This replaces the previous mechanism that did this via the global pending
op queue, shared with client read/write ops.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-04-26 11:37:20 +10:00
Marco Sirabella 863042dfd0 Move git command to update necessary tags to submodule
I ran into this one since I did an initial shallow clone due to slow
pycon 2023 wifi.
2023-04-25 16:05:20 -06:00
Marco Sirabella 6c85034952 Bump recommended shallow-since 1.5 years forward 2023-04-25 15:42:01 -06:00
Jeff Epler ca6ea83ff5
Fix `non_negative` validator to allow 0.0 2023-04-24 22:19:38 -05:00
Jeff Epler c4104898ce
add additional float validators 2023-04-24 22:19:38 -05:00
Jeff Epler 8e3c28e97d
Fix `memoryview.cast` in micropython-coverage
`locals_dict` and `attr` are incompatible, so just use circuitpython-style
properties so that a property and a method are both available.

this makes no difference in circuitpython, where
`MICROPY_PY_BUILTINS_MEMORYVIEW_ITEMSIZE` is never enabled
2023-04-23 09:41:26 -05:00
Scott Shawcroft 66edcf5d03
Add PicoDVI support
PicoDVI in CP support 640x480 and 800x480 on Feather DVI, Pico and
Pico W. 1 and 2 bit grayscale are full resolution. 8 and 16 bit
color are half resolution.

Memory layout is modified to give the top most 4k of ram to the
second core. Its MPU is used to prevent flash access after startup.

The port saved word is moved to a watchdog scratch register so that
it doesn't get overwritten by other things in RAM.

Right align status bar and scroll area. This normally gives a few
pixels of padding on the left hand side and improves the odds it is
readable in a case. Fixes #7562

Fixes c stack checking. The length was correct but the top was being
set to the current stack pointer instead of the correct top.
Fixes #7643

This makes Bitmap subscr raise IndexError instead of ValueError
when the index arguments are wrong.
2023-04-19 15:14:02 -07:00
Alex Riesen a22136a732 py/makeqstrdefs.py: Fix handling GreenHills C/C++ preprocessor output.
The GreenHills preprocessor produces #line directives without a file name,
which the regular expression used to distiguish between
"# <number> file..." (GCC and similar) and "#line <number> file..."
(Microsoft C and similar) does not match, aborting processing.

Besides, the regular expression was unnecessarily wide, matching lines
containing a "#", followed by any  number of 'l','i','n', and 'e'
characters.

Signed-off-by: Alex Riesen <alexander.riesen@cetitec.com>
2023-04-11 16:41:10 +10:00
Scott Shawcroft 097af804cd
Fix ticks
In #7497 port_background_task was renamed to port_background_tick
but the actual call site wasn't changed. This meant that it was
no longer called!

Rename more functions from task to tick to make it clearer which is
which.
2023-04-07 09:49:51 -07:00
Jeff Epler b011468251
Add synthio.Synthesizer
In contrast to MidiTrack, this can be controlled from Python code,
turning notes on/off as desired.

Not tested on real HW yet, just the acceptance test based on checking
which notes it thinks are held internally.
2023-04-02 20:37:23 -05:00
Jeff Epler 05cf5213e2
Allow disabling each class in keypad
.. for boards like pewpewm4 which need a specific kind. And need
some space.
2023-04-01 19:22:52 -05:00
Jeff Epler 13e17e6dcd
Make synthio debuggable in unix coverage port 2023-04-01 11:46:36 -05:00
Jeff Epler 02f8a45dfa
synthio: allow increasing number of channels
12 channels works well on metro m7
2023-04-01 11:46:27 -05:00
David Lechner 283c1ba07e py/obj: Fix spelling of staticmethod.
Signed-off-by: David Lechner <david@pybricks.com>
2023-03-28 10:02:05 -05:00
hathach 8c1095b268
Merge branch 'main' into add-codespell 2023-03-23 14:09:57 +07:00
hathach a8abe8e705
remove lib from exludes, fix all remaining typos 2023-03-23 13:59:08 +07:00
Scott Shawcroft 67e0a49a1f
Merge pull request #7724 from tannewt/get_perfbench_running
Improve iMX RT performance
2023-03-21 09:00:48 -07:00
MicroDev ccd417cd8b
add esp specific modules to src patterns 2023-03-21 16:01:13 +05:30
Damien George d54208a2ff py/scheduler: Implement VM abort flag and mp_sched_vm_abort().
This is intended to be used by the very outer caller of the VM/runtime.  It
allows setting a top-level NLR handler that can be jumped to directly, in
order to forcefully abort the VM/runtime.

Enable using:

    #define MICROPY_ENABLE_VM_ABORT (1)

Set up the handler at the top level using:

    nlr_buf_t nlr;
    nlr.ret_val = NULL;
    if (nlr_push(&nlr) == 0) {
        nlr_set_abort(&nlr);
        // call into the VM/runtime
        ...
        nlr_pop();
    } else {
        if (nlr.ret_val == NULL) {
            // handle abort
            ...
        } else {
            // handle other exception that propagated to the top level
            ...
        }
    }
    nlr_set_abort(NULL);

Schedule an abort, eg from an interrupt handler, using:

    mp_sched_vm_abort();

Signed-off-by: Damien George <damien@micropython.org>
2023-03-21 18:08:57 +11:00
Damien George 5d4bfce034 py/mpstate: Add mp_thread_is_main_thread() helper macro.
Signed-off-by: Damien George <damien@micropython.org>
2023-03-21 18:01:01 +11:00
hathach fecc1bdedb
fix typos (partial) detected by codepell 2023-03-18 22:17:02 +07:00
David Lechner fa8ebb1390 py/obj: Add MP_NOINLINE to mp_obj_malloc_helper.
As the comment in py/obj.h says:

> Implementing this as a call rather than inline saves 8 bytes per usage.

So in order to get this savings, we need to tell the compiler to never
inline the function.

Signed-off-by: David Lechner <david@pybricks.com>
2023-03-17 13:16:06 -05:00
Jeff Epler 6ff58fd94a
re-enable mkfs for >4GB filesystems whenver FULL_BUILD 2023-03-17 09:37:15 -05:00
Scott Shawcroft 5bb8a7a7c6
Improve iMX RT performance
* Enable dcache for OCRAM where the VM heap lives.
* Add CIRCUITPY_SWO_TRACE for pushing program counters out over the
  SWO pin via the ITM module in the CPU. Exempt some functions from
  instrumentation to reduce traffic and allow inlining.
* Place more functions in ITCM to handle errors using code in RAM-only
  and speed up CP.
* Use SET and CLEAR registers for digitalio. The SDK does read, mask
  and write.
* Switch to 2MiB reserved for CircuitPython code. Up from 1MiB.
* Run USB interrupts during flash erase and write.
* Allow storage writes from CP if the USB drive is disabled.
* Get perf bench tests running on CircuitPython and increase timeouts
  so it works when instrumentation is active.
2023-03-14 12:30:58 -07:00
Dan Halbert b6a7613350
Merge pull request #7470 from microdev1/espnow
Add ESP-NOW Support
2023-03-10 12:21:56 -05:00
Damien George 668a7bd28a py/makeversionhdr.py: Always add micro to version string even if it's 0.
Moving forward, tags in this repository will always have three components.

Signed-off-by: Damien George <damien@micropython.org>
2023-03-10 18:39:14 +11:00
Laurens Valk f3a596db7d py/builtinimport: Fix unix port build with external imports disabled.
Without this, building the unix port variants gives:
ports/unix/main.c:667: undefined reference to `mp_obj_is_package',
when MICROPY_ENABLE_EXTERNAL_IMPORT is 0.

Signed-off-by: Laurens Valk <laurens@pybricks.com>
2023-03-10 13:29:54 +11:00
Damien George 78dc2db2ba py/mpconfig: Provide config option for internal printf printer.
The C-level printf is usually used for internal debugging prints, and a
port/board may want to redirect this somewhere other than stdout.

Signed-off-by: Damien George <damien@micropython.org>
2023-03-10 11:19:15 +11:00
Damien George b3c8ab37ec py/gc: Make gc_dump_info/gc_dump_alloc_table take a printer as argument.
So that callers can redirect the output if needed.

Signed-off-by: Damien George <damien@micropython.org>
2023-03-10 10:58:10 +11:00
Damien George 7c1584aef1 py/compile: Fix scope of assignment expression target in comprehensions.
When := is used in a comprehension the target variable is bound to the
parent scope, so it's either a global or a nonlocal.  Prior to this commit
that was handled by simply using the parent scope's id_info for the
target variable.  That's completely wrong because it uses the slot number
for the parent's Python stack to store the variable, rather than the slot
number for the comprehension.  This will in most cases lead to incorrect
behaviour or memory faults.

This commit fixes the scoping of the target variable by explicitly
declaring it a global or nonlocal, depending on whether the parent is the
global scope or not.  Then the id_info of the comprehension can be used to
access the target variable.  This fixes a lot of cases of using := in a
comprehension.

Code size change for this commit:

       bare-arm:    +0 +0.000%
    minimal x86:    +0 +0.000%
       unix x64:  +152 +0.019% standard
          stm32:   +96 +0.024% PYBV10
         cc3200:   +96 +0.052%
        esp8266:  +196 +0.028% GENERIC
          esp32:  +156 +0.010% GENERIC[incl +8(data)]
         mimxrt:   +96 +0.027% TEENSY40
     renesas-ra:   +88 +0.014% RA6M2_EK
            nrf:   +88 +0.048% pca10040
            rp2:  +104 +0.020% PICO
           samd:   +88 +0.033% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Fixes issue #10895.

Signed-off-by: Damien George <damien@micropython.org>
2023-03-09 12:13:12 +11:00
David Grayson 2bcd88d556 py/makeversionhdr.py: Optionally get git tag and git hash from env vars.
This is handy when you are doing builds outside of the Git repository but
still want to record that information.

Signed-off-by: David Grayson <davidegrayson@gmail.com>
2023-03-02 23:19:30 +11:00
Pepijn de Vos 72e9318325 py/emitnative: Explicitly compare comparison ops in binary_op emitter.
Without this it's possible to get a compiler error about the comparison
always being true, because MP_BINARY_OP_LESS is 0.  And it seems that gcc
optimises these 6 equality comparisons into the same size machine code as
before.
2023-02-27 10:25:39 +11:00
Dan Halbert f9831b3bbc
Merge pull request #7639 from adafruit/8.0.x
Merge 8.0.x up to main
2023-02-24 19:32:09 -05:00
Scott Shawcroft 144aed40e3
Rename flag. Turn on UTF-8 and flag on unix
Also added label portion to the test.
2023-02-24 12:23:59 -08:00
Damien George 2e4dda3c20 py/modmath: Fix two-argument math function domain check.
Prior to this fix, pow(1.5, inf) and pow(0.5, -inf) (among other things)
would incorrectly raise a ValueError, because the result is inf with the
first argument being finite.  This commit fixes this by allowing the result
to be infinite if the first or second (or both) argument is infinite.

This fix doesn't affect the other three math functions that have two
arguments:
- atan2 never returns inf, so always fails isinf(ans)
- copysign returns inf only if the first argument x is inf, so will never
  reach the isinf(y) check
- fmod never returns inf, so always fails isinf(ans)

Signed-off-by: Damien George <damien@micropython.org>
2023-02-24 15:55:12 +11:00
Scott Shawcroft f832386cda
Merge remote-tracking branch 'adafruit/main' into banglejs2 2023-02-23 14:43:38 -08:00
Scott Shawcroft b79661d631
Alphabetize, fix typo and remove incorrect comment 2023-02-23 14:34:54 -08:00
Scott Shawcroft 1a3358d036
Limit FatFs' ability to upper case paths
Only uppercase ASCII letters a-z. This saves ~900 bytes. Previously
written files with other unicode letters will only be accessible
from their upper cased path.
2023-02-23 14:28:58 -08:00
Scott Shawcroft b67c0b7949
Merge pull request #7585 from bill88t/settings-toml-pystack
Settings.toml-configurable pystack
2023-02-22 10:13:31 -08:00
Scott Shawcroft a8b34bd067
Merge pull request #7549 from gamblor21/gif_displayio_support
Animated GIF support
2023-02-21 11:39:29 -08:00
Jeff Epler d249af2a24 don't explicitly register ulab here
.. it is registered by MP_REGISTER_MODULE in ulab.

Closes: #7600
2023-02-17 09:21:50 -06:00
Bill Sideris 5a9264261e
Merge branch 'adafruit:main' into settings-toml-pystack 2023-02-17 00:50:27 +02:00
Bill Sideris a37dad61fb
CIRCUITPY_SETTABLE_PYSTACK 2023-02-16 23:10:30 +02:00
Dan Halbert bbadc00599
Merge pull request #7577 from dhalbert/safemode-py
Implement safemode.py
2023-02-16 14:15:20 -05:00
MicroDev 32d86c3ea3
Merge pull request #7580 from adafruit/8.0.x
Merge 8.0.x up to main
2023-02-15 09:16:51 +05:30
Dan Halbert d46d9d5c21 shorten safe mode messages; improve message printing; fix CIRCUITPY_SAFEMODE_PY=0 2023-02-13 21:29:57 -05:00
Mark fa435468fa
Merge branch 'main' into gif_displayio_support 2023-02-13 18:30:51 -06:00
Dan Halbert d8231f1588 Implement safemode.py 2023-02-13 18:26:38 -05:00
MicroDev 65e9d937ac
use python based msgfmt 2023-02-13 11:22:35 +05:30
gamblor21 0c95e6a08e Moving to gifio module 2023-02-12 11:17:34 -06:00
gamblor21 da9b6fb0e2 Moving library to /lib 2023-02-10 18:31:59 -06:00
Jeff Epler 0d957fe15c
Fix several places where an exception could be chained wrongly
If an exception's chain or context can refer to a pointer from a different
VM, a crash would typically result.

This couldn't turn up on UNIX testing because the VM is never torn
down and rebuilt like it is on hardware.

Because in the 'static' case the GeneratorObject is now fully initialized
whenever it's raised, the initialization can be dropped, which reduces
the flash size slightly.
Closes: #7565
2023-02-10 09:06:47 -06:00
Jim Mussared 9848b0685f py/mkrules.cmake: Force build mpversion.h and frozen_content.c.
This ensures that all builds unconditionally run makeversionhdr.py and
makemanifest.py to generate mpversion.h and frozen_content.c respectively.
This now matches the Makefile behavior, and in particular this fixes the
issue on ESP32 builds that changes in code-to-be-frozen will cause the
build to update. Both these already tools know not to touch their output
if there is no change.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-02-08 12:24:15 +11:00
Gregory Neverov abc5eb30d7 add support for array.extend(iterable) 2023-02-06 15:07:26 -08:00
gamblor21 6a9b7199ec Initial gif proof of concept 2023-02-04 16:44:29 -06:00
MicroDev 14c3b52b8d
Merge branch 'main' into espnow 2023-02-03 01:37:07 +05:30
Jim Mussared 8b27482692 top: Update Python formatting to black "2023 stable style".
See https://black.readthedocs.io/en/stable/the_black_code_style/index.html

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-02-02 12:51:03 +11:00
MicroDev d9d94eacca
run updated pre-commit 2023-02-01 13:38:41 +05:30
Neradoc f0058774d0 add CIRCUITPY_SKIP_SAFE_MODE_WAIT to circuitpy_mpconfig.mk
use #if (also a CIRCUITPY_ULAB)
2023-01-29 00:56:16 +01:00
MicroDev cda5689263
move `esp32_camera` to `espcamera` 2023-01-27 22:05:43 +05:30
Damien George 31139b437a py/mkrules: Support mpy-tool-flags in cmake frozen code generation.
Signed-off-by: Damien George <damien@micropython.org>
2023-01-24 16:55:23 +11:00
MicroDev 3c10dd8b5f
update espnow api 2023-01-23 23:51:10 +05:30
Damien George d387ae3444 py/objint_mpz: Catch and reject @ and @= operating on big integers.
This will also catch / and /= when float support is disabled.

Fixes issue #10544.

Signed-off-by: Damien George <damien@micropython.org>
2023-01-23 13:03:51 +11:00
Alex Riesen abaa4abd2d py: Add parenthesis to default impl of MP_OBJ_TO_PTR, MP_OBJ_FROM_PTR.
Unless MICROPY_OBJ_REPR == MICROPY_OBJ_REPR_D, these macros only work with
values and "->"/"." expressions as their sole argument.  In other words,
the macros are broken with expressions which contain operations of lower
precedence than the cast operator.

Depending on situation, the old code either results in compiler error:

 MP_OBJ_TO_PTR(flag ? o1 : o2) expands into "(void *)flag ? o1 : o2",
 which some compiler configurations will reject (e.g. GCC -Wint-conversion
 -Wint-to-pointer-cast -Werror)

Or in an incorrect address calculation:

 For ptr declared as "uint8_t *" the MP_OBJ_FROM_PTR(ptr + off)
 expands into ((mp_obj_t)ptr) + off, resulting in an obviously
 wrong address.

Signed-off-by: Alex Riesen <alexander.riesen@cetitec.com>
2023-01-20 21:59:49 +11:00
MicroDev 7330c638b9
minimal changes to make espnow work 2023-01-20 14:12:55 +05:30
Jim Mussared fb8792c095 py/lexer: Wrap in parenthesis all f-string arguments passed to format.
This is important for literal tuples, e.g.

    f"{a,b,}, {c}" --> "{}".format((a,b), (c),)

which would otherwise result in either a syntax error or the wrong result.

Fixes issue #9635.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2023-01-20 17:54:32 +11:00
Andrew Leech 5c4153ea37 py/objarray: Raise error on out-of-bound memoryview slice start.
32-bit platforms only support a slice offset start of 24 bit max due to the
limited size of the mp_obj_array_t.free member.  Similarly on 64-bit
platforms the limit is 56 bits.

This commit adds an OverflowError if the user attempts to slice a
memoryview beyond this limit.

Signed-off-by: Damien George <damien@micropython.org>
2023-01-20 16:31:37 +11:00
robert-hh e78e0b7418 py/gc: Increase the address length in gc_dump_alloc_table().
Showing 8 digits instead of 5, supporting devices with more than 1 MByte of
RAM (which is common these days).  The masking was never needed, and the
related commented-out line can go.
2023-01-16 12:09:20 +11:00
Philip Peitsch edc92d18db py/map: Clear value when re-using slot with ordered dictionaries.
To adhere to the contract of mp_map_lookup, namely:

    MP_MAP_LOOKUP_ADD_IF_NOT_FOUND behaviour:
    - returns slot, with key non-null and value=MP_OBJ_NULL if it was added
2023-01-13 14:21:32 +11:00
Dan Halbert a974402542 Improve type validation errors messages, especially for pins 2023-01-10 15:02:55 -05:00
Dan Halbert e7475153c0
Merge pull request #7415 from jepler/fix-exception-chain-self
do not chain exceptions to themselves
2023-01-04 11:39:16 -05:00
Dan Halbert 4d33cf48a9
Merge pull request #7410 from jepler/unique-fat-volid
oofatfs: enable use of random volume IDs
2023-01-04 11:37:32 -05:00
Jeff Epler cc16bd3d53
do not chain exceptions to themselves
cpython actually makes sure the newly chained exception doesn't create
a cycle (even indirectly); see _PyErr_SetObject use of "Floyd's cycle
detection algo". We'll go for the simpler solution of just checking
one level deep until it's clear we need to do more.

Closes: #7414
2023-01-04 08:40:20 -06:00
Jeff Epler 3d66ed27ce
oofatfs: enable use of random volume IDs
This may help address #7409 if the underlying cause is the deterministic
volume ID. However, not all boards have working urandom (samd21
at least does not) so a couple of fallbacks are attempted when it fails.

I verified that on a pico_w, each `storage.erase_filesystem()` gives
a distinct 32-bit volume ID (pico_w's urandom can never fail)
2023-01-03 17:18:29 -06:00
Dan Halbert 03b43b7b3f complete rework for microcontroller.cpu.frequency and wifi.radio MAC addresses 2023-01-02 11:45:58 -05:00
Jeff Epler ef8b297d7f
Avoid null pointer dereference when no kwargs
clang scan-build reports "Access to field 'table' results in a
dereference of a null pointer (loaded from variable 'kw_args')"
2023-01-01 16:56:53 -06:00
Dan Halbert 2f1476874a
Merge pull request #7368 from dhalbert/preserve-nlr_push-regs
Use returns_twice attribute to preserve regs in nlrthumb nlr_push()
2022-12-21 08:40:05 -05:00
Dan Halbert 8087887afd Use returns_twice attribute to preserve regs in nlrthumb nlr_push() 2022-12-20 22:18:43 -05:00
Scott Shawcroft f8ac1d9261
Rework the coproc API
It is now a generic `memorymap` API and an ESP specific `espulp` module.

Fixes #7218. Fixes #3234. Fixes #7300.
2022-12-19 14:08:38 -05:00
Damien George 910f579403 py/emitnative: Initialise locals as Python object type for native code.
In @micropython.native code the types of variables and expressions are
always Python objects, so they can be initialised as such.  This prevents
problems with compiling optimised code like while-loops where a local may
be referenced before it is assigned to.

Signed-off-by: Damien George <damien@micropython.org>
2022-12-16 11:44:10 +11:00
Jim Mussared 9e91764671 py/obj: Remove unused MP_DEFINE_CONST_OBJ_FULL_TYPE macro.
This was previously used for the definition of NIC types, but they have
been updated to use a protocol instead.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-12-15 17:40:36 +11:00
Jeff Epler 5ac622a30d
get rid of nearly-empty getenv.c file 2022-12-13 13:13:01 -06:00
Jeff Epler cc7d550407
Really finish renaming to getenv 2022-12-09 14:28:46 -06:00
Jeff Epler 3459fe322b
Withdraw the _environ module
This existed solely for testing, so expose it a different way during
the unix coverage build

Also turn off os.getenv support on samd21.
2022-12-08 15:33:10 -06:00
Jeff Epler ef2bfdb5db
dotenv becomes settings.toml 2022-12-08 12:44:20 -06:00
Damien George ab0258fb1e py/gc: Fix debug printing of GC layout.
Signed-off-by: Damien George <damien@micropython.org>
2022-12-08 14:36:34 +11:00
Jeff Epler d75ff42297 unix/coverage: Add extra GC coverage test for ATB gap byte.
The assertion that is added here (to gc.c) fails when running this new test
if ALLOC_TABLE_GAP_BYTE is set to 0.

Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
2022-12-08 14:35:08 +11:00
Jeff Epler 9f434dd8de py/gc: Ensure a gap of one byte after the ATB.
Prior to this fix the follow crash occurred.  With a GC layout of:

    GC layout:
      alloc table at 0x3fd80428, length 32001 bytes, 128004 blocks
      finaliser table at 0x3fd88129, length 16001 bytes, 128008 blocks
      pool at 0x3fd8bfc0, length 2048064 bytes, 128004 blocks

Block 128003 is an AT_HEAD and eventually is passed to gc_mark_subtree.
This causes gc_mark_subtree to call ATB_GET_KIND(128004).  When block 1 is
created with a finaliser, the first byte of the finaliser table becomes
0x2, but ATB_GET_KIND(128004) reads these bits as AT_TAIL, and then
gc_mark_subtree references past the end of the heap, which happened to be
past the end of PSRAM on the esp32-s2.

The fix in this commit is to ensure there is a one-byte gap after the ATB
filled permanently with AT_FREE.

Fixes issue #7116.

See also https://github.com/adafruit/circuitpython/issues/5021

Signed-off-by: Jeff Epler <jepler@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
2022-12-08 14:29:58 +11:00
Jeff Epler 84071590b3 py/gc: Avoid valgrind false positives.
When you want to use the valgrind memory analysis tool on MicroPython, you
can arrange to define MICROPY_DEBUG_VALGRIND to enable use of special
valgrind macros.  For now, this only fixes `gc_get_ptr` so that it never
emits the diagnostic "Conditional jump or move depends on uninitialised
value(s)".

Signed-off-by: Jeff Epler <jepler@gmail.com>
2022-12-08 14:29:22 +11:00
Damien George 2283b6d68f py: Pass in address to compiled module instead of returning it.
This change makes it so the compiler and persistent code loader take a
mp_compiled_module_t* as their last argument, instead of returning this
struct.  This eliminates a duplicate context variable for all callers of
these functions (because the context is now stored in the
mp_compiled_module_t by the caller), and also eliminates any confusion
about which context to use after the mp_compile_to_raw_code or
mp_raw_code_load function returns (because there is now only one context,
that stored in mp_compiled_module_t.context).

Reduces code size by 16 bytes on ARM Cortex-based ports.

Signed-off-by: Damien George <damien@micropython.org>
2022-12-08 12:27:23 +11:00
Damien George a2347433b0 py: Remove the word "yet" from exception messages.
These unimplemented features may never be implemented, and having the word
"yet" there takes up space.

Signed-off-by: Damien George <damien@micropython.org>
2022-12-06 13:34:52 +11:00
Laurens Valk 632d43ed44 py/mpconfig: Include micropython module in core features.
This excludes it from the minimal builds.

Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-12-06 13:34:17 +11:00
Laurens Valk f724d90e6c py/modmicropython: Make module optional.
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>
2022-12-06 13:34:17 +11:00
Jeff Epler 7ceca0cbb2
fix display of the 'host not found' message
The conversion of characters like _space_ in qstrs is a bit
ad-hoc. Because "_not_" stands for the logical negation character ¬
the recently added message was displayed incorrectly:
```
>>> socket.getaddrinfo('does.not.exist', 0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
gaierror: (-2, 'Name or service_space¬space_known')
```
I had noticed this, but evidently failed to include the fix in
the problem in #7269.
2022-12-01 19:46:17 -06:00
Dan Halbert 082b0d1aed
Merge pull request #7191 from jepler/fastpixelmap
Add a fast PixelMap-like class
2022-12-01 11:43:00 -05:00
Jeff Epler fb66a6bfe5
Don't use "offset" as an identifier 2022-11-30 14:39:54 -06:00
Jeff Epler e5b83821f8
fr should fit in 8 bits 2022-11-30 14:39:53 -06:00
Jeff Epler d16c9515b7
Compact the characters of certain translations, so they fit in uint8_t
This saves a few hundred bytes on the affected translations, such
as `el` which shrunk from 186152 to 185588 bytes (564 bytes saved).
2022-11-30 14:39:53 -06:00
Jeff Epler 6be0a425c7
Don't run maketranslationdata twice 2022-11-30 14:39:51 -06:00
Jeff Epler 9c11bb2ed9
Check that translations fit in expected character type 2022-11-30 14:39:50 -06:00
Jeff Epler c46e219795
Having an input-only pin is rare, save a string on other ports 2022-11-30 14:39:49 -06:00
Jeff Epler 4158ddfc17
compile out terse mismatch message when not used 2022-11-30 14:39:49 -06:00
Jeff Epler a94663b3c9
use a standard error message 2022-11-30 14:39:49 -06:00
Jeff Epler d39d146352
Merge some messages 2022-11-30 14:39:47 -06:00
Jeff Epler d61fde349d
re-use an error message 2022-11-30 14:39:34 -06:00
Jeff Epler 5fb191b51c
Use a standard validator 2022-11-30 14:39:33 -06:00
Jeff Epler 0e19fbb60f
Use a function to raise ZeroDivisionError, consistent string 2022-11-30 14:39:30 -06:00
Jeff Epler 7df21c9ecf
Combine a message 2022-11-30 12:06:17 -06:00
Jeff Epler f652a898e7
merge two messages 2022-11-30 12:06:17 -06:00
Jeff Epler 2315b62bff
Remove unused static qstrs
These are turned into TRANSLATE() messages now, so the qstr version
would not be used.
2022-11-30 12:06:15 -06:00
Jeff Epler 10d92873c3
Don't generate QSTRs for wrong identifiers
MP_REGISTER_MODULE would use identifiers like
"MODULE_DEF_MP_QSTR___FUTURE__" which would in turn cause
a QSTR to be generated for it. This wasn't desirable, because the
qstr would never be used.

This clears out quite a bit of flash storage on the proxlight trinkey.
2022-11-30 12:06:15 -06:00
Dan Halbert c8390a7918
Merge pull request #7069 from jepler/exception-chain
Implement chained exceptions
2022-11-30 11:13:57 -05:00
David Lechner d5181034f2 py/bc: Fix checking for duplicate **kwargs.
The code was already checking for duplicate kwargs for named parameters but
if `**kwargs` was given as a parameter, it did not check for multiples of
the same argument name.

This fixes the issue by adding an addition test to catch duplicates and
adds a test to exercise the code.

Fixes issue #10083.

Signed-off-by: David Lechner <david@pybricks.com>
2022-11-28 11:23:22 +11:00
Rayane Chatrieux f3e4c505d1 py/objdict: Implement dictionary union (PEP 584).
Implements dictionary union according to PEP 584's specifications, minus
the fact that dictionary entries are not guaranteed to be in insertion
order.  This feature is enabled with MICROPY_CPYTHON_COMPAT.

Includes a new test.

With the assistance of Fangrui Qin <qinf@purdue.edu>

Signed-off-by: Rayane Chatrieux <rayane.chatrieux@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
2022-11-25 12:46:14 +11:00
foamyguy 57d7f7f2ad move to _pixelmap 2022-11-24 09:07:08 -06:00
Dan Halbert 6d022733b3
Merge pull request #7162 from rhooper/boards-list
add boards list to make error message
2022-11-21 23:18:37 -05:00
Jim Mussared 6873a14b61 py/makeversionhdr.py: Allow running outside of repo.
If a CMake-build is run with `make BUILD=/outside/path` then
makeversionheader.py is run with the CWD set to the build directory, which
means the git version lookup will fail and silently fall back to the
mpconfig.h mode (giving the wrong result).

This commit:
 - Uses the location of makeversionheader.py to find the repo path.
 - Allows overriding this path via --repo-path.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-11-22 14:36:21 +11:00
MicroDev c3c1717813
refactor common port specific Makefile code 2022-11-18 23:00:28 +05:30
Jan Willeke 40a3aa709c py/nlrmips: Add native NLR support for MIPS architecture.
This can be tested using ports/minimal and qemu:

    make CC=mips-linux-gnu-gcc-8

Then run with qemu-mips:

    stty raw opost -echo;
    QEMU_LD_PREFIX=/usr/mips-linux-gnu/ qemu-mips build/firmware.elf;
    sleep 1; reset

Signed-off-by: Jan Willeke <willeke@smartmote.de>
2022-11-15 17:09:49 +11:00
Jeff Epler dd443bacb8
Chain exceptions while unwinding 2022-11-13 19:53:23 -06:00
Jeff Epler b6f86e1e73
Recursively print chained exceptions 2022-11-13 19:53:21 -06:00
Jeff Epler f3169246ba
Implement chained exceptions
This adds the __cause__, __context__ and __suppress_context__
members to exception objects and makes e.g., `raise exc from cause`
set them in the same way as standard Python.
2022-11-13 19:52:50 -06:00
Jeff Epler b499275bb5
Don't crash when assigning attributes of the GeneratorExit const singleton 2022-11-13 19:52:49 -06:00
Jeff Epler f5c637dc10
Add adafruit_pixelmap.PixelMap
.. a fast helper for animations. It is similar to and inspired by the
PixelMap helper in Adafruit LED Animation library, but with an extremely
fast 'paste' method for setting a series of pixels. This is a common
operation for many animations, and can give a substantial speed improvement.

It's named `adafruit_pixelmap` so that we can package a compatible version
in pure Python for systems that can't fit it in C in flash, or for
Blinka.

This is a proof of concept and can make a very fast comet animation:
```python
import time
import adafruit_pixelbuf
import adafruti_pixelmap
import board
import neopixel
from supervisor import ticks_ms
from adafruit_led_animation.animation.solid import Solid
from adafruit_led_animation import color

pixel_pin = board.GP0
pixel_num = 96

pixels = neopixel.NeoPixel(pixel_pin, pixel_num, brightness=1, auto_write=False, pixel_order="RGB")

evens = adafruit_pixelmap.PixelMap(pixels, tuple(range(0, pixel_num, 2)))
odd_indices = tuple((i, i+2) for i in range(1, pixel_num, 4))
print(odd_indices)
odds = adafruit_pixelbuf.PixelMap(pixels, odd_indices)
assert len(odds) == len(odd_indices)


comet_length = 16

comet1 = [color.calculate_intensity(color.GREEN, ((1+i) / comet_length) ** 2.4)
        for i in range(comet_length)]
comet2 = [color.calculate_intensity(color.PURPLE, ((1+i) / comet_length) ** 2.4)
        for i in range(comet_length)]

pos1 = 0
pos2 = 96//4

while True:
    evens.paste(comet1, pos1, wrap=True, reverse=False, others=0)
    pos1 = (pos1 + 1) % len(evens)

    odds.paste(comet2, pos2, wrap=True, reverse=True, others=0)
    pos2 = (pos2 - 1) % len(odds)
    pixels.show()

    m = ticks_ms()
    if m % 2000 > 1000:
        time.sleep(.02)
```
2022-11-11 07:54:33 -06:00
Damien George 0698dd72ea py/emitnative: Ensure load_subscr does not clobber existing REG_ARG_2.
Follow up from a similar fix in 426785a19e

Fixes issue #6314.

Signed-off-by: Damien George <damien@micropython.org>
2022-11-11 12:25:32 +11:00
Jim Mussared 2c8dab7ab4 py/objarray: Detect bytearray(str) without an encoding.
This prevents a very subtle bug caused by writing e.g. `bytearray('\xfd')`
which gives you `(0xc3, 0xbd)`.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-11-08 23:09:22 +11:00
Simon Arlott f8b0ae32d3 py/builtinimport: Fix crash handling "weak link" module names.
There are two calls to mp_builtin___import__():
1. ports/unix/main.c:main_() which provides a str in args[0]
2. py/runtime.c:mp_import_name() which provides a qstr in args[0]

The default implementation of mp_builtin___import__() is
mp_builtin___import___default() which has a different implementation based
on MICROPY_ENABLE_EXTERNAL_IMPORT.

If MICROPY_ENABLE_EXTERNAL_IMPORT is disabled then the handling of weak
links assumes that args[0] is a `const char *`, when it is either a str or
qstr object.

Use the existing qstr of the module name instead, and also use a vstr
instead of strcpy() to ensure no overflow occurs.
2022-11-08 23:03:21 +11:00
Jeff Epler 9cdfba2e47
Simplify argument checking to reduce translated strings
Build size on proxlight trinkey m0 en_US:
Before:  2412 (en_US)  820 (ru)
After:   2544 (en_US)  984 (ru)
Savings: +132 (en_US) +164 (ru) bytes available flash
2022-11-07 12:37:11 -06:00
Damien George cd35b8a2a3 extmod/machine_timer: Move stm32's implementation of machine.Timer here.
So it can be reused by other ports.

Signed-off-by: Damien George <damien@micropython.org>
2022-10-27 14:32:43 +11:00
Damien George c138e10fbb py/makeversionhdr: Fall back to py/mpconfig.h instead of docs/conf.py.
Commit 64af916c11 removed the version string
from docs/conf.py.  py/mpconfig.h is a better place to get the version
from, so use that (when there is no git repository).

Signed-off-by: Damien George <damien@micropython.org>
2022-10-27 13:00:48 +11:00
Jim Mussared d75c7e822c py/obj: Add comments explaining the slot index scheme.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-26 00:08:41 +11:00
Jim Mussared 1ba0e8ff96 py/persistentcode: Only emit sub-version if generated code has native.
In order for v1.19.1 to load a .mpy, the formerly-feature-flags which are
now used for the sub-version must be zero.

The sub-version is only used to indicate a native version change, so it
should be zero when emitting bytecode-only .mpy files.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-25 14:57:04 +11:00
Jim Mussared b161abc574 py/obj: Verify floating point type is correct for repr C.
Prevents double-precision floats being enabled on 32-bit architectures
where they will not fit into the mp_obj_t encoding.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-25 14:46:04 +11:00
Jeff Epler 05252c87f4
Don't crash when assigning attributes of the GeneratorExit const singleton 2022-10-22 11:37:34 -05:00
microDev b33a2b45dc
add coproc alarm 2022-10-20 18:38:20 +05:30
microDev 5232e3f6c3
add coproc module 2022-10-20 09:09:44 +05:30
Jeff Epler 068b7c4af8
Use micropython #defines for stream polling operations
We adopted the file "py/ioctl.h" and the ioctl names beginning
with MP_IOCTL_POLL while micropython went with "py/stream.h" and
MP_STREAM_POLL.

Align with upstream.

Closes #6711
2022-10-14 12:15:30 -05:00
Dan Halbert 8825e7f241
Merge pull request #6397 from jepler/extra-memset
Some flash size optimizations related to string0.c (implementation of str/mem functions)
2022-10-13 12:34:59 -04:00
Dan Halbert 7e4b2a09eb
Merge pull request #7000 from MicroDev1/storage-extend
Add Storage Extension Support
2022-10-13 10:05:55 -04:00
David Lechner ab317a0d66 py/misc: Remove use of bitfield from vstr_t.
Since there is only one flag, we don't need to use a bitfield in vstr_t.
Compilers emit extra instructions to access a bitfield, so this should
reduce the binary size a small amount.

Signed-off-by: David Lechner <david@pybricks.com>
2022-10-12 00:33:09 +11:00
Jim Mussared d6d8722558 extmod: Make extmod.mk self-contained.
This makes it so that all a port needs to do is set the relevant variables
and "include extmod.mk" and doesn't need to worry about adding anything to
OBJ, CFLAGS, SRC_QSTR, etc.

Make all extmod variables (src, flags, etc) private to extmod.mk.

Also move common/shared, extmod-related fragments (e.g. wiznet, cyw43,
bluetooth) into extmod.mk.

Now that SRC_MOD, CFLAGS_MOD, CXXFLAGS_MOD are unused by both extmod.mk
(and user-C-modules in a previous commit), remove all uses of them from
port makefiles.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-11 23:31:49 +11:00
Jim Mussared 3cc6decfc4 py/py.mk: Make user-C-module handling self-contained in py.mk.
Removes the need for the port to add anything to OBJS or SRC_QSTR.

Also makes it possible for user-C-modules to differentiate between code
that should be processed for QSTR vs other files (e.g. helpers and
libraries).

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-11 23:23:18 +11:00
Jim Mussared 0e8dfaf538 py/modsys: Add support for sys.executable.
Only intended to be used on Unix and other "OS" ports.  Matches CPython.
This should give the absolute path to the executing binary.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
2022-10-11 18:10:26 +11:00
Jim Mussared c44b3927b8 py/objstr: Add a helper to set mp_obj_str_t data.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-10-11 17:50:19 +11:00
Dan Halbert 86a0f9a861 save about 112 bytes 2022-10-09 19:22:39 -04:00
Jeff Epler dcb650c513 pico w: add ssl module
Note: at this time, the ssl module on pico_w never verifies the server
certificate. This means it does not actually provide a higher security
level than regular socket / http protocols.
2022-10-05 13:12:43 -04:00
Dan Halbert ed87579a65 add missing native modules to support matrix 2022-10-04 19:22:18 -04:00
microDev 9fe7308b88
conditionally add storage extension 2022-10-04 17:26:28 +05:30
stijn fecfbc3f67 py/mkenv.mk: Make CPP definition explicit for consistency. 2022-09-29 14:17:37 +02:00
Dan Halbert db065a299f
Merge pull request #6933 from jepler/🥧🐮
Implement a useful subset of `wifi` and `socketpool` modules on 🥧🐮
2022-09-28 18:09:24 -04:00
Jeff Epler 6c3cdceb45
Implement scan, connect, ping
My pings go out, and then they come back

```py
import os
import wifi
import ipaddress

wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD'))
ipv4 = ipaddress.ip_address("8.8.4.4")
print("Ping google.com: %f ms" % (wifi.radio.ping(ipv4)*1000))
```
2022-09-28 10:06:33 -05:00
Jeff Epler 346fff2e7c
cyw43 basic gpio support, hwaddr in boot_out 2022-09-28 10:06:33 -05:00
Matt Trentini 0bc1d10557 rp2/Makefile: Add support for BOARD_VARIANTS.
Following stm32.  This allows a single board definition to define variants
of its configuration.
2022-09-26 12:38:58 +10:00
Dan Halbert a7b10d41b4
Merge pull request #6522 from jepler/must-be-int
Improve argument checking & reduce strings to translate
2022-09-25 17:55:56 -04:00
Dan Halbert de80db681f
Merge pull request #6915 from dhalbert/ringbuf-cleanup
ringbuf cleanup
2022-09-25 17:50:21 -04:00
Jim Mussared 9d6f474ea4 py/objstr: Don't treat bytes as unicode in str.count.
`b'\xaa \xaa'.count(b'\xaa')` now (correctly) returns 2 instead of 1.

Fixes issue #9404.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-26 00:54:18 +10:00
Angus Gratton 25ff5b52d9 py/parse: Allow const types other than int to optimise as true/false.
Allows optimisation of cases like:

    import micropython
    _DEBUG = micropython.const(False)
    if _DEBUG:
        print('Debugging info')

Previously the 'if' statement was only optimised out if the type of the
const() argument was integer.

The change is implemented in a way that makes the compiler slightly smaller
(-16 bytes on PYBV11) but compilation will also be very slightly slower.

As a bonus, if const support is enabled then the compiler can now optimise
const truthy/falsey expressions of other types, like:

    while "something":
        pass

... unclear if that is useful, but perhaps it could be.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2022-09-23 16:04:13 +10:00
Jeff Epler 18cb25e95c
Re-use another message 2022-09-22 08:39:34 -05:00
Dan Halbert ea15a9118a ringbuf cleanup 2022-09-21 10:03:05 -04:00
Dan Halbert c567b43441 add CIRCUITPY_USB_IDENTIFICATION to turn off on smallest builds 2022-09-20 14:32:38 -04:00
Damien George fb77be1506 py: Include filename in errors from loading/saving files via "open".
This improves error messages in mpy-cross:
- When loading a .py file that doesn't exist (or can't be opened) it now
  includes the filename in the OSError.
- When saving a .mpy file that can't be opened it now raises an exception
  (prior, it would silently fail), and includes the filename in the
  OSError.

Signed-off-by: Damien George <damien@micropython.org>
2022-09-19 23:27:08 +10:00
Damien George cc588ac3a9 py/runtime: Add mp_raise_OSError_with_filename helper function.
Useful when more detail is needed for an OSError associated with a file.

Signed-off-by: Damien George <damien@micropython.org>
2022-09-19 23:25:26 +10:00
Jim Mussared 15d0615d5c py/objmodule: Add support for __dict__.
This matches class `__dict__`, and is similarly gated on
MICROPY_CPYTHON_COMPAT. Unlike class though, because modules's globals are
actually dict instances, the result is a mutable dictionary.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 23:22:46 +10:00
Jim Mussared d94141e147 py/persistentcode: Introduce .mpy sub-version.
The intent is to allow us to make breaking changes to the native ABI (e.g.
changes to dynruntime.h) without needing the bytecode version to increment.

With this commit the two bits previously used for the feature flags (but
now unused as of .mpy version 6) encode a sub-version.  A bytecode-only
.mpy file can be loaded as long as MPY_VERSION matches, but a native .mpy
(i.e. one with an arch set) must also match MPY_SUB_VERSION.  This allows 3
additional updates to the native ABI per bytecode revision.

The sub-version is set to 1 because the previous commits that changed the
layout of mp_obj_type_t have changed the native ABI.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Signed-off-by: Damien George <damien@micropython.org>
2022-09-19 23:19:55 +10:00
Jim Mussared b41aaaa8a9 py/obj: Optimise code size and performance for make_new as a slot.
The check for make_new (i.e. used to determine something's type) is now
more complicated due to the slot access.  This commit changes the inlining
of a few frequently-used helpers to overall improve code size and
performance.
2022-09-19 19:06:16 +10:00
Jim Mussared 94beeabd2e py/obj: Convert make_new into a mp_obj_type_t slot.
Instead of being an explicit field, it's now a slot like all the other
methods.

This is a marginal code size improvement because most types have a make_new
(100/138 on PYBV11), however it improves consistency in how types are
declared, removing the special case for make_new.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:15 +10:00
Jim Mussared 6da41b5900 py/obj: Merge getiter and iternext mp_obj_type_t slots.
The goal here is to remove a slot (making way to turn make_new into a slot)
as well as reduce code size by the ~40 references to mp_identity_getiter
and mp_stream_unbuffered_iter.

This introduces two new type flags:
- MP_TYPE_FLAG_ITER_IS_ITERNEXT: This means that the "iter" slot in the
  type is "iternext", and should use the identity getiter.
- MP_TYPE_FLAG_ITER_IS_CUSTOM: This means that the "iter" slot is a pointer
  to a mp_getiter_iternext_custom_t instance, which then defines both
  getiter and iternext.

And a third flag that is the OR of both, MP_TYPE_FLAG_ITER_IS_STREAM: This
means that the type should use the identity getiter, and
mp_stream_unbuffered_iter as iternext.

Finally, MP_TYPE_FLAG_ITER_IS_GETITER is defined as a no-op flag to give
the default case where "iter" is "getiter".

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:13 +10:00
Jim Mussared 3c6127dfcf py/objnamedtuple: Optimise slot RAM usage for namedtuple.
Rather than reserving a full 12-slot mp_obj_type_t, reserve enough room for
seven and cast as necessary.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:13 +10:00
Jim Mussared 165388e4eb py/objtype: Optimise slot RAM usage for instance types.
In all cases other than where you have a native base with a protocol, it
now fits into 4 GC blocks (like it did before the slots representation).

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:13 +10:00
Jim Mussared cb0ffdd2bf py/obj: Remove basic mp_obj_type_t sparse representation.
This makes the slots-based representation the only option.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:12 +10:00
Jim Mussared 3ac8b5851e py/obj: Add slot-index mp_obj_type_t representation.
The existings mp_obj_type_t uses a sparse representation for slots for the
capability methods of the type (eg print, make_new).  This commit adds a
compact slot-index representation.  The basic idea is that where the
mp_obj_type_t struct used to have 12 pointer fields, it now has 12 uint8_t
indices, and a variable-length array of pointers.  So in the best case (no
fields used) it saves 12x4-12=36 bytes (on a 32-bit machine) and in the
common case (three fields used) it saves 9x4-12=24 bytes.

Overall with all associated changes, this slot-index representation reduces
code size by 1000 to 3000 bytes on bare-metal ports.  Performance is
marginally better on a few tests (eg about 1% better on misc_pystone.py and
misc_raytrace.py on PYBv1.1), but overall marginally worse by a percent or
so.

See issue #7542 for further analysis and discussion.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:09 +10:00
Jim Mussared a52cd5b07d py/obj: Add accessors for type slots and use everywhere.
This is a no-op, but sets the stage for changing the mp_obj_type_t
representation.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:07 +10:00
Jim Mussared e8355eb163 py/obj: Add "full" and "empty" non-variable-length mp_obj_type_t.
This will always have the maximum/minimum size of a mp_obj_type_t
representation and can be used as a member in other structs.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:04 +10:00
Jim Mussared 5ddf671944 py/objexcept: Make MP_DEFINE_EXCEPTION use MP_DEFINE_CONST_OBJ_TYPE.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:01 +10:00
Jim Mussared 9dce82776d all: Remove unnecessary locals_dict cast.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:01 +10:00
Jim Mussared b7d6ee9b75 all: Fix #if inside MP_DEFINE_CONST_OBJ_TYPE for msvc.
Changes:

    MP_DEFINE_CONST_OBJ_TYPE(
       ...
       #if FOO
       ...
       #endif
       ...
    );

to:

    MP_DEFINE_CONST_OBJ_TYPE(
       ...
       FOO_TYPE_ATTR
       ...
    );

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:01 +10:00
Jim Mussared 662b9761b3 all: Make all mp_obj_type_t defs use MP_DEFINE_CONST_OBJ_TYPE.
In preparation for upcoming rework of mp_obj_type_t layout.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:06:01 +10:00
Jim Mussared cdb880789f py/obj: Add macro to declare ROM mp_obj_type_t instances.
This will allow the structure of mp_obj_type_t to change while keeping the
definition code the same.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 19:05:50 +10:00
Jim Mussared fb2a57800a all: Simplify buffer protocol to just a "get buffer" callback.
The buffer protocol type only has a single member, and this existing layout
creates problems for the upcoming split/slot-index mp_obj_type_t layout
optimisations.

If we need to make the buffer protocol more sophisticated in the future
either we can rely on the mp_obj_type_t optimisations to just add
additional slots to mp_obj_type_t or re-visit the buffer protocol then.

This change is a no-op in terms of generated code.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-19 18:40:39 +10:00
Jeff Epler f11ef4cc4e
makeversionhdr should be unforgiving
Recently(?) github started making it the default to only copy a single
branch (e.g., main) and NO TAGS into new forks.

This makes the step of the build process that determines the CircuitPython
version not work, because tags are expected to be present. When tags are
not present, the version number is only a git hash. The version number
ends up being 0.0.0.

This causes problems with libraries that check for CircuitPython version
to determine compatibility, among other things.

We'll do other things to improve the situation, such as document it.
But it'd also be good if the build stopped when this detectable condition
occurs.
2022-09-16 10:28:53 -05:00
Dan Halbert 6bb47038d5 Merge remote-tracking branch 'adafruit/main' into adcdma 2022-09-15 20:42:51 -04:00
Jim Mussared 45972fa548 py/mkrules.mk: Add link to build troubleshooting on failure.
Also update the submodules help text to match.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-15 12:22:18 +10:00
Damien George 74805435f9 py/objpolyiter: Fix comment about finaliser method.
Signed-off-by: Damien George <damien@micropython.org>
2022-09-13 21:14:22 +10:00
Jim Mussared 3e5b1be8ca py/mpconfig: Add "everything" features from unix coverage.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-13 17:40:33 +10:00
Jim Mussared 605266ee9a py/mpconfig: Make feature levels available to mpconfigport.h.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-13 17:37:49 +10:00
Jim Mussared 89a0fefb6c py/mpconfig: Add LFS1/LFS2 options to match FAT/posix.
Also fixes the #ifndef for FAT & posix.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-09-13 17:37:32 +10:00
Andrew Leech 582b3e4e78 py/objpolyiter: Add a new polyiter type with finaliser support. 2022-09-13 13:00:25 +10:00
Jeff Epler 56112e00a1 Give fontio its own enable
.. the default is intended to be the equivalent of the original,
implementing `DISPLAYIO && TERMINALIO`.

This is a possible alternative to #6889, if I understand the intent.
2022-09-11 08:09:14 -05:00
Lee Atkinson f279a2dbb2
Merge branch 'adafruit:main' into adcdma 2022-09-08 11:46:01 -04:00
latkinso42 a1856ea3e9 Renaming module from adcbuffer to analogbufio 2022-09-05 12:11:49 -04:00
Dan Halbert 52080e24eb status bar control 2022-08-30 15:23:44 -04:00
Andrew Leech d521899e18 py/persistentcode: Clarify ValueError when native emitter disabled. 2022-08-29 12:38:49 +10:00
Jim Mussared 6c3d8d38bf py/objstr: Always validate utf-8 for mp_obj_new_str.
All uses of this are either tiny strings or not-known-to-be-safe.

Update comments for mp_obj_new_str_copy and mp_obj_new_str_of_type.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-26 16:45:46 +10:00
Jim Mussared 3a910b1565 py/objstr: Optimise mp_obj_new_str_from_vstr for known-safe strings.
The new `mp_obj_new_str_from_utf8_vstr` can be used when you know you
already have a unicode-safe string.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-26 16:44:35 +10:00
Jim Mussared 88864587f5 py/objstr: Always ensure mp_obj_str_from_vstr is unicode-safe.
Now that we have `mp_obj_new_str_type_from_vstr` (private helper used by
objstr.c) split from the public API (`mp_obj_new_str_from_vstr`), we can
enforce a unicode check at the public API without incurring a performance
cost on the various objstr.c methods (which are already working on known
unicode-safe strings).

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-26 16:44:20 +10:00
Jim Mussared 8a0ee5a5c0 py/objstr: Split mp_obj_str_from_vstr into bytes/str versions.
Previously the desired output type was specified.  Now make the type part
of the function name.  Because this function is used in a few places this
saves code size due to smaller call-site.

This makes `mp_obj_new_str_type_from_vstr` a private function of objstr.c
(which is almost the only place where the output type isn't a compile-time
constant).

This saves ~140 bytes on PYBV11.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-26 16:43:55 +10:00
Lee Atkinson 6fd08483e2 Tidying code for PR/ Minor Issues 2022-08-24 17:41:51 -04:00
Laurens Valk d8ad87843a py/builtinimport: Allow overriding of mp_builtin___import__.
This allows ports to override mp_builtin___import__.

This can be useful in MicroPython applications where
MICROPY_ENABLE_EXTERNAL_IMPORT has to be disabled due to its impact on
build size (2% to 2.5% of the minimal port). By overriding the otherwise
very minimal mp_builtin___import__, ports can still allow limited forms
of application-specific imports.

Signed-off-by: Laurens Valk <laurens@pybricks.com>
2022-08-23 13:34:06 +10:00
Damien George 3d65101a8a py: Clean up formatting of union definitions.
Signed-off-by: Damien George <damien@micropython.org>
2022-08-23 13:09:57 +10:00
Lee Atkinson f5655dd48c Fixing Build errors/ DMA Xfer Rate 2022-08-21 11:44:40 -04:00
Lee Atkinson 61591ac136 adding __init.c to circuitpy_defs.mk 2022-08-19 13:17:37 -04:00
Jim Mussared af1f167820 py/dynruntime: Add mp_obj_is_true.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-19 23:31:11 +10:00
Lee Atkinson d3c3c9eac4 Tidy a few things 2022-08-18 19:10:16 -04:00
Lee Atkinson f91af513b7 Introduced odule adcbuffer / removed analogio/AnalogFastIn 2022-08-18 16:23:17 -04:00
Lee Atkinson 34b8fbaf14
Merge branch 'adafruit:main' into adcdma 2022-08-18 09:08:10 -04:00
Damien George 8f4c108025 all: Remove MICROPY_PY_IO_FILEIO config option.
Since commit e65d1e69e8 there is no longer an
io.FileIO class, so this option is no longer needed.

This option also controlled whether or not files supported being opened in
binary mode (eg 'rb'), and could, if disabled, lead to confusion as to why
opening a file in binary mode silently did the wrong thing (it would just
open in text mode if MICROPY_PY_IO_FILEIO was disabled).

The various VFS implementations (POSIX, FAT, LFS) were the only places
where enabling this option made a difference, and in almost all cases where
one of these filesystems were enabled, MICROPY_PY_IO_FILEIO was also
enabled.  So it makes sense to just unconditionally enable this feature
(ability to open a file in binary mode) in all cases, and so just remove
this config option altogether.  That makes configuration simpler and means
binary file support always exists (and opening a file in binary mode is
arguably more fundamental than opening in text mode, so if anything should
be configurable then it should be the ability to open in text mode).

Signed-off-by: Damien George <damien@micropython.org>
2022-08-18 11:54:17 +10:00
Scott Shawcroft 7717ab8e13
Merge pull request #6754 from jepler/check-read-utf8
When reading data from a file into a str, check if it's utf-8
2022-08-15 13:48:43 -07:00
Lee Atkinson c53c1c0927 Adding py/circuitpy_mpconfig.mk 2022-08-14 18:49:59 -04:00
Lee Atkinson b2c6bcdf6d Fixing py/circuitpy_defns.mk 2022-08-14 14:22:22 -04:00
Lee Atkinson 4542c801b0 Tidying up loose endson draft PR 2022-08-14 13:03:33 -04:00
Dan Ellis 6f4d424f46 py/formatfloat: Use pow(10, e) instead of pos/neg_pow lookup tables.
Rework the conversion of floats to decimal strings so it aligns precisely
with the conversion of strings to floats in parsenum.c.  This is to avoid
rendering 1eX as 9.99999eX-1 etc.  This is achieved by removing the power-
of-10 tables and using pow() to compute the exponent directly, and that's
done efficiently by first estimating the power-of-10 exponent from the
power-of-2 exponent in the floating-point representation.

Code size is reduced by roughly 100 to 200 bytes by this commit.

Signed-off-by: Dan Ellis <dan.ellis@gmail.com>
2022-08-12 23:53:34 +10:00
Dan Ellis 6cd2e41918 py/parsenum: Ensure that trailing zeros lead to identical results.
Prior to this commit, parsenum would calculate "1e-20" as 1.0*pow(10, -20),
and "1.000e-20" as 1000.0*pow(10, -23); in certain cases, this could make
seemingly-identical values compare as not equal.  This commit watches for
trailing zeros as a special case, and ignores them when appropriate, so
"1.000e-20" is also calculated as 1.0*pow(10, -20).

Fixes issue #5831.
2022-08-12 23:44:11 +10:00
Jeff Epler 06f22cb59c
When reading data from a file into a str, check if it's utf-8
Otherwise, weird stuff can happen down the line when it is print()ed,
especially as it can break the webrepl of circuitpython.
2022-08-12 08:25:27 -05:00
Damien George cf90e24335 py/mkrules: Use abspath to find directory for mpy-cross dependency.
Otherwise if the `mpy-cross/build/` directory doesn't exist then
`mpy-cross/build/..` won't work.

Signed-off-by: Damien George <damien@micropython.org>
2022-08-12 16:38:24 +10:00
Damien George 945f377b43 py/objstr: Remove str function object declarations from header file.
Since f7f56d4285 consolidated all uses of
these to a single locals dict, they no longer need to be made public.

Signed-off-by: Damien George <damien@micropython.org>
2022-08-12 16:38:22 +10:00
Jim Mussared 28aaab9590 py/objstr: Add hex/fromhex to bytes/memoryview/bytearray.
These were added in Python 3.5.

Enabled via MICROPY_PY_BUILTINS_BYTES_HEX, and enabled by default for all
ports that currently have ubinascii.

Rework ubinascii to use the implementation of these methods.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-12 12:44:30 +10:00
Andrew Leech f7f56d4285 py/objstr: Consolidate methods for str/bytes/bytearray/array.
This commit adds the bytes methods to bytearray, matching CPython.  The
existing implementations of these methods for str/bytes are reused for
bytearray with minor updates to match CPython return types.

For details on the CPython behaviour see
https://docs.python.org/3/library/stdtypes.html#bytes-and-bytearray-operations

The work to merge locals tables for str/bytes/bytearray/array was done by
@jimmo.  Because of this merging of locals the change in code size for this
commit is mostly negative:

       bare-arm:    +0 +0.000%
    minimal x86:   +29 +0.018%
       unix x64:  -792 -0.128% standard[incl -448(data)]
    unix nanbox:  -436 -0.078% nanbox[incl -448(data)]
          stm32:   -40 -0.010% PYBV10
         cc3200:   -32 -0.017%
        esp8266:   -28 -0.004% GENERIC
          esp32:   -72 -0.005% GENERIC[incl -200(data)]
         mimxrt:   -40 -0.011% TEENSY40
     renesas-ra:   -40 -0.006% RA6M2_EK
            nrf:   -16 -0.009% pca10040
            rp2:   -64 -0.013% PICO
           samd:  +148 +0.105% ADAFRUIT_ITSYBITSY_M4_EXPRESS
2022-08-11 23:18:02 +10:00
Damien George 82b3500724 py/qstr: Change qstr hash type from mp_uint_t to size_t.
The hash is either 8 or 16 bits (depending on MICROPY_QSTR_BYTES_IN_HASH)
so will fit in a size_t.

This saves 268 bytes on the unix nanbox build.  Non-nanbox configurations
are unchanged because mp_uint_t is the same size as size_t.

Signed-off-by: Damien George <damien@micropython.org>
2022-08-11 23:18:02 +10:00
Efi Weiss f3285fef07 py/nlrpowerpc: Fix generation of ppc64 code on ppc32 build.
Due to inline assembly, wrong instructions were generated.  Use
corresponding 32 bit instructions and fix the offsets used.

Signed-off-by: Efi Weiss <efiwiss@gmail.com>
2022-08-11 14:04:13 +10:00
Mat Booth 2e8816de91 py/dynruntime.mk: Allow building assembly source in natmods.
Allow inclusion of assembly source files in dynamic native modules.
2022-08-11 14:00:13 +10:00
Daniel Jour 47c84286e8 all: Fix paths to mpy-cross and micropython binaries.
Binaries built using the Make build system now no longer appear in the
working directory of the build, but rather in the build directory.  Thus
some paths had to be adjusted.
2022-08-11 13:31:13 +10:00
Daniel Jour b2e8240268 py/mkrules.mk: Keep all build artefacts inside $(BUILD) directory.
The rules for lib (static library with name $(LIBMICROPYTHON)) and the
default rule to build a binary (name $(PROG)) produced outputs in the
current working directory.  Change this to build these files in the build
directory.

Note: An empty BUILD variable can cause issues (references to the root
directory); this is not addressed by this commit due to multiple other
places having the same issue.
2022-08-11 13:29:44 +10:00
Lee Atkinson 5fb5fd0558
Merge branch 'adafruit:main' into adcdma 2022-08-10 18:33:52 -04:00
Scott Shawcroft 741a5c2bec
Merge pull request #6722 from dhalbert/micropython-float-print-fix
py/formatfloat: Format all whole-number floats exactly.
2022-08-10 09:32:28 -07:00
Lee Atkinson e778112710
Merge branch 'adafruit:main' into adcdma 2022-08-10 10:29:17 -04:00
Lee Atkinson 9e0c580d3d AnalogFastIn 2022-08-10 09:42:24 -04:00
Damien George b5986784e4 py/objstr: Reformat str access macros to make them readable.
Signed-off-by: Damien George <damien@micropython.org>
2022-08-10 14:31:06 +10:00
Damien George 7d91a9bf5b py/mpprint: Fix formatting typo with mp_print_ext_t struct name.
Signed-off-by: Damien George <damien@micropython.org>
2022-08-10 14:30:47 +10:00
Scott Shawcroft 554063a817
Merge branch 'main' into espressif-camera-2 2022-08-09 14:07:14 -07:00
Dan Ellis 9b5e00fcc5 py/formatfloat: Format all whole-number floats exactly.
Formerly, py/formatfloat would print whole numbers inaccurately with
nonzero digits beyond the decimal place.  This resulted from its strategy
of successive scaling of the argument by 0.1 which cannot be exactly
represented in floating point.  The change in this commit avoids scaling
until the value is smaller than 1, so all whole numbers print with zero
fractional part.

Fixes issue #4212.

Signed-off-by: Dan Ellis dan.ellis@gmail.com
2022-08-09 13:43:47 -04:00
Dan Halbert 84807cd6eb Change I2C terminology from "peripheral" to "target" 2022-08-09 13:13:19 -04:00
David Lechner 6baeded322 py/runtime: Fix crash in star arg unpacking.
The reallocation trigger for unpacking star args with unknown length
did not take into account the number of fixed args remaining. So it was
possible that the unpacked iterators could take up exactly the memory
allocated then nothing would be left for fixed args after the star args.
This causes a segfault crash.

This is fixed by taking into account the remaining number of fixed args
in the check to decide whether to realloc yet or not.

Signed-off-by: David Lechner <david@pybricks.com>
2022-08-06 11:32:58 -05:00
Jeff Epler 1a11ff864a
Merge remote-tracking branch 'origin/main' into espressif-camera-2 2022-08-05 21:08:12 -05:00
Scott Shawcroft 207311b02a
Merge branch 'main' into title_execution_status 2022-08-05 13:42:58 -07:00
Scott Shawcroft 3a2bcbc5c7
Enable title bar on all builds
* Tweak scroll area position so last line is complete and top is
  under the title bar.
* Pick Blinka size based on the font to minimize unused space in
  title bar. Related to #2791
* Update the title bar after terminal is started. Fixes #6078

Fixes #6668
2022-08-04 16:33:10 -07:00
Jeff Epler 5db6db0128
add esp32-camera
This uses the esp32-camera code instead of our own homebrewed camera code.
In theory it supports esp32, esp32-s2 and esp32-s3, as long as they have
PSRAM.

This is very basic and doesn't support changing any camera parameters,
including switching resolution or pixelformat.

This is tested on the Kaluga (ESP32-S2) and ESP32-S3-Eye boards.

First, reserve some PSRAM by putting this line in `CIRCUITPY/_env`:
```
CIRCUITPY_RESERVED_PSRAM=524288
```
and hard-reset the board for it to take effect.

Now, the following script will take a very low-resolution jpeg file and print
it in the REPL in escape coded form:

```python
import board
import esp32_camera

c = esp32_camera.Camera(
    data_pins=board.CAMERA_DATA,
    external_clock_pin=board.CAMERA_XCLK,
    pixel_clock_pin=board.CAMERA_PCLK,
    vsync_pin=board.CAMERA_VSYNC,
    href_pin=board.CAMERA_HREF,
    pixel_format=esp32_camera.PixelFormat.JPEG,
    i2c=board.I2C(),
    external_clock_frequency=20_000_000)

m = c.take()
if m is not None:
    print(bytes(m))
```

Then on desktop open a python repl and run something like
```python
>>> with open("my.jpg", "wb") as f: f.write(<BIG PASTE FROM REPL>)
```
and open my.jpg in a viewer.
2022-08-04 15:11:50 -05:00
Jeff Epler 82be75adb5 Add ability to reserve psram
.. the primary user of which will be the camera, since the framebuffers
must be allocated via esp-idf allocation function and never from the
gc heap.

A board can have a default value, and the value can also be set in the
/.env file using the key CIRCUITPY_RESERVED_PSRAM with the value being
the reserved size in bytes.

Co-authored-by: Dan Halbert <halbert@adafruit.com>
2022-08-03 16:19:40 -05:00
Jim Mussared 579f330508 py/mkenv.mk: Use micropython-lib from submodule by default.
Also adds micropython-lib to 'make submodules' when using a frozen manifest
(for make and cmake).

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-08-03 00:08:41 +10:00
Angus Gratton 1230d86dca py/builtinimport: Remove duplicate static function argument.
context==mc in all cases where this function was being called.

Signed-off-by: Angus Gratton <angus@redyak.com.au>
2022-07-27 11:38:47 +10:00
Dan Ellis f9cbe6bc47 py/formatfloat: Format all whole-number floats exactly.
Formerly, py/formatfloat would print whole numbers inaccurately with
nonzero digits beyond the decimal place.  This resulted from its strategy
of successive scaling of the argument by 0.1 which cannot be exactly
represented in floating point.  The change in this commit avoids scaling
until the value is smaller than 1, so all whole numbers print with zero
fractional part.

Fixes issue #4212.

Signed-off-by: Dan Ellis dan.ellis@gmail.com
2022-07-26 22:23:47 +10:00
Jim Mussared e65d1e69e8 py/modio: Remove FileIO and TextIOWrapper from io module.
On ports with more than one filesystem, the type will be wrong, for example
if using LFS but FAT enabled, then the type will be FAT.  So it's not
possible to use these classes to identify a file object type.

Furthermore, constructing an io.FileIO currently crashes on FAT, and
make_new isn't supported on LFS.

And the io.TextIOWrapper class does not match CPython at all.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
2022-07-26 17:58:01 +10:00
Damien George c0fa903d6b py/compile: Support large integers in inline-asm data directive.
Fixes issue #8956.

Signed-off-by: Damien George <damien@micropython.org>
2022-07-26 12:24:50 +10:00
Jeff Epler 162fa6ef02
Merge remote-tracking branch 'origin/main' into extra-memset 2022-07-25 06:51:59 -05:00
Damien George 4fe3e493b1 py/obj: Make mp_obj_get_complex_maybe call mp_obj_get_float_maybe first.
This commit simplifies mp_obj_get_complex_maybe() by first calling
mp_obj_get_float_maybe() to handle the cases corresponding to floats.
Only if that fails does it attempt to extra a full complex number.

This reduces code size and also means that mp_obj_get_complex_maybe() now
supports user-defined classes defining __float__; in particular this allows
user-defined classes to be used as arguments to cmath-module function.

Furthermore, complex_make_new() can now be simplified to directly call
mp_obj_get_complex(), instead of mp_obj_get_complex_maybe() followed by
mp_obj_get_float().  This also improves error messages from complex with
an invalid argument, it now raises "can't convert <type> to complex" rather
than "can't convert <type> to float".

Signed-off-by: Damien George <damien@micropython.org>
2022-07-25 16:11:26 +10:00
Andrew Leech 1e87b56219 py/obj: Add support for __float__ and __complex__ functions. 2022-07-25 14:23:34 +10:00
Rob Knegjens 4a48531803 py/gc: Reduce code size when MICROPY_GC_SPLIT_HEAP is disabled.
Use C macros to reduce the size of firmware images when the GC split-heap
feature is disabled.

The code size difference of this commit versus HEAD~2 (ie the commit prior
to MICROPY_GC_SPLIT_HEAP being introduced) when split-heap is disabled is:

       bare-arm:    +0 +0.000%
    minimal x86:    +0 +0.000%
       unix x64:   -16 -0.003%
    unix nanbox:   -20 -0.004%
          stm32:    -8 -0.002% PYBV10
         cc3200:    +0 +0.000%
        esp8266:    +8 +0.001% GENERIC
          esp32:    +0 +0.000% GENERIC
            nrf:   -20 -0.011% pca10040
            rp2:    +0 +0.000% PICO
           samd:    -4 -0.003% ADAFRUIT_ITSYBITSY_M4_EXPRESS

The code size difference of this commit versus HEAD~2 split-heap is enabled
with MICROPY_GC_MULTIHEAP=1 (but no extra code to add more heaps):

    unix x64: +1032 +0.197% [incl +544(bss)]
       esp32:  +592 +0.039% GENERIC[incl +16(data) +264(bss)]
2022-07-23 00:43:08 +10:00
Ayke van Laethem bcc827d695 py/gc: Allow the GC heap to be split over multiple memory areas.
This commit adds a new option MICROPY_GC_SPLIT_HEAP (disabled by default)
which, when enabled, allows the GC heap to be split over multiple memory
areas/regions.  The first area is added with gc_init() and subsequent areas
can be added with gc_add().  New areas can be added at runtime.  Areas are
stored internally as a linked list, and calls to gc_alloc() can be
satisfied from any area.

This feature has the following use-cases (among others):
- The ESP32 has a fragmented OS heap, so to use all (or more) of it the
  GC heap must be split.
- Other MCUs may have disjoint RAM regions and are now able to use them
  all for the GC heap.
- The user could explicitly increase the size of the GC heap.
- Support a dynamic heap while running on an OS, adding more heap when
  necessary.
2022-07-23 00:42:54 +10:00
Dan Halbert bb47484cb1 Merge remote-tracking branch 'adafruit/main' into from_bytes-check-parameters 2022-07-19 17:18:26 -04:00
stijn e82aa2abc4 py/qstr: Make mp_decompress_rom_string decl and def the same.
Fixes MSVC warning about mismatching argument types.
2022-07-18 23:27:28 +10:00
stijn 1f16d682da py/misc: Fix msvc compilation with compressed error messages. 2022-07-18 23:25:12 +10:00
David Lechner a1ef5ac65d py/scheduler: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register sched_queue
instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner 85b4f36100 py/modsys: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register cur_exception,
sys_exitfunc, mp_sys_path_obj, mp_sys_argv_obj and sys_mutable
instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner a98aa66df6 py/persistentcode: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register track_reloc_code_list
instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner 2c728c5330 extmod/modbluetooth: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register `bluetooth`
instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner 32e32bd761 extmod/vfs: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register vfs_cur and
vfs_mount_table instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner d532c55e3b extmod/modlwip: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register lwip_slip_stream
instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner 631b692177 extmod/uos_dupterm: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register dupterm_objs
instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner 68f46342aa shared/runtime/pyexec: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register repl_line
instead of using a conditional inside of mp_state_vm_t.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:52:01 +10:00
David Lechner 7e4b205cb0 py/mpstate: Drop MICROPY_PORT_ROOT_POINTERS from mp_state_vm_t.
All in-tree uses of MICROPY_PORT_ROOT_POINTERS have been replaced with
MP_REGISTER_ROOT_POINTER(), so now we can remove both
MICROPY_PORT_ROOT_POINTERS and MICROPY_BOARD_ROOT_POINTERS from the code
and remaining config files.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:51:16 +10:00
David Lechner 81dbea1ce3 shared/readline: Use MP_REGISTER_ROOT_POINTER().
This uses MP_REGISTER_ROOT_POINTER() to register the readline_history root
pointer array used by shared/readline.c and removes the registration from
all mpconfigport.h files.

This also required adding a new MICROPY_READLINE_HISTORY_SIZE config option
since not all ports used the same sized array.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:48:49 +10:00
David Lechner fc3d7ae11b py/make_root_pointers: Add MP_REGISTER_ROOT_POINTER parser/generator.
This adds new compile-time infrastructure to parse source code files for
`MP_REGISTER_ROOT_POINTER()` and generates a new `root_pointers.h` header
file containing the collected declarations.  This works the same as the
existing `MP_REGISTER_MODULE()` feature.

Signed-off-by: David Lechner <david@pybricks.com>
2022-07-18 13:48:23 +10:00
Yonatan Goldschmidt a8d78cc398 py/obj: Add debug-only runtime checks to mp_obj_is_type().
Zero effect on non debug builds, and also usually optimized out even in
debug builds as mp_obj_is_type() is called with a compile-time known type.
I'm not sure we even have dynamic uses of mp_obj_is_type() at the moment,
but if we ever will they will be protected from now on.

Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2022-07-18 11:17:49 +10:00
Yonatan Goldschmidt 2a6ba47110 py/obj: Add static safety checks to mp_obj_is_type().
Commit d96cfd13e3 introduced a regression by breaking existing
users of mp_obj_is_type(.., &mp_obj_bool).  This function (and associated
helpers like mp_obj_is_int()) have some specific nuances, and mistakes like
this one can happen again.

This commit adds mp_obj_is_exact_type() which behaves like the the old
mp_obj_is_type().  The new mp_obj_is_type() has the same prototype but it
attempts to statically assert that it's not called with types which should
be checked using mp_obj_is_type().  If called with any of these types: int,
str, bool, NoneType - it will cause a compilation error.  Additional
checked types (e.g function types) can be added in the future.

Existing users of mp_obj_is_type() with the now "invalid" types, were
translated to use mp_obj_is_exact_type().

The use of MP_STATIC_ASSERT() is not bulletproof - usually GCC (and other
compilers) can't statically check conditions that are only known during
link-time (like variables' addresses comparison).  However, in this case,
GCC is able to statically detect these conditions, probably because it's
the exact same object - `&mp_type_int == &mp_type_int` is detected.
Misuses of this function with runtime-chosen types (e.g:
`mp_obj_type_t *x = ...; mp_obj_is_type(..., x);` won't be detected.  MSC
is unable to detect this, so we use MP_STATIC_ASSERT_NOT_MSC().

Compiling with this commit and without the fix for d96cfd13e3 shows
that it detects the problem.

Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2022-07-18 11:17:46 +10:00
Yonatan Goldschmidt 6670281472 py/misc: Add MP_STATIC_ASSERT_NOT_MSC().
To be used in cases where the condition of the assert does not compile
under msvc.

Signed-off-by: Yonatan Goldschmidt <yon.goldschmidt@gmail.com>
2022-07-18 11:11:00 +10:00
Scott Shawcroft ac460dd1e1
Merge branch 'main' into esp32 2022-07-13 15:30:53 -07:00
Lars Haulin 5bf3765631 py/objnamedtuple: Fix segfault with empty namedtuple.
The empty tuple is usually a constant object, but named tuples must be
allocated to allow modification.  Added explicit allocation to fix this.

Also added a regression test to verify creating an empty named tuple works.

Fixes issue #7870.

Signed-off-by: Lars Haulin <lars.haulin@gmail.com>
2022-07-13 16:25:35 +10:00