Skip to content

Commit 5d5e187

Browse files
committed
Disallow explicit concat between Template and str
1 parent 8594d2c commit 5d5e187

File tree

3 files changed

+13
-100
lines changed

3 files changed

+13
-100
lines changed

Lib/test/test_tstring.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ def test_template_concatenation(self):
161161

162162
# Test template + string
163163
t1 = t"Hello"
164-
combined = t1 + ", world"
165-
self.assertTStringEqual(combined, ("Hello, world",), ())
166-
self.assertEqual(fstring(combined), "Hello, world")
164+
with self.assertRaises(TypeError):
165+
_ = t1 + ", world"
167166

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

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

181179
def test_nested_templates(self):
182180
# Test a template inside another template expression

Objects/templateobject.c

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -245,54 +245,6 @@ template_iter(PyObject *op)
245245
return (PyObject *)iter;
246246
}
247247

248-
static PyObject *
249-
template_strings_append_str(PyObject *strings, PyObject *str)
250-
{
251-
Py_ssize_t stringslen = PyTuple_GET_SIZE(strings);
252-
PyObject *string = PyTuple_GET_ITEM(strings, stringslen - 1);
253-
PyObject *concat = PyUnicode_Concat(string, str);
254-
if (concat == NULL) {
255-
return NULL;
256-
}
257-
258-
PyObject *newstrings = PyTuple_New(stringslen);
259-
if (newstrings == NULL) {
260-
Py_DECREF(concat);
261-
return NULL;
262-
}
263-
264-
for (Py_ssize_t i = 0; i < stringslen - 1; i++) {
265-
PyTuple_SET_ITEM(newstrings, i, Py_NewRef(PyTuple_GET_ITEM(strings, i)));
266-
}
267-
PyTuple_SET_ITEM(newstrings, stringslen - 1, concat);
268-
269-
return newstrings;
270-
}
271-
272-
static PyObject *
273-
template_strings_prepend_str(PyObject *strings, PyObject *str)
274-
{
275-
Py_ssize_t stringslen = PyTuple_GET_SIZE(strings);
276-
PyObject *string = PyTuple_GET_ITEM(strings, 0);
277-
PyObject *concat = PyUnicode_Concat(str, string);
278-
if (concat == NULL) {
279-
return NULL;
280-
}
281-
282-
PyObject *newstrings = PyTuple_New(stringslen);
283-
if (newstrings == NULL) {
284-
Py_DECREF(concat);
285-
return NULL;
286-
}
287-
288-
PyTuple_SET_ITEM(newstrings, 0, concat);
289-
for (Py_ssize_t i = 1; i < stringslen; i++) {
290-
PyTuple_SET_ITEM(newstrings, i, Py_NewRef(PyTuple_GET_ITEM(strings, i)));
291-
}
292-
293-
return newstrings;
294-
}
295-
296248
static PyObject *
297249
template_strings_concat(PyObject *left, PyObject *right)
298250
{
@@ -344,46 +296,16 @@ template_concat_templates(templateobject *self, templateobject *other)
344296
return newtemplate;
345297
}
346298

347-
static PyObject *
348-
template_concat_template_str(templateobject *self, PyObject *other)
349-
{
350-
PyObject *newstrings = template_strings_append_str(self->strings, other);
351-
if (newstrings == NULL) {
352-
return NULL;
353-
}
354-
355-
PyObject *newtemplate = _PyTemplate_Build(newstrings, self->interpolations);
356-
Py_DECREF(newstrings);
357-
return newtemplate;
358-
}
359-
360-
static PyObject *
361-
template_concat_str_template(templateobject *self, PyObject *other)
362-
{
363-
PyObject *newstrings = template_strings_prepend_str(self->strings, other);
364-
if (newstrings == NULL) {
365-
return NULL;
366-
}
367-
368-
PyObject *newtemplate = _PyTemplate_Build(newstrings, self->interpolations);
369-
Py_DECREF(newstrings);
370-
return newtemplate;
371-
}
372-
373299
PyObject *
374300
_PyTemplate_Concat(PyObject *self, PyObject *other)
375301
{
376302
if (_PyTemplate_CheckExact(self) && _PyTemplate_CheckExact(other)) {
377303
return template_concat_templates((templateobject *) self, (templateobject *) other);
378-
}
379-
else if ((_PyTemplate_CheckExact(self)) && PyUnicode_Check(other)) {
380-
return template_concat_template_str((templateobject *) self, other);
381-
}
382-
else if (PyUnicode_Check(self) && (_PyTemplate_CheckExact(other))) {
383-
return template_concat_str_template((templateobject *) other, self);
384-
}
385-
else {
386-
Py_RETURN_NOTIMPLEMENTED;
304+
} else {
305+
PyErr_Format(PyExc_TypeError,
306+
"can only concatenate Template (not \"%.200s\") to Template",
307+
Py_TYPE(other)->tp_name);
308+
return NULL;
387309
}
388310
}
389311

Objects/unicodeobject.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5656
#include "pycore_pyhash.h" // _Py_HashSecret_t
5757
#include "pycore_pylifecycle.h" // _Py_SetFileSystemEncoding()
5858
#include "pycore_pystate.h" // _PyInterpreterState_GET()
59-
#include "pycore_template.h" // _PyTemplate_Concat()
6059
#include "pycore_tuple.h" // _PyTuple_FromArray()
6160
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
6261
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
@@ -11610,16 +11609,10 @@ PyUnicode_Concat(PyObject *left, PyObject *right)
1161011609
return NULL;
1161111610

1161211611
if (!PyUnicode_Check(right)) {
11613-
if (_PyTemplate_CheckExact(right)) {
11614-
// str + tstring is implemented in the tstring type
11615-
return _PyTemplate_Concat(left, right);
11616-
}
11617-
else {
11618-
PyErr_Format(PyExc_TypeError,
11619-
"can only concatenate str (not \"%.200s\") to str",
11620-
Py_TYPE(right)->tp_name);
11621-
return NULL;
11622-
}
11612+
PyErr_Format(PyExc_TypeError,
11613+
"can only concatenate str (not \"%.200s\") to str",
11614+
Py_TYPE(right)->tp_name);
11615+
return NULL;
1162311616
}
1162411617

1162511618
/* Shortcuts */

0 commit comments

Comments
 (0)