Damien George
d4751a164e
py: Add support for PEP 750's t-strings.
...
This commit adds support for t-strings by leveraging the existing f-string
parser in the lexer. It includes:
- t-string parsing in `py/lexer.c`
- new built-in `__template__()` function to construct t-string objects
- new built-in `Template` and `Interpolation` classes which implement all
the functionality from PEP 750
- new built-in `string` module with `templatelib` sub-module, which
contains the classes `Template` and `Interpolation`
The way the t-string parser works is that an input t-string like:
t"hello {name:5}"
is converted character-by-character by the lexer/tokenizer to:
__template__(("hello ", "",), name, "name", None, "5")
For reference, if it were an f-string it would be converted to:
"hello {:5}".format(name)
Some properties of this implementation:
- it's enabled by default at the full feature level,
MICROPY_CONFIG_ROM_LEVEL_AT_LEAST_FULL_FEATURES
- when enabled on a Cortex-M bare-metal port it costs about +3000 bytes
- there are no limits on the size or complexity of t-strings, and it allows
arbitrary levels of nesting of f-strings and t-strings (up to the memory
available to the compiler)
- the 'a' (ascii) conversion specifier is not supported (MicroPython does
not have the built-in `ascii` function)
- space after conversion specifier, eg t"{x!r :10}", is not supported
- arguments to `__template__` and `Interpolation` are not fully validated
(it's not necessary, it won't crash if the wrong arguments are passed in)
Otherwise the implementation here matches CPython.
Signed-off-by: Damien George <damien@micropython.org >
2026-03-09 23:47:33 +11:00
..
2025-05-13 10:36:47 +10:00
2025-07-01 15:34:29 +10:00
2025-12-19 14:26:01 +11:00
2025-12-07 16:02:38 +11:00
2024-06-21 15:06:07 +10:00
2026-02-24 18:42:53 +11:00
2025-12-19 17:06:53 +11:00
2025-08-19 12:40:29 +10:00
2026-02-24 18:42:53 +11:00
2024-03-19 10:31:36 +11:00
2025-12-19 14:26:01 +11:00
2024-03-19 10:31:36 +11:00
2025-12-19 14:26:01 +11:00
2026-02-07 18:36:22 +11:00
2025-12-19 14:26:01 +11:00
2022-03-28 15:43:09 +11:00
2026-02-05 10:43:52 +11:00
2025-10-24 16:32:53 +02:00
2025-08-12 14:34:19 +10:00
2019-09-02 13:14:27 +10:00
2025-08-01 13:35:44 +10:00
2025-02-11 16:42:14 +11:00
2026-02-05 10:43:54 +11:00
2025-11-21 00:01:01 +11:00
2026-02-07 18:36:22 +11:00
2025-02-11 16:42:14 +11:00
2024-08-14 12:55:45 +10:00
2025-10-02 16:30:52 +10:00
2025-05-13 10:36:47 +10:00
2026-01-11 09:22:13 +01:00
2025-01-02 11:49:10 +11:00
2024-07-18 12:35:42 +10:00
2025-08-01 13:35:44 +10:00
2026-01-27 01:20:36 +11:00
2025-12-26 10:00:02 +01:00
2025-12-07 16:05:14 +11:00
2025-05-09 18:31:40 +10:00
2026-02-10 14:17:25 +11:00
2022-05-23 15:43:06 +10:00
2025-12-19 14:26:01 +11:00
2025-12-19 14:26:01 +11:00
2024-06-21 15:06:07 +10:00
2022-05-23 15:43:06 +10:00
2022-05-23 15:43:06 +10:00
2024-03-07 14:20:42 +11:00
2022-05-23 15:43:06 +10:00
2022-05-23 15:43:06 +10:00
2025-08-01 00:47:33 +10:00
2025-08-01 00:47:33 +10:00
2022-02-24 18:08:43 +11:00
2022-05-25 13:04:45 +10:00
2026-02-07 17:41:26 +11:00
2023-09-15 12:19:13 +10:00
2020-06-16 23:18:01 +10:00
2026-03-09 23:47:33 +11:00
2026-02-04 23:19:09 +11:00
2025-09-15 14:54:09 +10:00
2025-09-15 14:54:09 +10:00
2025-10-04 16:22:32 +10:00
2025-09-15 14:54:09 +10:00
2025-09-15 14:54:09 +10:00
2025-09-15 14:54:09 +10:00
2025-09-10 17:24:56 +10:00
2024-03-07 14:20:42 +11:00
2026-02-07 18:16:54 +11:00
2024-12-10 11:28:38 +11:00
2025-08-15 01:24:53 +10:00
2025-09-26 14:38:05 +10:00
2024-03-07 14:20:42 +11:00
2026-03-09 23:47:33 +11:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2025-06-17 10:15:59 +10:00
2025-10-01 23:59:15 +10:00
2025-07-25 10:56:57 +10:00
2026-03-09 23:47:33 +11:00
2024-03-07 14:20:42 +11:00
2025-10-24 19:13:15 +02:00
2024-08-14 12:55:45 +10:00
2026-03-09 23:47:33 +11:00
2023-06-08 17:54:11 +10:00
2025-07-30 11:47:43 +10:00
2025-10-23 15:12:28 +11:00
2025-08-01 00:47:33 +10:00
2017-07-31 18:35:40 +10:00
2025-09-19 10:06:18 +02:00
2025-02-03 15:02:02 +11:00
2025-06-16 12:11:07 +10:00
2024-06-24 14:07:00 +10:00
2025-12-26 10:00:02 +01:00
2025-12-26 10:00:02 +01:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2023-06-02 21:47:34 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-05-13 10:36:47 +10:00
2025-07-25 10:56:02 +10:00
2026-03-09 23:47:33 +11:00
2026-02-06 00:19:48 +11:00
2025-03-06 12:52:35 +11:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2025-07-31 11:38:35 +10:00
2025-07-25 11:00:41 +10:00
2024-03-07 14:20:42 +11:00
2025-11-03 14:02:55 +11:00
2025-11-03 14:02:55 +11:00
2025-08-01 00:47:33 +10:00
2024-11-04 11:21:56 +11:00
2025-11-21 13:28:28 +11:00
2024-03-07 14:20:42 +11:00
2026-02-26 16:15:54 +11:00
2022-09-19 19:06:15 +10:00
2024-03-07 14:20:42 +11:00
2025-10-01 23:59:15 +10:00
2026-02-05 10:43:52 +11:00
2026-02-05 10:43:52 +11:00
2026-02-05 10:43:52 +11:00
2017-07-31 18:35:40 +10:00
2024-03-07 14:20:42 +11:00
2025-07-29 01:14:35 +10:00
2025-07-28 23:58:46 +10:00
2025-09-28 23:24:24 +10:00
2024-06-24 14:07:00 +10:00
2026-02-05 10:43:54 +11:00
2026-02-05 10:43:54 +11:00
2024-03-07 14:20:42 +11:00
2025-10-04 16:24:45 +10:00
2026-02-05 10:42:50 +11:00
2025-07-25 10:56:02 +10:00
2022-09-19 19:06:15 +10:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2025-09-26 13:34:57 +10:00
2024-03-07 14:20:42 +11:00
2025-08-15 01:21:25 +10:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2024-03-07 14:20:42 +11:00
2026-03-04 12:06:33 +11:00
2024-03-15 13:37:31 +11:00
2024-03-07 14:20:42 +11:00
2017-06-09 17:33:01 +03:00
2024-03-07 14:20:42 +11:00
2026-03-09 23:47:33 +11:00
2026-02-07 17:09:22 +11:00
2026-02-07 17:09:22 +11:00
2026-02-26 16:15:54 +11:00
2026-02-26 16:15:54 +11:00
2026-02-07 17:08:18 +11:00
2024-03-07 14:20:42 +11:00
2020-03-26 01:21:04 +11:00
2020-03-26 01:21:04 +11:00
2025-08-15 11:38:20 +10:00
2022-04-14 22:44:56 +10:00
2025-10-01 09:23:05 +10:00
2025-08-01 00:47:33 +10:00
2025-01-29 12:39:05 +11:00
2017-07-31 18:35:40 +10:00
2026-01-27 01:20:36 +11:00
2026-01-27 01:20:36 +11:00
2026-02-05 00:29:36 +11:00
2025-02-11 16:42:14 +11:00
2026-03-09 23:47:33 +11:00
2026-03-09 23:47:33 +11:00
2021-07-15 00:12:41 +10:00
2020-02-28 10:33:03 +11:00
2024-12-23 13:04:54 +11:00
2024-12-23 13:04:54 +11:00
2026-03-09 23:47:33 +11:00
2024-12-23 13:04:54 +11:00
2025-02-25 16:11:33 +11:00
2025-10-04 16:22:32 +10:00
2022-03-10 10:58:33 +11:00
2024-09-19 18:00:44 +10:00
2024-09-19 18:00:44 +10:00
2024-02-16 12:48:02 +11:00
2025-10-23 15:12:28 +11:00
2026-02-26 16:15:54 +11:00
2026-02-05 00:29:05 +11:00
2026-02-05 00:29:36 +11:00
2024-03-26 22:52:25 +11:00
2024-02-16 14:17:01 +11:00
2024-07-18 12:51:29 +10:00
2025-07-08 11:03:22 -04:00
2025-10-01 09:23:05 +10:00
2025-10-01 09:23:05 +10:00
2024-08-14 12:55:45 +10:00
2024-08-14 12:55:45 +10:00
2026-02-07 16:34:20 +11:00
2026-02-07 16:34:20 +11:00
2024-03-07 14:20:42 +11:00
2017-09-06 16:43:09 +10:00
2024-11-20 17:38:39 +11:00
2024-12-10 11:28:38 +11:00
2026-02-05 00:29:36 +11:00
2020-10-22 11:47:36 +02:00
2026-02-04 23:19:09 +11:00
2019-02-06 00:19:00 +11:00