mirror of
https://github.com/python/cpython.git
synced 2025-07-31 23:23:11 +00:00
[3.14] GH-132661: Add `string.templatelib.convert()
` (GH-135217) (#136671)
GH-132661: Add ``string.templatelib.convert()`` (GH-135217)
(cherry picked from commit 5b969fd645
)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
parent
115477399b
commit
2ae0eb1a87
2 changed files with 33 additions and 6 deletions
|
@ -1,15 +1,22 @@
|
|||
"""Support for template string literals (t-strings)."""
|
||||
|
||||
__all__ = [
|
||||
"Interpolation",
|
||||
"Template",
|
||||
]
|
||||
|
||||
t = t"{0}"
|
||||
Template = type(t)
|
||||
Interpolation = type(t.interpolations[0])
|
||||
del t
|
||||
|
||||
def convert(obj, /, conversion):
|
||||
"""Convert *obj* using formatted string literal semantics."""
|
||||
if conversion is None:
|
||||
return obj
|
||||
if conversion == 'r':
|
||||
return repr(obj)
|
||||
if conversion == 's':
|
||||
return str(obj)
|
||||
if conversion == 'a':
|
||||
return ascii(obj)
|
||||
raise ValueError(f'invalid conversion specifier: {conversion}')
|
||||
|
||||
def _template_unpickle(*args):
|
||||
import itertools
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue