10000 gh-132661: Disallow `Template`/`str` concatenation after PEP 750 spec update by davepeck · Pull Request #135996 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-132661: Disallow Template/str concatenation after PEP 750 spec update #135996

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

Open
wants to merge 36 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
5d5e187
Disallow explicit concat between Template and str
davepeck Ju 8000 n 26, 2025
1cc778f
First pass at removing implicit Template/str concat
davepeck Jun 26, 2025
7ea498c
Merge branch 'main' into pep750-concat-update
davepeck Jun 30, 2025
d08c8b8
Merge branch 'main' into pep750-concat-update
davepeck Jul 8, 2025
c662041
Remove extraneous newline
davepeck Jul 8, 2025
b17b7c9
Add expected exception messages to tests
davepeck Jul 8, 2025
a0c1bb6
Remove _ast_unparse t/f implicit concat helper code
davepeck Jul 8, 2025
23988e8
Merge branch 'main' into pep750-concat-update
davepeck Jul 8, 2025
d7eadec
Add a parallel comment
davepeck Jul 8, 2025
cb1ad63
Add news blurb for PR.
davepeck Jul 8, 2025
741eaf2
One day, I may develop RST muscle memory.
davepeck Jul 8, 2025
8aeb512
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
bf5447b
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
7e7ea5a
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
2f79337
Two minor tweaks
davepeck Jul 9, 2025
dacafdb
Use subtests; test both directions.
davepeck Jul 9, 2025
b272287
Update Objects/templateobject.c
davepeck Jul 9, 2025
ffae8e9
Update Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-22-08.gh-issu…
davepeck Jul 9, 2025
c6ca45a
Fix bug in suggested change
davepeck Jul 9, 2025
091a2a9
Merge branch 'main' into pep750-concat-update
davepeck Jul 9, 2025
860caea
First pass at update to grammar and action_helpers.c
davepeck Jul 10, 2025
6a6f734
Merge branch 'main' into pep750-concat-update
davepeck Jul 10, 2025
860f81d
Simplify grammar
davepeck Jul 10, 2025
e0fe608
Use KNOWN_RANGE for string/t-string mix error messages
davepeck Jul 10, 2025
c9ed06b
Merge branch 'pep750-concat-update' of github.com:t-strings/cpython i…
davepeck Jul 10, 2025
3ebdd97
Merge branch 'main' into pep750-concat-update
davepeck Jul 10, 2025
ef1cd5c
Clean up ast_unparse.c append_tstring()
davepeck Jul 10, 2025
055af82
Merge branch 'main' into pep750-concat-update
davepeck Jul 15, 2025
ab393f7
Update Misc/NEWS.d/next/Core_and_Builtins/2025-07-08-23-22-08.gh-issu…
davepeck Jul 15, 2025
16e6a1f
Update Objects/templateobject.c
davepeck Jul 15, 2025
b81a807
Update error messages; add an assert(0)
davepeck Jul 15, 2025
b9bac0e
Simplify ast_unparase.c further
davepeck Jul 15, 2025
6b6acda
Fix news dangling reference (for now).
davepeck Jul 15, 2025
e5b03e8
Merge branch 'pep750-concat-update' of github.com:t-strings/cpython i…
davepeck Jul 15, 2025
6ea3af3
Fix exception assert message checks
davepeck Jul 16, 2025
5037f89
Remove no-longer used path in codegen.c
davepeck Jul 17, 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
Next Next commit
Disallow explicit concat between Template and str
  • Loading branch information
davepeck committed Jun 26, 2025
commit 5d5e187ba8015a0db94163e6f845aee4cfbd55cd
10 changes: 4 additions & 6 deletions Lib/test/test_tstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,8 @@ def test_template_concatenation(self):

# Test template + string
t1 = t"Hello"
combined = t1 + ", world"
self.assertTStringEqual(combined, ("Hello, world",), ())
self.assertEqual(fstring(combined), "Hello, world")
with self.assertRaises(TypeError):
_ = t1 + ", world"

# Test template + template with interpolation
name = "Python"
Expand All @@ -174,9 +173,8 @@ def test_template_concatenation(self):
self.assertEqual(fstring(combined), "Hello, Python")

# Test string + template
t = "Hello, " + t"{name}"
self.assertTStringEqual(t, ("Hello, ", ""), [(name, "name")])
self.assertEqual(fstring(t), "Hello, Python")
with self.assertRaises(TypeError):
_ = "Hello, " + t"{name}"

def test_nested_templates(self):
# Test a template inside another template expression
Expand Down
88 changes: 5 additions & 83 deletions Objects/templateobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,54 +245,6 @@ template_iter(PyObject *op)
return (PyObject *)iter;
}

static PyObject *
template_strings_append_str(PyObject *strings, PyObject *str)
{
Py_ssize_t stringslen = PyTuple_GET_SIZE(strings);
PyObject *string = PyTuple_GET_ITEM(strings, stringslen - 1);
PyObject *concat = PyUnicode_Concat(string, str);
if (concat == NULL) {
return NULL;
}

PyObject *newstrings = PyTuple_New(stringslen);
if (newstrings == NULL) {
Py_DECREF(concat);
return NULL;
}

for (Py_ssize_t i = 0; i < stringslen - 1; i++) {
PyTuple_SET_ITEM(newstrings, i, Py_NewRef(PyTuple_GET_ITEM(strings, i)));
}
PyTuple_SET_ITEM(newstrings, stringslen - 1, concat);

return newstrings;
}

static PyObject *
template_strings_prepend_str(PyObject *strings, PyObject *str)
{
Py_ssize_t stringslen = PyTuple_GET_SIZE(strings);
PyObject *string = PyTuple_GET_ITEM(strings, 0);
PyObject *concat = PyUnicode_Concat(str, string);
if (concat == NULL) {
return NULL;
}

PyObject *newstrings = PyTuple_New(stringslen);
if (newstrings == NULL) {
Py_DECREF(concat);
return NULL;
}

PyTuple_SET_ITEM(newstrings, 0, concat);
for (Py_ssize_t i = 1; i < stringslen; i++) {
PyTuple_SET_ITEM(newstrings, i, Py_NewRef(PyTuple_GET_ITEM(strings, i)));
}

return newstrings;
}

static PyObject *
template_strings_concat(PyObject *left, PyObject *right)
{
Expand Down Expand Up @@ -344,46 +296,16 @@ template_concat_templates(templateobject *self, templateobject *other)
return newtemplate;
}

static PyObject *
template_concat_template_str(templateobject *self, PyObject *other)
{
PyObject *newstrings = template_strings_append_str(self->strings, other);
if (newstrings == NULL) {
return NULL;
}

PyObject *newtemplate = _PyTemplate_Build(newstrings, self->interpolations);
Py_DECREF(newstrings);
return newtemplate;
}

static PyObject *
template_concat_str_template(templateobject *self, PyObject *other)
{
PyObject *newstrings = template_strings_prepend_str(self->strings, other);
if (newstrings == NULL) {
return NULL;
}

PyObject *newtemplate = _PyTemplate_Build(newstrings, self->interpolations);
Py_DECREF(newstrings);
return newtemplate;
}

PyObject *
_PyTemplate_Concat(PyObject *self, PyObject *other)
{
if (_PyTemplate_CheckExact(self) && _PyTemplate_CheckExact(other)) {
return template_concat_templates((templateobject *) self, (templateobject *) other);
}
else if ((_PyTemplate_CheckExact(self)) && PyUnicode_Check(other)) {
return template_concat_template_str((templateobject *) self, other);
}
else if (PyUnicode_Check(self) && (_PyTemplate_CheckExact(other))) {
return template_concat_str_template((templateobject *) other, self);
}
else {
Py_RETURN_NOTIMPLEMENTED;
} else {
PyErr_Format(PyExc_TypeError,
"can only concatenate Template (not \"%.200s\") to Template",
Py_TYPE(other)->tp_name);
return NULL;
}
}

Expand Down
15 changes: 4 additions & 11 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ 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_Concat()
#include "pycore_tuple.h" // _PyTuple_FromArray()
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
Expand Down Expand Up @@ -11610,16 +11609,10 @@ PyUnicode_Concat(PyObject *left, PyObject *right)
return NULL;

if (!PyUnicode_Check(right)) {
if (_PyTemplate_CheckExact(right)) {
// str + tstring is implemented in the tstring type
return _PyTemplate_Concat(left, right);
}
else {
PyErr_Format(PyExc_TypeError,
"can only concatenate str (not \"%.200s\") to str",
Py_TYPE(right)->tp_name);
return NULL;
}
PyErr_Format(PyExc_TypeError,
"can only concatenate str (not \"%.200s\") to str",
Py_TYPE(right)->tp_name);
return NULL;
}

/* Shortcuts */
Expand Down
0