-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Update MicroPython to v1.28.0 #11334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6b13e6c
230bbbb
e66a1a3
eae83df
b14d129
f1363d8
b792efc
cfaba32
16ccf55
8e3ee30
baf75e2
8248d11
2782d45
f387484
78ec31b
f918e33
894d7a5
33474a6
c696ca9
a7cd418
25b400f
a5bda23
03f50d3
384cc62
878f800
93692ca
85e9d75
2b07661
2a5a8cc
5021137
77e62f6
af88d65
1b2f6e3
be15be3
af76da9
96bce47
0e3cc29
b0feb9c
cf048e0
0e9e661
1046b5d
023a49c
1ecec98
63dbaca
6ce0214
2b4b05a
e40d609
edab53c
e5f772a
d291a28
cef2538
a1d4f62
f624200
6436f8b
60c37a2
5f59f39
bfb5e77
617c7db
7ea88f7
c9f747c
570744d
adbdded
2984574
7484598
0c7726a
e07bad9
cf90c8d
60fa4ab
512da03
1199015
8c1462b
90f259f
71b6a85
b2073a0
53d9004
1095bbb
ee99014
ed7bfcb
a63cc63
5e04827
3ca2722
115f413
0b150d7
633a27b
71866ed
3332e1e
a2aad14
1ebeee0
eaf03bd
39d777c
dc91fa6
9dcad25
b7d7a26
588fa2b
92d64ca
05c2f88
6ae08ea
9721955
a8b7155
b892461
f949806
746e2c9
fd009e6
7e1f754
d278d6b
43cfbbb
9dea789
8834330
de7e360
1e164be
1ab9b66
c1ed5f7
9e2df2d
158cbd6
f02dc6d
b46f9f5
424ae08
43a4914
b006bb0
fdb7c0f
2e62b56
6b8bcb6
6dbabc9
2050055
aaa30ab
c199ba9
2631b06
412ffd4
2ab88c3
ff4d4bf
b8098df
7b91633
e08e3d7
0dac591
104deaa
30cc160
ab86f75
947d04e
e8304ee
f013735
8a1f050
1bbce9c
d4b5eb8
45556c4
6d9d880
3b92139
414fcbf
aac4653
58d2610
04bf835
4191cec
1a01f76
bebb404
63e4529
95b3e72
bfc69db
716aae8
65ddc23
06bfefe
c3ca843
61bbd78
b1aba22
655dc9f
b64f4f9
43d38d9
2322d37
550b68f
6820e22
41d49b6
b343a36
58436b2
d4751a1
a989585
bdef10a
68c2d4e
10af0a2
3a24bdb
20ffe6f
1968b96
8cbd320
ad054fc
4319927
fc5195b
4625f97
e4920d6
df9b714
06dbc1f
d2cda57
82c6b0e
fe32e1d
f625d2e
bac45e5
4b339ee
a906cfb
d1c936d
cda49be
6768325
0225704
6f835b3
6cac2d2
c802a13
e74f3d5
2d3241f
dbe6a11
094c268
6e9d35b
142f8b9
1d5073f
af31472
2b64d6d
98ab12a
ef2b30b
47871a4
8f24c86
2ccf78a
e0beace
b1d635f
b3d88cf
406356e
8a3c9f0
134bf4d
82e44e0
702f15a
803a4d7
74e9457
c91d09a
2cca348
f83f363
6f96d26
44d8f70
5c00edc
85e8f61
bce8549
93201ff
ac48088
f4d2447
2dc2e30
d41b8dc
e8a3ee0
e0e9fbb
e2588d1
184726c
6877260
d01486b
0dd9201
ae2ec71
f7b77e5
568bfb7
2097c90
2686e0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| :mod:`string.templatelib` -- Template String Support | ||
| ==================================================== | ||
|
|
||
| .. module:: string.templatelib | ||
| :synopsis: PEP 750 template string support | ||
|
|
||
| This module provides support for template strings (t-strings) as defined in | ||
| `PEP 750 <https://peps.python.org/pep-0750/>`_. Template strings are created | ||
| using the ``t`` prefix and provide access to both the literal string parts and | ||
| interpolated values before they are combined. | ||
|
|
||
| **Availability:** template strings require ``MICROPY_PY_TSTRINGS`` to be enabled | ||
| at compile time. They are enabled by default at the full feature level, which | ||
| includes the alif, mimxrt and samd (SAMD51 only) ports, the unix coverage variant | ||
| and the webassembly pyscript variant. | ||
|
|
||
| Classes | ||
| ------- | ||
|
|
||
| .. class:: Template(*args) | ||
|
|
||
| Represents a template string. Template objects are typically created by | ||
| t-string syntax (``t"..."``) but can also be constructed directly using | ||
| the constructor. | ||
|
|
||
| .. attribute:: strings | ||
|
|
||
| A tuple of string literals that appear between interpolations. | ||
|
|
||
| .. attribute:: interpolations | ||
|
|
||
| A tuple of :class:`Interpolation` objects representing the interpolated | ||
| expressions. | ||
|
|
||
| .. attribute:: values | ||
|
|
||
| A read-only property that returns a tuple containing the ``value`` | ||
| attribute from each interpolation in the template. | ||
|
|
||
| .. method:: __iter__() | ||
|
|
||
| Iterate over the template contents, yielding string parts and | ||
| :class:`Interpolation` objects in the order they appear. Empty strings | ||
| are omitted. | ||
|
|
||
| .. method:: __add__(other) | ||
|
|
||
| Concatenate two templates. Returns a new :class:`Template` combining | ||
| the strings and interpolations from both templates. | ||
|
|
||
| :raises TypeError: if *other* is not a :class:`Template` | ||
|
|
||
| Template concatenation with ``str`` is prohibited to avoid ambiguity | ||
| about whether the string should be treated as a literal or interpolation:: | ||
|
|
||
| t1 = t"Hello " | ||
| t2 = t"World" | ||
| result = t1 + t2 # Valid | ||
|
|
||
| # TypeError: cannot concatenate str to Template | ||
| result = t1 + "World" | ||
|
|
||
| .. class:: Interpolation(value, expression='', conversion=None, format_spec='') | ||
|
|
||
| Represents an interpolated expression within a template string. All | ||
| arguments can be passed as keyword arguments. | ||
|
|
||
| .. attribute:: value | ||
|
|
||
| The evaluated value of the interpolated expression. | ||
|
|
||
| .. attribute:: expression | ||
|
|
||
| The string representation of the expression as it appeared in the | ||
| template string. | ||
|
|
||
| .. attribute:: conversion | ||
|
|
||
| The conversion specifier (``'s'`` or ``'r'``) if present, otherwise ``None``. | ||
| Note that MicroPython does not support the ``'a'`` conversion. | ||
|
|
||
| .. attribute:: format_spec | ||
|
|
||
| The format specification string if present, otherwise an empty string. | ||
|
|
||
| Template String Syntax | ||
| ---------------------- | ||
|
|
||
| Template strings use the same syntax as f-strings but with a ``t`` prefix:: | ||
|
|
||
| name = "World" | ||
| template = t"Hello {name}!" | ||
|
|
||
| # Access template components | ||
| print(template.strings) # ('Hello ', '!') | ||
| print(template.values) # ('World',) | ||
| print(template.interpolations[0].expression) # 'name' | ||
|
|
||
| Conversion Specifiers | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Template strings store conversion specifiers as metadata. Unlike f-strings, | ||
| the conversion is not applied automatically:: | ||
|
|
||
| value = "test" | ||
| t = t"{value!r}" | ||
| # t.interpolations[0].value == "test" (not repr(value)) | ||
| # t.interpolations[0].conversion == "r" | ||
|
|
||
| Processing code must explicitly apply conversions when needed. | ||
|
|
||
| Format Specifications | ||
| ~~~~~~~~~~~~~~~~~~~~~ | ||
|
|
||
| Format specifications are stored as metadata in the ``Interpolation`` object. | ||
| Unlike f-strings, formatting is not applied automatically:: | ||
|
|
||
| pi = 3.14159 | ||
| t = t"{pi:.2f}" | ||
| # t.interpolations[0].value == 3.14159 (not formatted) | ||
| # t.interpolations[0].format_spec == ".2f" | ||
|
|
||
| Per PEP 750, processing code is not required to use format specifications, but | ||
| when present they should be respected and match f-string behavior where possible. | ||
|
|
||
| Debug Format | ||
| ~~~~~~~~~~~~ | ||
|
|
||
| The debug format ``{expr=}`` is supported:: | ||
|
|
||
| x = 42 | ||
| t = t"{x=}" | ||
| # t.strings == ("x=", "") | ||
| # t.interpolations[0].expression == "x" | ||
| # t.interpolations[0].conversion == "r" | ||
|
|
||
| .. admonition:: Important | ||
| :class: attention | ||
|
|
||
| As per PEP 750, unlike f-strings, template strings do not automatically | ||
| apply conversions or format specifications. This is by design to allow | ||
| processing code to control how these are handled. Processing code must | ||
| explicitly handle these attributes. | ||
|
|
||
| MicroPython does not provide the ``format()`` built-in function. Use | ||
| string formatting methods like ``str.format()`` instead. | ||
|
|
||
| Example Usage | ||
| ------------- | ||
|
|
||
| Basic processing without format support:: | ||
|
|
||
| def simple_process(template): | ||
| """Simple template processing""" | ||
| parts = [] | ||
| for item in template: | ||
| if isinstance(item, str): | ||
| parts.append(item) | ||
| else: | ||
| parts.append(str(item.value)) | ||
| return "".join(parts) | ||
|
|
||
| Processing template with format support:: | ||
|
|
||
| from string.templatelib import Template, Interpolation | ||
|
|
||
| def convert(value, conversion): | ||
| """Apply conversion specifier to value""" | ||
| if conversion == "r": | ||
| return repr(value) | ||
| elif conversion == "s": | ||
| return str(value) | ||
| return value | ||
|
|
||
| def process_template(template): | ||
| """Process template with conversion and format support""" | ||
| result = [] | ||
| for part in template: | ||
| if isinstance(part, str): | ||
| result.append(part) | ||
| else: # Interpolation | ||
| value = convert(part.value, part.conversion) | ||
| if part.format_spec: | ||
| # Apply format specification using str.format | ||
| value = ("{:" + part.format_spec + "}").format(value) | ||
| else: | ||
| value = str(value) | ||
| result.append(value) | ||
| return "".join(result) | ||
|
|
||
| pi = 3.14159 | ||
| name = "Alice" | ||
| t = t"{name!r}: {pi:.2f}" | ||
| print(process_template(t)) | ||
| # Output: "'Alice': 3.14" | ||
|
|
||
| # Other format specifications work too | ||
| value = 42 | ||
| print(process_template(t"{value:>10}")) # " 42" | ||
| print(process_template(t"{value:04d}")) # "0042" | ||
|
|
||
| HTML escaping example:: | ||
|
|
||
| def html_escape(value): | ||
| """Escape HTML special characters""" | ||
| if not isinstance(value, str): | ||
| value = str(value) | ||
| return value.replace("&", "&").replace("<", "<").replace(">", ">") | ||
|
|
||
| def safe_html(template): | ||
| """Convert template to HTML-safe string""" | ||
| result = [] | ||
| for part in template: | ||
| if isinstance(part, str): | ||
| result.append(part) | ||
| else: | ||
| result.append(html_escape(part.value)) | ||
| return "".join(result) | ||
|
|
||
| user_input = "<script>alert('xss')</script>" | ||
| t = t"User said: {user_input}" | ||
| print(safe_html(t)) | ||
| # Output: "User said: <script>alert('xss')</script>" | ||
|
|
||
| See Also | ||
| -------- | ||
|
|
||
| * `PEP 750 <https://peps.python.org/pep-0750/>`_ - Template Strings specification | ||
| * :ref:`python:formatstrings` - Format string syntax | ||
| * `Formatted string literals <https://docs.python.org/3/reference/lexical_analysis.html#f-strings>`_ - f-strings in Python | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| :mod:`weakref` -- Python object lifetime management | ||
| =================================================== | ||
|
|
||
| .. module:: weakref | ||
| :synopsis: Create weak references to Python objects | ||
|
|
||
| |see_cpython_module| :mod:`python:weakref`. | ||
|
|
||
| This module allows creation of weak references to Python objects. A weak reference | ||
| is a non-traceable reference to a heap-allocated Python object, so the garbage | ||
| collector can still reclaim the object even though the weak reference refers to it. | ||
|
|
||
| Python callbacks can be registered to be called when an object is reclaimed by the | ||
| garbage collector. This provides a safe way to clean up when objects are no longer | ||
| needed. | ||
|
|
||
| **Availability:** the weakref module requires ``MICROPY_PY_WEAKREF`` to be enabled | ||
| at compile time. It is enabled on the unix coverage variant and the webassembly | ||
| pyscript variant. | ||
|
Comment on lines
+17
to
+19
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. MicroPython-specific.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem that useful for us right now. Just remove it from the toctree. |
||
|
|
||
| ref objects | ||
| ----------- | ||
|
|
||
| A ref object is the simplest way to make a weak reference. | ||
|
|
||
| .. class:: ref(object [, callback], /) | ||
|
|
||
| Return a weak reference to the given *object*. | ||
|
|
||
| If *callback* is given and is not ``None`` then, when *object* is reclaimed | ||
| by the garbage collector and if the weak reference object is still alive, the | ||
| *callback* will be called. The *callback* will be passed the weak reference | ||
| object as its single argument. | ||
|
|
||
| .. method:: ref.__call__() | ||
|
|
||
| Calling the weak reference object will return its referenced object if that | ||
| object is still alive. Otherwise ``None`` will be returned. | ||
|
|
||
| finalize objects | ||
| ---------------- | ||
|
|
||
| A finalize object is an extended version of a ref object that is more convenient to | ||
| use, and allows more control over the callback. | ||
|
|
||
| .. class:: finalize(object, callback, /, *args, **kwargs) | ||
|
|
||
| Return a weak reference to the given *object*. In contrast to *weakref.ref* | ||
| objects, finalize objects are held onto internally and will not be collected until | ||
| *object* is collected. | ||
|
|
||
| A finalize object starts off alive. It transitions to the dead state when the | ||
| finalize object is called, either explicitly or when *object* is collected. It also | ||
| transitions to dead if the `finalize.detach()` method is called. | ||
|
|
||
| When *object* is reclaimed by the garbage collector (or the finalize object is | ||
| explicitly called by user code) and the finalize object is still in the alive state, | ||
| the *callback* will be called. The *callback* will be passed arguments as: | ||
| ``callback(*args, **kwargs)``. | ||
|
|
||
| .. method:: finalize.__call__() | ||
|
|
||
| If the finalize object is alive then it transitions to the dead state and returns | ||
| the value of ``callback(*args, **kwargs)``. Otherwise ``None`` will be returned. | ||
|
|
||
| .. method:: finalize.alive | ||
|
|
||
| Read-only boolean attribute that indicates if the finalizer is in the alive state. | ||
|
|
||
| .. method:: finalize.peek() | ||
|
|
||
| If the finalize object is alive then return ``(object, callback, args, kwargs)``. | ||
| Otherwise return ``None``. | ||
|
|
||
| .. method:: finalize.detach() | ||
|
|
||
| If the finalize object is alive then it transitions to the dead state and returns | ||
| ``(object, callback, args, kwargs)``. Otherwise ``None`` will be returned. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment about availability is MicroPython-specific.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, and it may go further.
MICROPY_PY_TSTRINGSis only enabled inmpy-cross, never in firmware, so no board can import this module. The merge also added this page to thedocs/library/index.rsttoctree. Should it be there at all, or would you rather I just rewrite the availability text?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to decide whether to add this new templates feature to most boards. What is the size growth when it's turned on?
The easy documentation fix is just to remove the
Availability:paragraph. Users can always check circuitpython.org.