Files
micropython/py
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
..
2026-02-07 18:16:54 +11:00
2025-07-25 10:56:02 +10:00
2026-03-09 23:47:33 +11:00
2026-03-09 23:47:33 +11:00
2025-02-25 16:11:33 +11:00