8000 gh-132661: Implement PEP 750 by lysnikolaou · Pull Request #132662 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132661: Implement PEP 750 #132662

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

Merged
merged 104 commits into from
Apr 30, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
9248287
Implement next version of PEP 750
lysnikolaou Oct 1, 2024
450d771
First fixes; conv and format_spec still not working
lysnikolaou Oct 18, 2024
a095a9b
Change BUILD_INTERPOLATION opcode to simplify
lysnikolaou Oct 18, 2024
499d70c
Add templatelib module & clean-up types/objects etc.
lysnikolaou Oct 20, 2024
f742d45
Support explicit concatenation of template and str
lysnikolaou Oct 21, 2024
0dda3f9
Avoid folding one node template into its content
lysnikolaou Oct 22, 2024
17014e7
Implement __eq__ for template and interpolation
lysnikolaou Oct 23, 2024
94371bd
Fix debug specifier segfault
lysnikolaou Oct 23, 2024
99184cc
Add __match_args__ to Interpolation
lysnikolaou Oct 23, 2024
4e89c87
Fix syntax error prefix in t-strings
lysnikolaou Oct 23, 2024
6bfb095
Fix test_dis
lysnikolaou Oct 23, 2024
560e957
Fix test_fstring
lysnikolaou Oct 23, 2024
4d172c2
Fix test_embed
lysnikolaou Oct 23, 2024
c9362e3
Implement interleaving, hash functions and *args __init__
lysnikolaou Oct 23, 2024
d2bd673
Use Argument Clinic for interpolation_new
lysnikolaou Oct 25, 2024
7ae1b60
Revert to using different grammar rules for tstring due to format_spec
lysnikolaou Oct 25, 2024
5e7304f
Fix code generation for when there's many values in tstring
lysnikolaou Oct 25, 2024
c8f22f0
Remove duplicate parens from template repr
lysnikolaou Oct 25, 2024
b0dbe27
Fix conversion converter for interpolation __new__
lysnikolaou Oct 25, 2024
b07c419
Merge branch 'main' into tstrings
lysnikolaou Oct 27, 2024
3e84771
Fix test_embed and test_dis -- again
lysnikolaou Nov 1, 2024
9380ab9
Implement implicit concatenation with TemplateStr
lysnikolaou Nov 1, 2024
22f9dc6
Merge branch 'main' into tstrings
lysnikolaou Feb 20, 2025
f207637
Implement new specification of template type
lysnikolaou Feb 20, 2025
29b1386
Replace nb_add with sq_concat
lysnikolaou Feb 20, 2025
aad5d67
Correctly implement str + tstring
lysnikolaou Feb 21, 2025
77822be
Make changes requested by Steering Council (#57)
Wingysam Apr 9, 2025
f3ba0fe
Merge branch 'main' into tstrings
lysnikolaou Apr 9, 2025
422a515
Remove unnecessary _Py_TPFLAGS_MATCH_SELF from Template type (#60)
koxudaxi Apr 9, 2025
bb7359a
Fix memory leak in template concat error handling (#59)
koxudaxi Apr 9, 2025
6a7b448
PEP 750 bug and spec fix (#58)
davepeck Apr 9, 2025
e6d01ef
Fix for segv in `_PyInterpolation_FromStackRefSteal` (#61)
davepeck Apr 10, 2025
0801f17
More minor fixes after merge with main
lysnikolaou Apr 10, 2025
a51e954
Fix JIT build
lysnikolaou Apr 10, 2025
76816a1
Fix Windows build
lysnikolaou Apr 10, 2025
2c53f55
Remove smelly _conversion_converter symbol
lysnikolaou Apr 10, 2025
fa4b531
Fix check-c-globals
lysnikolaou Apr 10, 2025
98e5d83
Fix various stuff in the VM
lysnikolaou Apr 11, 2025
a7c57fe
Add tests
lysnikolaou Apr 11, 2025
d6c470c
Fix Windows build (#64)
AA-Turner Apr 11, 2025
101abe3
Merge branch 'main' into tstrings
lysnikolaou Apr 11, 2025
7573346
Fix lint
lysnikolaou Apr 11, 2025
4f91ea3
Skip test_tstring.py from ruff
lysnikolaou Apr 11, 2025
14d9fa4
Fix parser bug and implement ast_unparse in Python
lysnikolaou Apr 11, 2025
bf89325
Modify CODEOWNERS
lysnikolaou Apr 11, 2025
b7edcd6
Do not use _PyAST_ExprAsUnicode (unparse) for interpolation expression
lysnikolaou Apr 11, 2025
99c3bf4
Remove interpolation_compare() and interpolation_hash() (#65)
davepeck Apr 17, 2025
f09d99f
Add type check macros (#67)
AA-Turner Apr 17, 2025
878eb69
t-strings: cosmetics (#66)
AA-Turner Apr 17, 2025
7ed73fa
Implement GC protocol for template and interpolation types
lysnikolaou Apr 17, 2025
e88d111
Fix test_unparse again!
lysnikolaou Apr 17, 2025
92fd148
Remove unnecessary tp_flag from interpolation type
lysnikolaou Apr 17, 2025
c833f6d
Merge branch 'main' into tstrings
lysnikolaou Apr 17, 2025
8f8a414
Minor fixes (mostly cosmetic)
lysnikolaou Apr 17, 2025
6bfc888
Add test for interleaving
lysnikolaou Apr 17, 2025
0ee90f9
Merge branch 'main' into tstrings
terryjreedy Apr 18, 2025
e6da757
t-strings: Add more syntax tests (#68)
AA-Turner Apr 18, 2025
58b7e5c
Move string.templatelib specific tests to test_string/test_templatlib…
AA-Turner Apr 19, 2025
4c6cb47
Add more concatenation tests (#71)
AA-Turner Apr 19, 2025
5782897
t-strings: Reduce undefined behaviour and general improvements (#72)
AA-Turner Apr 19, 2025
fb68d60
Add ``assertTStringEqual()`` helper (#73)
AA-Turner Apr 21, 2025
174b045
Add a what's new entry (#69)
pauleveritt Apr 21, 2025
62ace68
Fix repr
lysnikolaou Apr 21, 2025
4f6c5df
Fix syntax errors in grammar
lysnikolaou Apr 21, 2025
e84778a
Rename f helper to fstring
lysnikolaou Apr 21, 2025
8b24ff3
Use Py_BuildValue and add error checking for _PyType_GetDict
lysnikolaou Apr 21, 2025
23444df
Return a strong ref from _PyInterpolation_GetValue
lysnikolaou Apr 21, 2025
19aad93
Remove unnecessary function for template concatenation
lysnikolaou Apr 21, 2025
adbc109
Formatting
lysnikolaou Apr 21, 2025
a5e3bde
Add versionadded directive to TSTRING_START docs
lysnikolaou Apr 21, 2025
c8a9482
Add TOK_GET_STRING_PREFIX macro and change tstring field to enum
lysnikolaou Apr 21, 2025
4e32db6
Merge branch 'main' into tstrings
lysnikolaou Apr 21, 2025
a78c082
Minor fixes to lexer and Makefile
lysnikolaou Apr 21, 2025
173e456
Rename fields in tokenizer mode to not be f-string specific
lysnikolaou Apr 21, 2025
a595ccc
Nit: return result instead of -1
lysnikolaou Apr 22, 2025
2282ccc
Move AST test to test_ast
lysnikolaou Apr 22, 2025
53fc995
Fix unparsing implicit concat of t-strings with f-strings
lysnikolaou Apr 22, 2025
58da4e0
Update CODEOWNERS
lysnikolaou Apr 22, 2025
3e10c49
Clean up test_tstring
lysnikolaou Apr 22, 2025
2addef7
Add new tokens TSTRING_MIDDLE & TSTRING_END
lysnikolaou Apr 22, 2025
550fd07
Merge branch 'main' into tstrings
lysnikolaou Apr 22, 2025
06c15de
Make Interpolation.conversion an int and change bytecode def
lysnikolaou Apr 22, 2025
f57647e
Fix ast_unparse and token generator
lysnikolaou Apr 22, 2025
a21391a
Let compiler build template tuples
lysnikolaou Apr 22, 2025
1dca604
Add tp_clear slot and remove UB casts
lysnikolaou Apr 22, 2025
011ca83
Apply Hugo's suggestions from code review
lysnikolaou Apr 22, 2025
1379756
Add empty line to CODEOWNERS
lysnikolaou Apr 22, 2025
4d1f2a5
Add news blurb
lysnikolaou Apr 22, 2025
8842739
t-strings: Various changes (#74)
AA-Turner Apr 23, 2025
cf56234
Merge branch 'main' into tstrings
lysnikolaou Apr 23, 2025
280e90e
Merge branch 'main' into tstrings
AA-Turner Apr 23, 2025
051e8eb
Add pickle support for tstrings (#75)
sobolevn Apr 23, 2025
aa7c603
Merge branch 'main' into tstrings
lysnikolaou Apr 23, 2025
ce78496
Address feedback; fix template_values_get and conversion
lysnikolaou Apr 23, 2025
8468e10
Merge branch 'main' into tstrings
lysnikolaou Apr 24, 2025
9cee9ff
Merge branch 'main' into tstrings
lysnikolaou Apr 25, 2025
0303c25
Add snippets to Lib/test/test_ast/snippets.py
lysnikolaou Apr 25, 2025
6edaba0
Merge branch 'main' into tstrings
sobolevn Apr 28, 2025
ab9026f 8000
Remove _templatelib extension
lysnikolaou Apr 29, 2025
48f771b
Change __reduce__ flags to METH_NOARGS
lysnikolaou Apr 29, 2025
0b1aef9
Merge branch 'main' into tstrings
lysnikolaou Apr 29, 2025
8b0c132
Fix docs for TSTRING_MIDDLE
lysnikolaou Apr 30, 2025
b35ba13
Merge branch 'main' into tstrings
lysnikolaou Apr 30, 2025
44749c7
Fix ERROR_IF
lysnikolaou Apr 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
t-strings: Various changes (#74)
  • Loading branch information
AA-Turner authored Apr 23, 2025
commit 884273909b43af6f136d0ffc550f0753bd04d434
6 changes: 3 additions & 3 deletions Doc/library/token.rst
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ The token constants are:

.. data:: TSTRING_START

Token value used to indicate the beginning of a t-string literal.
Token value used to indicate the beginning of a template string literal.

.. impl-detail::

Expand All @@ -144,7 +144,7 @@ The token constants are:

.. data:: TSTRING_MIDDLE

Token value used for literal text inside a t-string literal
Token value used for literal text inside a template string literal
including format specifications.

.. impl-detail::
Expand All @@ -158,7 +158,7 @@ The token constants are:

.. data:: TSTRING_END

Token value used to indicate the end of a t-string.
Token value used to indicate the end of a template string literal.

.. impl-detail::

Expand Down
3 changes: 2 additions & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ safe shell operations, improve logging, tackle modern ideas in web development
See :pep:`750` for more details.

(Contributed by Jim Baker, Guido van Rossum, Paul Everitt, Koudai Aono,
Lysandros Nikolaou, and Dave Peck in :gh:`132661`.)
Lysandros Nikolaou, Dave Peck, Adam Turner, Jelle Zijlstra, Bénédikt Tran,
and Pablo Galindo Salgado in :gh:`132661`.)

.. _whatsnew314-pep768:

Expand Down
2 changes: 0 additions & 2 deletions Include/internal/pycore_interpolation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ extern "C" {
# error "this header requires Py_BUILD_CORE define"
#endif

#include "pycore_stackref.h" // _PyStackRef

extern PyTypeObject _PyInterpolation_Type;

#define _PyInterpolation_CheckExact(op) Py_IS_TYPE((op), &_PyInterpolation_Type)
Expand Down
16 changes: 8 additions & 8 deletions Lib/_ast_unparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,11 @@ def _write_str_avoiding_backslashes(self, string, *, quote_types=_ALL_QUOTES):
quote_type = quote_types[0]
self.write(f"{quote_type}{string}{quote_type}")

def _ftstring_helper(self, ftstring_parts):
new_ftstring_parts = []
def _ftstring_helper(self, parts):
new_parts = []
quote_types = list(_ALL_QUOTES)
fallback_to_repr = False
for value, is_constant in ftstring_parts:
for value, is_constant in parts:
if is_constant:
value, new_quote_types = self._str_literal_helper(
value,
Expand All @@ -596,22 +596,22 @@ def _ftstring_helper(self, ftstring_parts):
new_quote_types = [q for q in quote_types if q not in value]
if new_quote_types:
quote_types = new_quote_types
new_ftstring_parts.append(value)
new_parts.append(value)

if fallback_to_repr:
# If we weren't able to find a quote type that works for all parts
# of the JoinedStr, fallback to using repr and triple single quotes.
quote_types = ["'''"]
new_ftstring_parts.clear()
for value, is_constant in ftstring_parts:
new_parts.clear()
for value, is_constant in parts:
if is_constant:
value = repr('"' + value) # force repr to use single quotes
expected_prefix = "'\""
assert value.startswith(expected_prefix), repr(value)
value = value[len(expected_prefix):-1]
new_ftstring_parts.append(value)
new_parts.append(value)

value = "".join(new_ftstring_parts)
value = "".join(new_parts)
quote_type = quote_types[0]
self.write(f"{quote_type}{value}{quote_type}")

Expand Down
4 changes: 2 additions & 2 deletions Lib/tokenize.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def untokenize(self, iterable):
self.tokens.append(indent)
self.prev_col = len(indent)
startline = False
elif tok_type in (FSTRING_MIDDLE, TSTRING_MIDDLE):
elif tok_type in {FSTRING_MIDDLE, TSTRING_MIDDLE}:
if '{' in token or '}' in token:
token = self.escape_brackets(token)
last_line = token.splitlines()[-1]
Expand Down Expand Up @@ -308,7 +308,7 @@ def compat(self, token, iterable):
elif startline and indents:
toks_append(indents[-1])
startline = False
elif toknum in (FSTRING_MIDDLE, TSTRING_MIDDLE):
elif toknum in {FSTRING_MIDDLE, TSTRING_MIDDLE}:
tokval = self.escape_brackets(tokval)

# Insert a space between two consecutive brackets if we are in an f-string
Expand Down
3 changes: 2 additions & 1 deletion Objects/interpolationobject.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* t-string Interpolation object implementation */

#include "Python.h"
#include "pycore_initconfig.h" // _PyStatus_OK
#include "pycore_initconfig.h" // _PyStatus_OK
#include "pycore_interpolation.h"
#include "pycore_typeobject.h" // _PyType_GetDict

static int
_conversion_converter(PyObject *arg, PyObject **conversion)
Expand Down
3 changes: 3 additions & 0 deletions Objects/templateobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "Python.h"
#include "pycore_interpolation.h" // _PyInterpolation_CheckExact()
#include "pycore_runtime.h" // _Py_STR()
#include "pycore_template.h"

typedef struct {
Expand Down Expand Up @@ -155,13 +156,15 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
}
else if (_PyInterpolation_CheckExact(item)) {
if (!last_was_str) {
_Py_DECLARE_STR(empty, "");
PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty));
}
PyTuple_SET_ITEM(interpolations, interpolationsidx++, Py_NewRef(item));
last_was_str = 0;
}
}
if (!last_was_str) {
_Py_DECLARE_STR(empty, "");
PyTuple_SET_ITEM(strings, stringsidx++, &_Py_STR(empty));
}

Expand Down
2 changes: 1 addition & 1 deletion Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "pycore_pyhash.h" // _Py_HashSecret_t
#include "pycore_pylifecycle.h" // _Py_SetFileSystemEncoding()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_template.h" // _PyTemplate_Type
#include "pycore_template.h" // _PyTemplate_Concat()
#include "pycore_tuple.h" // _PyTuple_FromArray()
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
Expand Down
4 changes: 2 additions & 2 deletions Python/bytecodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "pycore_emscripten_signal.h" // _Py_CHECK_EMSCRIPTEN_SIGNALS
#include "pycore_function.h"
#include "pycore_instruments.h"
#include "pycore_interpolation.h" // _PyInterpolation_FromStackRefStealOnSuccess()
#include "pycore_interpolation.h" // _PyInterpolation_Build()
#include "pycore_intrinsics.h"
#include "pycore_long.h" // _PyLong_GetZero()
#include "pycore_moduleobject.h" // PyModuleObject
Expand All @@ -31,7 +31,7 @@
#include "pycore_setobject.h" // _PySet_NextEntry()
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
#include "pycore_stackref.h"
#include "pycore_template.h" // _PyTemplate_From{List,Values}()
#include "pycore_template.h" // _PyTemplate_Build()
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_typeobject.h" // _PySuper_Lookup()

Expand Down
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "pycore_import.h" // _PyImport_IsDefaultImportFunc()
#include "pycore_instruments.h"
#include "pycore_interpframe.h" // _PyFrame_SetStackPointer()
#include "pycore_interpolation.h" // _PyInterpolation_FromStackRefStealOnSuccess()
#include "pycore_interpolation.h" // _PyInterpolation_Build()
#include "pycore_intrinsics.h"
#include "pycore_jit.h"
#include "pycore_list.h" // _PyList_GetItemRef()
Expand All @@ -37,7 +37,7 @@
#include "pycore_setobject.h" // _PySet_Update()
#include "pycore_sliceobject.h" // _PyBuildSlice_ConsumeRefs
#include "pycore_sysmodule.h" // _PySys_GetOptionalAttrString()
#include "pycore_template.h" // _PyTemplate_From{List,Values}()
#include "pycore_template.h" // _PyTemplate_Build()
#include "pycore_traceback.h" // _PyTraceBack_FromFrame
#include "pycore_tuple.h" // _PyTuple_ITEMS()
#include "pycore_uop_ids.h" // Uops
Expand Down
2 changes: 0 additions & 2 deletions Tools/cases_generator/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,6 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
"_PyFrame_StackPush",
"_PyFunction_SetVersion",
"_PyGen_GetGeneratorFromFrame",
"_PyInterpolation_FromStackRefStealOnSuccess",
"_PyInterpreterState_GET",
"_PyList_AppendTakeRef",
"_PyList_ITEMS",
Expand All @@ -649,7 +648,6 @@ def has_error_without_pop(op: parser.CodeDef) -> bool:
"_PyObject_InlineValues",
"_PyObject_IsUniquelyReferenced",
"_PyObject_ManagedDictPointer",
"_PyTemplate_FromList",
"_PyThreadState_HasStackSpace",
"_PyTuple_FromStackRefStealOnSuccess",
"_PyTuple_ITEMS",
Expand Down
Loading
0