Skip to content

Commit aad5d67

Browse files
committed
Correctly implement str + tstring
1 parent 29b1386 commit aad5d67

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

Include/internal/pycore_template.h

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ extern "C" {
1414
extern PyTypeObject _PyTemplate_Type;
1515
extern PyTypeObject _PyTemplateIter_Type;
1616

17+
extern PyObject *_PyTemplate_Concat(PyObject *self, PyObject *other);
18+
1719
PyAPI_FUNC(PyObject *) _PyTemplate_FromValues(PyObject **values, Py_ssize_t n);
1820
PyAPI_FUNC(PyObject *) _PyTemplate_FromListStackRef(_PyStackRef ref);
1921

Objects/templateobject.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ template_concat_templates(templateobject *self, templateobject *other)
341341
}
342342

343343
static PyObject *
344-
template_concat_template_str(templateobject *self, PyObject *str)
344+
template_concat_template_str(templateobject *self, PyObject *other)
345345
{
346-
PyObject *newstrings = template_strings_append_str(self->strings, str);
346+
PyObject *newstrings = template_strings_append_str(self->strings, other);
347347
if (newstrings == NULL) {
348348
return NULL;
349349
}
@@ -358,9 +358,9 @@ template_concat_template_str(templateobject *self, PyObject *str)
358358
}
359359

360360
static PyObject *
361-
template_concat_str_template(templateobject *self, PyObject *str)
361+
template_concat_str_template(templateobject *self, PyObject *other)
362362
{
363-
PyObject *newstrings = template_strings_prepend_str(self->strings, str);
363+
PyObject *newstrings = template_strings_prepend_str(self->strings, other);
364364
if (newstrings == NULL) {
365365
return NULL;
366366
}
@@ -374,8 +374,8 @@ template_concat_str_template(templateobject *self, PyObject *str)
374374
return template_from_strings_interpolations(Py_TYPE(self), newstrings, newinterpolations);
375375
}
376376

377-
static PyObject *
378-
template_concat(PyObject *self, PyObject *other)
377+
PyObject *
378+
_PyTemplate_Concat(PyObject *self, PyObject *other)
379379
{
380380
if (PyObject_TypeCheck(self, &_PyTemplate_Type) &&
381381
PyObject_TypeCheck(other, &_PyTemplate_Type)) {
@@ -433,7 +433,7 @@ static PyGetSetDef template_getset[] = {
433433
};
434434

435435
static PySequenceMethods template_as_sequence = {
436-
.sq_concat = template_concat,
436+
.sq_concat = _PyTemplate_Concat,
437437
};
438438

439439
PyTypeObject _PyTemplate_Type = {

Objects/unicodeobject.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
5858
#include "pycore_ucnhash.h" // _PyUnicode_Name_CAPI
5959
#include "pycore_unicodeobject.h" // struct _Py_unicode_state
6060
#include "pycore_unicodeobject_generated.h" // _PyUnicode_InitStaticStrings()
61+
#include "pycore_template.h" // _PyTemplate_Type
6162

6263
#include "stringlib/eq.h" // unicode_eq()
6364
#include <stddef.h> // ptrdiff_t
@@ -11614,10 +11615,16 @@ PyUnicode_Concat(PyObject *left, PyObject *right)
1161411615
return NULL;
1161511616

1161611617
if (!PyUnicode_Check(right)) {
11617-
PyErr_Format(PyExc_TypeError,
11618-
"can only concatenate str (not \"%.200s\") to str",
11619-
Py_TYPE(right)->tp_name);
11620-
return NULL;
11618+
if (PyObject_TypeCheck(right, &_PyTemplate_Type)) {
11619+
// str + tstring is implemented in the tstring type
11620+
return _PyTemplate_Concat(left, right);
11621+
}
11622+
else {
11623+
PyErr_Format(PyExc_TypeError,
11624+
"can only concatenate str (not \"%.200s\") to str",
11625+
Py_TYPE(right)->tp_name);
11626+
return NULL;
11627+
}
1162111628
}
1162211629

1162311630
/* Shortcuts */

0 commit comments

Comments
 (0)