When foo.bar is imported, bar is added as an attribute to foo. Previously
this happened on every import, but should only happen on first import.
This verifies the behavior for relative imports and overriding.
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This verifies the behavior:
- Exact matches of built-ins bypass filesystem.
- u-prefix modules can be overridden from the filesystem.
- Builtin import can be forced using either u-prefix or sys.path=[].
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This demonstrates how to add a sub-package in a user c module, as well
as how to define the necessary qstrs and enable the feature in the build.
This is used by the unix coverage build to test this feature.
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
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>
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>
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>
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>
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>
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>
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>
MicroPython does not support these special methods, and they may get in the
way of other tests (eg indexing with __int__).
Signed-off-by: Damien George <damien@micropython.org>
This one sets the flash image length properly for the teensy loader, such
that the file system is not erased. It was already set in commit
8e54225140 but got lost when the MIMXRT1176
board was added.
Signed-off-by: robert-hh <robert@hammelrath.com>
Three bugs have been fixed in this commit:
1. When the duty was set with duty_u16(), changing the freq with pwm.freq()
would not keep relative duty rate, but the absolute pulse duration.
2. Fix another inconsistency when displaying the PWM pin's properties of a
QTMR channel.
3. Improve the error checks for the second channel being a PWM pin and pin
pairs to be a FLEXPWM A/B pair.
Signed-off-by: robert-hh <robert@hammelrath.com>
These have by default FAT support. The SAMD21 build does not support FAT.
The nrf port also implements os.sync(), but has it's own copy of moduos.c.
Code size increases seen: 40 to 56 bytes.
Signed-off-by: robert-hh <robert@hammelrath.com>
Avoiding code duplication. To enable it, set MICROPY_PY_UOS_SYNC in the
port's mpconfigport.h. It is operational only for FAT file system. For
other filesystems it's a no-op.
Signed-off-by: robert-hh <robert@hammelrath.com>
The hal_entry.c code is duplicated across all boards, so consolidate it to
a common ra_hal.c file. And remove the hal_entry() function because it
simply calls main().
Signed-off-by: Damien George <damien@micropython.org>
Also fix MAX_ENDPOINT definition for G0, which follows G4.
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
By specifying rts=pin(x) and/or cts=Pin(x) in the constructor. The pad
numbers for the UART pins are fix in this case: TX must be at pad 0, RX at
pad 1, RTS at pad 2 and CTS at pad 3.
repr(uart) shows the pin names for rts and cts, if set. In case of a RX
overflow, the rx interrupt will be disabled instead of just discarding the
data. That allows RTS to act.
If RTS is inactive, still 2 bytes can be buffered in the FIFO.
Signed-off-by: robert-hh <robert@hammelrath.com>
With Crystal: set the crystal startup wait time to 1 second. It was 2
seconds before, and that seeemed too long.
With USB-Sync: scan for up to 1 second for the USB to be registered and
carry on with boot as soon as it it. Before, the code just waited for
500ms.
Side change: improve related comments.
Signed-off-by: robert-hh <robert@hammelrath.com>
These include ADC, DAC, I2C, SoftI2C, SPI, SoftI2C, PWM, UART, pulse. This
is useful for devices like the Adafruit Trinket series which have almost no
accessible GPIO pins.
Signed-off-by: robert-hh <robert@hammelrath.com>
If sockets were open when calling soft reset, gc_sweep_all() would try to
close them. In case of e.g. the NINA WiFi handler, connected through SPI,
spi_transfer() would be called for command exchange with the NINA module.
But at that time SerCom was already disabled.
Moving sercom_deinit_all() behind gc_sweep_all() solves this issue.
Signed-off-by: robert-hh <robert@hammelrath.com>
Such that they are easier to adapt. The maximum code size is set by:
MICROPY_HW_CODESIZE=xxxK
in mpconfigmcu.mk for the MCU family as default or in mpconfigboard.mk for
a specific board. Setting the maximum code size allows the loader to error
out if the code gets larger than the space dedicated for it.
Signed-off-by: robert-hh <robert@hammelrath.com>
This commit adds the "--escape-non-printable" option to the repl command.
When specified the REPL console will escape non-printable characters,
printing them as their hex value in square brackets.
This escaping behaviour was previously the default and only behaviour, but
it is now opt-in.
As part of this change, the speed of echoing device data to the console is
improved by by reading and writing in chunks.
Signed-off-by: Damien George <damien@micropython.org>
Change UART clock source on S3/C3 so the UART can operate when CPU
frequency is below 80MHz. This allows the UART to remain operational when
using Dynamic Frequency Scaling (DFS).
Signed-off-by: Patrick Joy <patrick@joytech.com.au>
This commit enables the ULP for the S2 and S3 chips.
Note this is the FSM (Finite State Machine) ULP.
Signed-off-by: Patrick Joy <patrick@joytech.com.au>
Once all the firmware has been flashed and the final signatures checked,
mboot writes the "all good" byte into the header of the application. This
step uses the buffer firmware_head which, if unaligned in the build, fails
when cast to a uint64_t* in flash.c.
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
These were incorrectly added in d995c01042.
The fix here includes the full differential ADC definitions.
Signed-off-by: brave ulysses <brave_ulysses@email.com>