Skip to content

Commit 168c40e

Browse files
authored
Merge pull request #8508 from dhalbert/v1.21-merge
V1.21 merge
2 parents e3873c1 + ecaf9e6 commit 168c40e

File tree

540 files changed

+8463
-3798
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

540 files changed

+8463
-3798
lines changed

.git-blame-ignore-revs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# all: Fix various spelling mistakes found by codespell 2.2.6.
2+
cf490a70917a1b2d38ba9b58e763e0837d0f7ca7
3+
4+
# all: Fix spelling mistakes based on codespell check.
5+
b1229efbd1509654dec6053865ab828d769e29db
6+
17
# top: Update Python formatting to black "2023 stable style".
28
8b2748269244304854b3462cb8902952b4dcb892
39

.github/workflows/run-tests.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ jobs:
5757
run: |
5858
make -C examples/natmod/features1
5959
make -C examples/natmod/features2
60-
make -C examples/natmod/uheapq
61-
make -C examples/natmod/urandom
62-
make -C examples/natmod/ure
63-
make -C examples/natmod/uzlib
60+
make -C examples/natmod/heapq
61+
make -C examples/natmod/random
62+
make -C examples/natmod/re
6463
- name: Test native modules
6564
if: matrix.test == 'all'
66-
run: ./run-natmodtests.py extmod/{uheapq*,ure*,uzlib*}.py
65+
run: ./run-natmodtests.py extmod/{heapq*,re*,zlib*}.py
6766
working-directory: tests

docs/library/gc.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Functions
2424

2525
.. function:: mem_alloc()
2626

27-
Return the number of bytes of heap RAM that are allocated.
27+
Return the number of bytes of heap RAM that are allocated by Python code.
2828

2929
.. admonition:: Difference to CPython
3030
:class: attention
@@ -33,8 +33,8 @@ Functions
3333

3434
.. function:: mem_free()
3535

36-
Return the number of bytes of available heap RAM, or -1 if this amount
37-
is not known.
36+
Return the number of bytes of heap RAM that is available for Python
37+
code to allocate, or -1 if this amount is not known.
3838

3939
.. admonition:: Difference to CPython
4040
:class: attention

docs/library/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limited flash memory:
1717
``binascii``, ``errno``, ``json``, ``re``.
1818

1919
These libraries are not currently enabled in any CircuitPython build, but may be in the future:
20-
``ctypes``
20+
``ctypes``, ``platform``
2121

2222
.. toctree::
2323
:maxdepth: 1
@@ -31,6 +31,7 @@ These libraries are not currently enabled in any CircuitPython build, but may be
3131
gc.rst
3232
io.rst
3333
json.rst
34+
platform.rst
3435
re.rst
3536
sys.rst
3637
ctypes.rst

docs/library/platform.rst

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
:mod:`platform` -- access to underlying platform’s identifying data
2+
===================================================================
3+
4+
.. module:: platform
5+
:synopsis: access to underlying platform’s identifying data
6+
7+
|see_cpython_module| :mod:`python:platform`.
8+
9+
This module tries to retrieve as much platform-identifying data as possible. It
10+
makes this information available via function APIs.
11+
12+
Functions
13+
---------
14+
15+
.. function:: platform()
16+
17+
Returns a string identifying the underlying platform. This string is composed
18+
of several substrings in the following order, delimited by dashes (``-``):
19+
20+
- the name of the platform system (e.g. Unix, Windows or MicroPython)
21+
- the MicroPython version
22+
- the architecture of the platform
23+
- the version of the underlying platform
24+
- the concatenation of the name of the libc that MicroPython is linked to
25+
and its corresponding version.
26+
27+
For example, this could be
28+
``"MicroPython-1.20.0-xtensa-IDFv4.2.4-with-newlib3.0.0"``.
29+
30+
.. function:: python_compiler()
31+
32+
Returns a string identifying the compiler used for compiling MicroPython.
33+
34+
.. function:: libc_ver()
35+
36+
Returns a tuple of strings *(lib, version)*, where *lib* is the name of the
37+
libc that MicroPython is linked to, and *version* the corresponding version
38+
of this libc.

docs/library/sys.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Constants
3333

3434
* *name* - string "circuitpython"
3535
* *version* - tuple (major, minor, micro), e.g. (1, 7, 0)
36+
* *_machine* - string describing the underlying machine
3637
* *_mpy* - supported mpy file-format version (optional attribute)
3738

3839
This object is the recommended way to distinguish CircuitPython from other

docs/reference/glossary.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Glossary
3232

3333
callee-owned tuple
3434
This is a MicroPython-specific construct where, for efficiency
35-
reasons, some built-in functions or methods may re-use the same
35+
reasons, some built-in functions or methods may reuse the same
3636
underlying tuple object to return data. This avoids having to allocate
3737
a new tuple for every call, and reduces heap fragmentation. Programs
3838
should not hold references to callee-owned tuples and instead only
@@ -120,7 +120,7 @@ Glossary
120120
<https://github.com/micropython/micropython-lib>`_ which provides
121121
implementations for many modules from CPython's standard library.
122122

123-
Some of the modules are are implemented in pure Python, and are able to
123+
Some of the modules are implemented in pure Python, and are able to
124124
be used on all ports. However, the majority of these modules use
125125
:term:`FFI` to access operating system functionality, and as such can
126126
only be used on the :term:`MicroPython Unix port` (with limited support

docs/shared_bindings_matrix.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
}
6464

6565
ADDITIONAL_MODULES = {
66-
"_asyncio": "MICROPY_PY_UASYNCIO",
66+
"_asyncio": "MICROPY_PY_ASYNCIO",
6767
"adafruit_bus_device": "CIRCUITPY_BUSDEVICE",
6868
"adafruit_pixelbuf": "CIRCUITPY_PIXELBUF",
6969
"array": "CIRCUITPY_ARRAY",
@@ -79,7 +79,7 @@
7979
"keypad.Keys": "CIRCUITPY_KEYPAD_KEYS",
8080
"keypad.ShiftRegisterKeys": "CIRCUITPY_KEYPAD_SHIFTREGISTERKEYS",
8181
"os.getenv": "CIRCUITPY_OS_GETENV",
82-
"select": "MICROPY_PY_USELECT_SELECT",
82+
"select": "MICROPY_PY_SELECT_SELECT",
8383
"sys": "CIRCUITPY_SYS",
8484
"terminalio": "CIRCUITPY_DISPLAYIO",
8585
"usb": "CIRCUITPY_USB_HOST",

examples/natmod/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Dynamic Native Modules
2+
3+
Dynamic Native Modules are .mpy files that contain native machine code from a
4+
language other than Python. For more info see [the documentation]
5+
(https://docs.micropython.org/en/latest/develop/natmod.html).
6+
7+
This should not be confused with [User C Modules]
8+
(https://docs.micropython.org/en/latest/develop/cmodules.html) which are a
9+
mechanism to add additional out-of-tree modules into the firmware build.
10+
11+
## Examples
12+
13+
This directory contains several examples of writing dynamic native modules, in
14+
two main categories:
15+
16+
1. Feature examples.
17+
18+
* `features0` - A module containing a single "factorial" function which
19+
demonstrates working with integers.
20+
21+
* `features1` - A module that demonstrates some common tasks:
22+
- defining simple functions exposed to Python
23+
- defining local, helper C functions
24+
- defining constant integers and strings exposed to Python
25+
- getting and creating integer objects
26+
- creating Python lists
27+
- raising exceptions
28+
- allocating memory
29+
- BSS and constant data (rodata)
30+
- relocated pointers in rodata
31+
32+
* `features2` - This is a hybrid module containing both Python and C code,
33+
and additionally the C code is spread over multiple files. It also
34+
demonstrates using floating point (only when the target supports
35+
hardware floating point).
36+
37+
* `features3` - A module that shows how to use types, constant objects,
38+
and creating dictionary instances.
39+
40+
* `features4` - A module that demonstrates how to define a class.
41+
42+
2. Dynamic version of existing built-ins.
43+
44+
This provides a way to add missing functionality to firmware that doesn't
45+
include certain built-in modules. See the `heapq`, `random`, `re`,
46+
`deflate`, `btree`, and `framebuf` directories.
47+
48+
So for example, if your firmware was compiled with `MICROPY_PY_FRAMEBUF`
49+
disabled (e.g. to save flash space), then it would not include the
50+
`framebuf` module. The `framebuf` native module provides a way to add the
51+
`framebuf` module dynamically.
52+
53+
The way these work is they define a dynamic native module which
54+
`#include`'s the original module and then does the necessary
55+
initialisation of the module's globals dict.
56+
57+
## Build instructions
58+
59+
To compile an example, you need to have the same toolchain available as
60+
required for your target port. e.g. `arm-none-eabi-gcc` for any ARM Cortex M
61+
target. See the port instructions for details.
62+
63+
You also need to have the `pyelftools` Python package available, either via
64+
your system package manager or installed from PyPI in a virtual environment
65+
with `pip`.
66+
67+
Each example provides a Makefile. You should specify the `ARCH` argument to
68+
make (one of x86, x64, armv6m, armv7m, xtensa, xtensawin):
69+
70+
```
71+
$ cd features0
72+
$ make ARCH=armv7m
73+
$ mpremote cp features0.mpy :
74+
```

examples/natmod/uzlib/Makefile renamed to examples/natmod/deflate/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
MPY_DIR = ../../..
33

44
# Name of module (different to built-in uzlib so it can coexist)
5-
MOD = uzlib_$(ARCH)
5+
MOD = deflate_$(ARCH)
66

77
# Source files (.c or .py)
8-
SRC = uzlib.c
8+
SRC = deflate.c
99

1010
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
1111
ARCH = x64

examples/natmod/deflate/deflate.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#define MICROPY_PY_DEFLATE (1)
2+
#define MICROPY_PY_DEFLATE_COMPRESS (1)
3+
4+
#include "py/dynruntime.h"
5+
6+
#if !defined(__linux__)
7+
void *memcpy(void *dst, const void *src, size_t n) {
8+
return mp_fun_table.memmove_(dst, src, n);
9+
}
10+
void *memset(void *s, int c, size_t n) {
11+
return mp_fun_table.memset_(s, c, n);
12+
}
13+
#endif
14+
15+
mp_obj_full_type_t deflateio_type;
16+
17+
#include "extmod/moddeflate.c"
18+
19+
// Re-implemented from py/stream.c, not yet available in dynruntime.h.
20+
mp_obj_t mp_stream_close(mp_obj_t stream) {
21+
const mp_stream_p_t *stream_p = mp_get_stream(stream);
22+
int error;
23+
mp_uint_t res = stream_p->ioctl(stream, MP_STREAM_CLOSE, 0, &error);
24+
if (res == MP_STREAM_ERROR) {
25+
mp_raise_OSError(error);
26+
}
27+
return mp_const_none;
28+
}
29+
MP_DEFINE_CONST_FUN_OBJ_1(mp_stream_close_obj, mp_stream_close);
30+
31+
// Re-implemented from py/stream.c, not yet available in dynruntime.h.
32+
STATIC mp_obj_t mp_stream___exit__(size_t n_args, const mp_obj_t *args) {
33+
(void)n_args;
34+
return mp_stream_close(args[0]);
35+
}
36+
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_stream___exit___obj, 4, 4, mp_stream___exit__);
37+
38+
// Re-implemented from obj.c, not yet available in dynruntime.h.
39+
mp_obj_t mp_identity(mp_obj_t self) {
40+
return self;
41+
}
42+
MP_DEFINE_CONST_FUN_OBJ_1(mp_identity_obj, mp_identity);
43+
44+
mp_map_elem_t deflateio_locals_dict_table[7];
45+
STATIC MP_DEFINE_CONST_DICT(deflateio_locals_dict, deflateio_locals_dict_table);
46+
47+
mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *args) {
48+
MP_DYNRUNTIME_INIT_ENTRY
49+
50+
deflateio_type.base.type = mp_fun_table.type_type;
51+
deflateio_type.name = MP_QSTR_DeflateIO;
52+
MP_OBJ_TYPE_SET_SLOT(&deflateio_type, make_new, &deflateio_make_new, 0);
53+
MP_OBJ_TYPE_SET_SLOT(&deflateio_type, protocol, &deflateio_stream_p, 1);
54+
deflateio_locals_dict_table[0] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_OBJ_FROM_PTR(&mp_stream_read_obj) };
55+
deflateio_locals_dict_table[1] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readinto), MP_OBJ_FROM_PTR(&mp_stream_readinto_obj) };
56+
deflateio_locals_dict_table[2] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_readline), MP_OBJ_FROM_PTR(&mp_stream_unbuffered_readline_obj) };
57+
deflateio_locals_dict_table[3] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_write), MP_OBJ_FROM_PTR(&mp_stream_write_obj) };
58+
deflateio_locals_dict_table[4] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR_close), MP_OBJ_FROM_PTR(&mp_stream_close_obj) };
59+
deflateio_locals_dict_table[5] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR___enter__), MP_OBJ_FROM_PTR(&mp_identity_obj) };
60+
deflateio_locals_dict_table[6] = (mp_map_elem_t){ MP_OBJ_NEW_QSTR(MP_QSTR___exit__), MP_OBJ_FROM_PTR(&mp_stream___exit___obj) };
61+
MP_OBJ_TYPE_SET_SLOT(&deflateio_type, locals_dict, (void*)&deflateio_locals_dict, 2);
62+
63+
mp_store_global(MP_QSTR___name__, MP_OBJ_NEW_QSTR(MP_QSTR_deflate));
64+
mp_store_global(MP_QSTR_DeflateIO, MP_OBJ_FROM_PTR(&deflateio_type));
65+
mp_store_global(MP_QSTR_RAW, MP_OBJ_NEW_SMALL_INT(DEFLATEIO_FORMAT_RAW));
66+
mp_store_global(MP_QSTR_ZLIB, MP_OBJ_NEW_SMALL_INT(DEFLATEIO_FORMAT_ZLIB));
67+
mp_store_global(MP_QSTR_GZIP, MP_OBJ_NEW_SMALL_INT(DEFLATEIO_FORMAT_GZIP));
68+
69+
MP_DYNRUNTIME_INIT_EXIT
70+
}

examples/natmod/features1/features1.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ mp_obj_t mpy_init(mp_obj_fun_bc_t *self, size_t n_args, size_t n_kw, mp_obj_t *a
8888
// This must be first, it sets up the globals dict and other things
8989
MP_DYNRUNTIME_INIT_ENTRY
9090

91-
// Messages can be printed as usually
91+
// Messages can be printed as usual
9292
mp_printf(&mp_plat_print, "initialising module self=%p\n", self);
9393

9494
// Make the functions available in the module's namespace

examples/natmod/features2/test.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# This Python code will be merged with the C code in main.c
22

3+
# ruff: noqa: F821 - this file is evaluated with C-defined names in scope
4+
35
import array
46

57

examples/natmod/features4/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Location of top-level MicroPython directory
2+
MPY_DIR = ../../..
3+
4+
# Name of module
5+
MOD = features4
6+
7+
# Source files (.c or .py)
8+
SRC = features4.c
9+
10+
# Architecture to build for (x86, x64, armv7m, xtensa, xtensawin)
11+
ARCH = x64
12+
13+
# Include to get the rules for compiling and linking the module
14+
include $(MPY_DIR)/py/dynruntime.mk

0 commit comments

Comments
 (0)