@@ -245,10 +245,10 @@ template_interpolations_concat(PyObject *left, PyObject *right) {
245245}
246246
247247static PyObject *
248- template_add_template_str ( templateobject * template , PyObject * str )
248+ template_strings_append_str ( PyObject * strings , PyObject * str )
249249{
250- Py_ssize_t stringslen = PyTuple_GET_SIZE (template -> strings );
251- PyObject * string = PyTuple_GET_ITEM (template -> strings , stringslen - 1 );
250+ Py_ssize_t stringslen = PyTuple_GET_SIZE (strings );
251+ PyObject * string = PyTuple_GET_ITEM (strings , stringslen - 1 );
252252 PyObject * concat = PyUnicode_Concat (string , str );
253253 if (concat == NULL ) {
254254 return NULL ;
@@ -261,25 +261,18 @@ template_add_template_str(templateobject *template, PyObject *str)
261261 }
262262
263263 for (Py_ssize_t i = 0 ; i < stringslen - 1 ; i ++ ) {
264- PyTuple_SET_ITEM (newstrings , i , Py_NewRef (PyTuple_GET_ITEM (template -> strings , i )));
264+ PyTuple_SET_ITEM (newstrings , i , Py_NewRef (PyTuple_GET_ITEM (strings , i )));
265265 }
266266 PyTuple_SET_ITEM (newstrings , stringslen - 1 , concat );
267267
268- PyObject * newinterpolations = template_interpolations_copy (template -> interpolations );
269- if (newinterpolations == NULL ) {
270- // No need to decref concat here since it's in newstrings
271- Py_DECREF (newstrings );
272- return NULL ;
273- }
274-
275- return template_from_strings_interpolations (Py_TYPE (template ), newstrings , newinterpolations );
268+ return newstrings ;
276269}
277270
278271static PyObject *
279- template_add_str_template ( templateobject * template , PyObject * str )
272+ template_strings_prepend_str ( PyObject * strings , PyObject * str )
280273{
281- Py_ssize_t stringslen = PyTuple_GET_SIZE (template -> strings );
282- PyObject * string = PyTuple_GET_ITEM (template -> strings , 0 );
274+ Py_ssize_t stringslen = PyTuple_GET_SIZE (strings );
275+ PyObject * string = PyTuple_GET_ITEM (strings , 0 );
283276 PyObject * concat = PyUnicode_Concat (str , string );
284277 if (concat == NULL ) {
285278 return NULL ;
@@ -293,26 +286,19 @@ template_add_str_template(templateobject *template, PyObject *str)
293286
294287 PyTuple_SET_ITEM (newstrings , 0 , concat );
295288 for (Py_ssize_t i = 1 ; i < stringslen ; i ++ ) {
296- PyTuple_SET_ITEM (newstrings , i , Py_NewRef (PyTuple_GET_ITEM (template -> strings , i )));
297- }
298-
299- PyObject * newinterpolations = template_interpolations_copy (template -> interpolations );
300- if (newinterpolations == NULL ) {
301- // No need to decref concat here since it's in newstrings
302- Py_DECREF (newstrings );
303- return NULL ;
289+ PyTuple_SET_ITEM (newstrings , i , Py_NewRef (PyTuple_GET_ITEM (strings , i )));
304290 }
305291
306- return template_from_strings_interpolations ( Py_TYPE ( template ), newstrings , newinterpolations ) ;
292+ return newstrings ;
307293}
308294
309295static PyObject *
310- template_add_templates ( templateobject * self , templateobject * other )
296+ template_strings_concat ( PyObject * left , PyObject * right )
311297{
312- Py_ssize_t left_stringslen = PyTuple_GET_SIZE (self -> strings );
313- PyObject * left_laststring = PyTuple_GET_ITEM (self -> strings , left_stringslen - 1 );
314- Py_ssize_t right_stringslen = PyTuple_GET_SIZE (other -> strings );
315- PyObject * right_firststring = PyTuple_GET_ITEM (other -> strings , 0 );
298+ Py_ssize_t left_stringslen = PyTuple_GET_SIZE (left );
299+ PyObject * left_laststring = PyTuple_GET_ITEM (left , left_stringslen - 1 );
300+ Py_ssize_t right_stringslen = PyTuple_GET_SIZE (right );
301+ PyObject * right_firststring = PyTuple_GET_ITEM (right , 0 );
316302
317303 PyObject * concat = PyUnicode_Concat (left_laststring , right_firststring );
318304 if (concat == NULL ) {
@@ -327,16 +313,26 @@ template_add_templates(templateobject *self, templateobject *other)
327313
328314 Py_ssize_t index = 0 ;
329315 for (Py_ssize_t i = 0 ; i < left_stringslen - 1 ; i ++ ) {
330- PyTuple_SET_ITEM (newstrings , index ++ , Py_NewRef (PyTuple_GET_ITEM (self -> strings , i )));
316+ PyTuple_SET_ITEM (newstrings , index ++ , Py_NewRef (PyTuple_GET_ITEM (left , i )));
331317 }
332318 PyTuple_SET_ITEM (newstrings , index ++ , concat );
333319 for (Py_ssize_t i = 1 ; i < right_stringslen ; i ++ ) {
334- PyTuple_SET_ITEM (newstrings , index ++ , Py_NewRef (PyTuple_GET_ITEM (other -> strings , i )));
320+ PyTuple_SET_ITEM (newstrings , index ++ , Py_NewRef (PyTuple_GET_ITEM (right , i )));
321+ }
322+
323+ return newstrings ;
324+ }
325+
326+ static PyObject *
327+ template_concat_templates (templateobject * self , templateobject * other )
328+ {
329+ PyObject * newstrings = template_strings_concat (self -> strings , other -> strings );
330+ if (newstrings == NULL ) {
331+ return NULL ;
335332 }
336333
337334 PyObject * newinterpolations = template_interpolations_concat (self -> interpolations , other -> interpolations );
338335 if (newinterpolations == NULL ) {
339- // No need to decref concat here since it's in newstrings
340336 Py_DECREF (newstrings );
341337 return NULL ;
342338 }
@@ -345,17 +341,51 @@ template_add_templates(templateobject *self, templateobject *other)
345341}
346342
347343static PyObject *
348- template_add (PyObject * self , PyObject * other )
344+ template_concat_template_str (templateobject * self , PyObject * str )
345+ {
346+ PyObject * newstrings = template_strings_append_str (self -> strings , str );
347+ if (newstrings == NULL ) {
348+ return NULL ;
349+ }
350+
351+ PyObject * newinterpolations = template_interpolations_copy (self -> interpolations );
352+ if (newinterpolations == NULL ) {
353+ Py_DECREF (newstrings );
354+ return NULL ;
355+ }
356+
357+ return template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
358+ }
359+
360+ static PyObject *
361+ template_concat_str_template (templateobject * self , PyObject * str )
362+ {
363+ PyObject * newstrings = template_strings_prepend_str (self -> strings , str );
364+ if (newstrings == NULL ) {
365+ return NULL ;
366+ }
367+
368+ PyObject * newinterpolations = template_interpolations_copy (self -> interpolations );
369+ if (newinterpolations == NULL ) {
370+ Py_DECREF (newstrings );
371+ return NULL ;
372+ }
373+
374+ return template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
375+ }
376+
377+ static PyObject *
378+ template_concat (PyObject * self , PyObject * other )
349379{
350380 if (PyObject_TypeCheck (self , & _PyTemplate_Type ) &&
351381 PyObject_TypeCheck (other , & _PyTemplate_Type )) {
352- return template_add_templates ((templateobject * ) self , (templateobject * ) other );
382+ return template_concat_templates ((templateobject * ) self , (templateobject * ) other );
353383 }
354384 else if (PyObject_TypeCheck (self , & _PyTemplate_Type ) && PyUnicode_Check (other )) {
355- return template_add_template_str ((templateobject * ) self , other );
385+ return template_concat_template_str ((templateobject * ) self , other );
356386 }
357387 else if (PyUnicode_Check (self ) && PyObject_TypeCheck (other , & _PyTemplate_Type )) {
358- return template_add_str_template ((templateobject * ) other , self );
388+ return template_concat_str_template ((templateobject * ) other , self );
359389 }
360390 else {
361391 Py_RETURN_NOTIMPLEMENTED ;
@@ -402,8 +432,8 @@ static PyGetSetDef template_getset[] = {
402432 {NULL },
403433};
404434
405- static PyNumberMethods template_as_number = {
406- .nb_add = template_add ,
435+ static PySequenceMethods template_as_sequence = {
436+ .sq_concat = template_concat ,
407437};
408438
409439PyTypeObject _PyTemplate_Type = {
@@ -413,7 +443,7 @@ PyTypeObject _PyTemplate_Type = {
413443 .tp_basicsize = sizeof (templateobject ),
414444 .tp_itemsize = 0 ,
415445 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | _Py_TPFLAGS_MATCH_SELF ,
416- .tp_as_number = & template_as_number ,
446+ .tp_as_sequence = & template_as_sequence ,
417447 .tp_new = (newfunc ) template_new ,
418448 .tp_dealloc = (destructor ) template_dealloc ,
419449 .tp_repr = (reprfunc ) template_repr ,
0 commit comments