LoLBench / cpython_13 /tests /private /omitted.patch
lolbench26's picture
Publish LoLBench Harbor tasks from artifact branch (part 2)
a4edccc verified
Raw History Blame Contribute Delete
13.5 kB
diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index bd199f8168b..11e8acd963a 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -320,3 +320,9 @@ Lib/test/test__colorize.py @hugovk
# Fuzzing
Modules/_xxtestfuzz/ @ammaraskar
+
+# t-strings
+**/*interpolationobject* @lysnikolaou
+**/*templateobject* @lysnikolaou
+**/*templatelib* @lysnikolaou
+**/*tstring* @lysnikolaou
diff --git a/Doc/library/token.rst b/Doc/library/token.rst
index 24455b1ef77..1529d173e17 100644
--- a/Doc/library/token.rst
+++ b/Doc/library/token.rst
@@ -131,6 +131,41 @@ The token constants are:
The token string contains the closing quote(s).
+.. data:: TSTRING_START
+
+ Token value used to indicate the beginning of a template string literal.
+
+ .. impl-detail::
+
+ The token string includes the prefix and the opening quote(s), but none
+ of the contents of the literal.
+
+ .. versionadded:: next
+
+.. data:: TSTRING_MIDDLE
+
+ Token value used for literal text inside a template string literal
+ including format specifications.
+
+ .. impl-detail::
+
+ Replacement fields (that is, the non-literal parts of t-strings) use
+ the same tokens as other expressions, and are delimited by
+ :data:`LBRACE`, :data:`RBRACE`, :data:`EXCLAMATION` and :data:`COLON`
+ tokens.
+
+ .. versionadded:: next
+
+.. data:: TSTRING_END
+
+ Token value used to indicate the end of a template string literal.
+
+ .. impl-detail::
+
+ The token string contains the closing quote(s).
+
+ .. versionadded:: next
+
.. data:: ENDMARKER
Token value that indicates the end of input.
diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst
index 9e6b69fbc05..dad63e47a62 100644
--- a/Doc/whatsnew/3.14.rst
+++ b/Doc/whatsnew/3.14.rst
@@ -66,6 +66,7 @@ Summary -- release highlights
* :ref:`PEP 649: deferred evaluation of annotations <whatsnew314-pep649>`
* :ref:`PEP 741: Python Configuration C API <whatsnew314-pep741>`
+* :ref:`PEP 750: Template Strings <whatsnew314-pep750>`
* :ref:`PEP 758: Allow except and except* expressions without parentheses <whatsnew314-pep758>`
* :ref:`PEP 761: Discontinuation of PGP signatures <whatsnew314-pep761>`
* :ref:`PEP 765: Disallow return/break/continue that exit a finally block <whatsnew314-pep765>`
@@ -92,6 +93,76 @@ If you encounter :exc:`NameError`\s or pickling errors coming out of
New features
============
+.. _whatsnew314-pep750:
+
+PEP 750: Template Strings
+-------------------------
+
+Template string literals (t-strings) are a generalization of f-strings,
+using a ``t`` in place of the ``f`` prefix. Instead of evaluating
+to :class:`str`, t-strings evaluate to a new :class:`!string.templatelib.Template` type:
+
+.. code-block:: python
+
+ from string.templatelib import Template
+
+ name = "World"
+ template: Template = t"Hello {name}"
+
+The template can then be combined with functions that operate on the template's
+structure to produce a :class:`str` or a string-like result.
+For example, sanitizing input:
+
+.. code-block:: python
+
+ evil = "<script>alert('evil')</script>"
+ template = t"<p>{evil}</p>"
+ assert html(template) == "<p>&lt;script&gt;alert('evil')&lt;/script&gt;</p>"
+
+As another example, generating HTML attributes from data:
+
+.. code-block:: python
+
+ attributes = {"src": "shrubbery.jpg", "alt": "looks nice"}
+ template = t"<img {attributes} />"
+ assert html(template) == '<img src="shrubbery.jpg" alt="looks nice" class="looks-nice" />'
+
+Unlike f-strings, the ``html`` function has access to template attributes
+containing the original information: static strings, interpolations, and values
+from the original scope. Unlike existing templating approaches, t-strings build
+from the well-known f-string syntax and rules. Template systems thus benefit
+from Python tooling as they are much closer to the Python language, syntax,
+scoping, and more.
+
+Writing template handlers is straightforward:
+
+.. code-block:: python
+
+ from string.templatelib import Template, Interpolation
+
+ def lower_upper(template: Template) -> str:
+ """Render static parts lowercased and interpolations uppercased."""
+ parts: list[str] = []
+ for item in template:
+ if isinstance(item, Interpolation):
+ parts.append(str(item.value).upper())
+ else:
+ parts.append(item.lower())
+ return "".join(parts)
+
+ name = "world"
+ assert lower_upper(t"HELLO {name}") == "hello WORLD"
+
+With this in place, developers can write template systems to sanitize SQL, make
+safe shell operations, improve logging, tackle modern ideas in web development
+(HTML, CSS, and so on), and implement lightweight, custom business DSLs.
+
+See :pep:`750` for more details.
+
+(Contributed by Jim Baker, Guido van Rossum, Paul Everitt, Koudai Aono,
+Lysandros Nikolaou, Dave Peck, Adam Turner, Jelle Zijlstra, Bénédikt Tran,
+and Pablo Galindo Salgado in :gh:`132661`.)
+
.. _whatsnew314-pep768:
PEP 768: Safe external debugger interface for CPython
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-22-15-37-05.gh-issue-132661.XE_A42.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-22-15-37-05.gh-issue-132661.XE_A42.rst
new file mode 100644
index 00000000000..666e8de943b
--- /dev/null
+++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-22-15-37-05.gh-issue-132661.XE_A42.rst
@@ -0,0 +1,3 @@
+Implement :pep:`750` (Template Strings). Add new syntax for t-strings and
+implement new internal :class:`!string.templatelib.Template` and
+:class:`!string.templatelib.Interpolation` types.
diff --git a/PCbuild/_freeze_module.vcxproj b/PCbuild/_freeze_module.vcxproj
index 59d664dc174..1f3f3170f3f 100644
--- a/PCbuild/_freeze_module.vcxproj
+++ b/PCbuild/_freeze_module.vcxproj
@@ -143,6 +143,7 @@ <?xml version="1.0" encoding="utf-8"?>
<ClCompile Include="..\Objects\funcobject.c" />
<ClCompile Include="..\Objects\genericaliasobject.c" />
<ClCompile Include="..\Objects\genobject.c" />
+ <ClCompile Include="..\Objects\interpolationobject.c" />
<ClCompile Include="..\Objects\iterobject.c" />
<ClCompile Include="..\Objects\listobject.c" />
<ClCompile Include="..\Objects\longobject.c" />
@@ -158,6 +159,7 @@ <?xml version="1.0" encoding="utf-8"?>
<ClCompile Include="..\Objects\setobject.c" />
<ClCompile Include="..\Objects\sliceobject.c" />
<ClCompile Include="..\Objects\structseq.c" />
+ <ClCompile Include="..\Objects\templateobject.c" />
<ClCompile Include="..\Objects\tupleobject.c" />
<ClCompile Include="..\Objects\typeobject.c" />
<ClCompile Include="..\Objects\typevarobject.c" />
diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj
index ce9b056ac25..d2aafce058d 100644
--- a/PCbuild/pythoncore.vcxproj
+++ b/PCbuild/pythoncore.vcxproj
@@ -264,6 +264,7 @@ <?xml version="1.0" encoding="utf-8"?>
<ClInclude Include="..\Include\internal\pycore_interp_structs.h" />
<ClInclude Include="..\Include\internal\pycore_interpframe.h" />
<ClInclude Include="..\Include\internal\pycore_interpframe_structs.h" />
+ <ClInclude Include="..\Include\internal\pycore_interpolation.h" />
<ClInclude Include="..\Include\internal\pycore_intrinsics.h" />
<ClInclude Include="..\Include\internal\pycore_jit.h" />
<ClInclude Include="..\Include\internal\pycore_list.h" />
@@ -310,6 +311,7 @@ <?xml version="1.0" encoding="utf-8"?>
<ClInclude Include="..\Include\internal\pycore_structseq.h" />
<ClInclude Include="..\Include\internal\pycore_sysmodule.h" />
<ClInclude Include="..\Include\internal\pycore_symtable.h" />
+ <ClInclude Include="..\Include\internal\pycore_template.h" />
<ClInclude Include="..\Include\internal\pycore_time.h" />
<ClInclude Include="..\Include\internal\pycore_token.h" />
<ClInclude Include="..\Include\internal\pycore_traceback.h" />
@@ -528,6 +530,7 @@ <?xml version="1.0" encoding="utf-8"?>
<ClCompile Include="..\Objects\funcobject.c" />
<ClCompile Include="..\Objects\genericaliasobject.c" />
<ClCompile Include="..\Objects\genobject.c" />
+ <ClCompile Include="..\Objects\interpolationobject.c" />
<ClCompile Include="..\Objects\iterobject.c" />
<ClCompile Include="..\Objects\listobject.c" />
<ClCompile Include="..\Objects\longobject.c" />
@@ -543,6 +546,7 @@ <?xml version="1.0" encoding="utf-8"?>
<ClCompile Include="..\Objects\setobject.c" />
<ClCompile Include="..\Objects\sliceobject.c" />
<ClCompile Include="..\Objects\structseq.c" />
+ <ClCompile Include="..\Objects\templateobject.c" />
<ClCompile Include="..\Objects\tupleobject.c" />
<ClCompile Include="..\Objects\typeobject.c" />
<ClCompile Include="..\Objects\typevarobject.c" />
diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters
index 2c2eac85941..873f7d65a26 100644
--- a/PCbuild/pythoncore.vcxproj.filters
+++ b/PCbuild/pythoncore.vcxproj.filters
@@ -708,6 +708,9 @@ <?xml version="1.0" encoding="utf-8"?>
<ClInclude Include="..\Include\internal\pycore_interpframe_structs.h">
<Filter>Include\internal</Filter>
</ClInclude>
+ <ClInclude Include="..\Include\internal\pycore_interpolation.h">
+ <Filter>Include\internal</Filter>
+ </ClInclude>
<ClInclude Include="..\Include\internal\pycore_intrinsics.h">
<Filter>Include\cpython</Filter>
</ClInclude>
@@ -840,6 +843,9 @@ <?xml version="1.0" encoding="utf-8"?>
<ClInclude Include="..\Include\internal\pycore_stackref.h">
<Filter>Include\internal</Filter>
</ClInclude>
+ <ClInclude Include="..\Include\internal\pycore_template.h">
+ <Filter>Include\internal</Filter>
+ </ClInclude>
<ClInclude Include="..\Include\internal\pycore_time.h">
<Filter>Include\internal</Filter>
</ClInclude>
diff --git a/Programs/test_frozenmain.h b/Programs/test_frozenmain.h
index 8cedee31e08..b7d23f57018 100644
--- a/Programs/test_frozenmain.h
+++ b/Programs/test_frozenmain.h
@@ -1,19 +1,19 @@
// Auto-generated by Programs/freeze_test_frozenmain.py
unsigned char M_test_frozenmain[] = {
227,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,
- 0,0,0,0,0,243,184,0,0,0,128,0,92,0,80,1,
- 71,0,114,0,92,0,80,1,71,1,114,1,91,2,32,0,
- 80,2,50,1,0,0,0,0,0,0,30,0,91,2,32,0,
- 80,3,91,0,78,6,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,50,2,0,0,0,0,0,0,
- 30,0,91,1,78,8,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,32,0,50,0,0,0,0,0,
- 0,0,80,4,43,26,0,0,0,0,0,0,0,0,0,0,
- 114,5,80,7,15,0,68,24,0,0,114,6,91,2,32,0,
- 80,5,91,6,11,0,80,6,91,5,91,6,43,26,0,0,
- 0,0,0,0,0,0,0,0,11,0,48,4,50,1,0,0,
- 0,0,0,0,30,0,73,26,0,0,8,0,29,0,80,1,
- 34,0,41,8,233,0,0,0,0,78,122,18,70,114,111,122,
+ 0,0,0,0,0,243,184,0,0,0,128,0,94,0,82,1,
+ 73,0,116,0,94,0,82,1,73,1,116,1,93,2,33,0,
+ 82,2,52,1,0,0,0,0,0,0,31,0,93,2,33,0,
+ 82,3,93,0,80,6,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,52,2,0,0,0,0,0,0,
+ 31,0,93,1,80,8,0,0,0,0,0,0,0,0,0,0,
+ 0,0,0,0,0,0,0,0,33,0,52,0,0,0,0,0,
+ 0,0,82,4,44,26,0,0,0,0,0,0,0,0,0,0,
+ 116,5,82,7,16,0,70,24,0,0,116,6,93,2,33,0,
+ 82,5,93,6,12,0,82,6,93,5,93,6,44,26,0,0,
+ 0,0,0,0,0,0,0,0,12,0,50,4,52,1,0,0,
+ 0,0,0,0,31,0,75,26,0,0,9,0,30,0,82,1,
+ 35,0,41,8,233,0,0,0,0,78,122,18,70,114,111,122,
101,110,32,72,101,108,108,111,32,87,111,114,108,100,122,8,
115,121,115,46,97,114,103,118,218,6,99,111,110,102,105,103,
122,7,99,111,110,102,105,103,32,122,2,58,32,41,5,218,
diff --git a/Tools/c-analyzer/TODO b/Tools/c-analyzer/TODO
index d509489176b..2077534ccf4 100644
--- a/Tools/c-analyzer/TODO
+++ b/Tools/c-analyzer/TODO
@@ -794,6 +794,7 @@ Objects/genobject.c:_PyAsyncGenASend_Type PyTypeObject _P
Objects/genobject.c:_PyAsyncGenAThrow_Type PyTypeObject _PyAsyncGenAThrow_Type
Objects/genobject.c:_PyAsyncGenWrappedValue_Type PyTypeObject _PyAsyncGenWrappedValue_Type
Objects/genobject.c:_PyCoroWrapper_Type PyTypeObject _PyCoroWrapper_Type
+Objects/interpolationobject.c:_PyInterpolation_Type PyTypeObject _PyInterpolation_Type
Objects/interpreteridobject.c:_PyInterpreterID_Type PyTypeObject _PyInterpreterID_Type
Objects/iterobject.c:PyCallIter_Type PyTypeObject PyCallIter_Type
Objects/iterobject.c:PySeqIter_Type PyTypeObject PySeqIter_Type
@@ -827,6 +828,8 @@ Objects/sliceobject.c:PyEllipsis_Type PyTypeObject Py
Objects/sliceobject.c:PySlice_Type PyTypeObject PySlice_Type
Objects/stringlib/unicode_format.h:PyFieldNameIter_Type static PyTypeObject PyFieldNameIter_Type
Objects/stringlib/unicode_format.h:PyFormatterIter_Type static PyTypeObject PyFormatterIter_Type
+Objects/templateobject.c:_PyTemplateIter_Type PyTypeObject _PyTemplateIter_Type
+Objects/templateobject.c:_PyTemplate_Type PyTypeObject _PyTemplate_Type
Objects/tupleobject.c:PyTupleIter_Type PyTypeObject PyTupleIter_Type
Objects/tupleobject.c:PyTuple_Type PyTypeObject PyTuple_Type
Objects/typeobject.c:PyBaseObject_Type PyTypeObject PyBaseObject_Type