@@ -245,10 +245,10 @@ template_interpolations_concat(PyObject *left, PyObject *right) {
245
245
}
246
246
247
247
static PyObject *
248
- template_add_template_str ( templateobject * template , PyObject * str )
248
+ template_strings_append_str ( PyObject * strings , PyObject * str )
249
249
{
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 );
252
252
PyObject * concat = PyUnicode_Concat (string , str );
253
253
if (concat == NULL ) {
254
254
return NULL ;
@@ -261,25 +261,18 @@ template_add_template_str(templateobject *template, PyObject *str)
261
261
}
262
262
263
263
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 )));
265
265
}
266
266
PyTuple_SET_ITEM (newstrings , stringslen - 1 , concat );
267
267
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 ;
276
269
}
277
270
278
271
static PyObject *
279
- template_add_str_template ( templateobject * template , PyObject * str )
272
+ template_strings_prepend_str ( PyObject * strings , PyObject * str )
280
273
{
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 );
283
276
PyObject * concat = PyUnicode_Concat (str , string );
284
277
if (concat == NULL ) {
285
278
return NULL ;
@@ -293,26 +286,19 @@ template_add_str_template(templateobject *template, PyObject *str)
293
286
294
287
PyTuple_SET_ITEM (newstrings , 0 , concat );
295
288
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 )));
304
290
}
305
291
306
- return template_from_strings_interpolations ( Py_TYPE ( template ), newstrings , newinterpolations ) ;
292
+ return newstrings ;
307
293
}
308
294
309
295
static PyObject *
310
- template_add_templates ( templateobject * self , templateobject * other )
296
+ template_strings_concat ( PyObject * left , PyObject * right )
311
297
{
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 );
316
302
317
303
PyObject * concat = PyUnicode_Concat (left_laststring , right_firststring );
318
304
if (concat == NULL ) {
@@ -327,16 +313,26 @@ template_add_templates(templateobject *self, templateobject *other)
327
313
328
314
Py_ssize_t index = 0 ;
329
315
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 )));
331
317
}
332
318
PyTuple_SET_ITEM (newstrings , index ++ , concat );
333
319
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 ;
335
332
}
336
333
337
334
PyObject * newinterpolations = template_interpolations_concat (self -> interpolations , other -> interpolations );
338
335
if (newinterpolations == NULL ) {
339
- // No need to decref concat here since it's in newstrings
340
336
Py_DECREF (newstrings );
341
337
return NULL ;
342
338
}
@@ -345,17 +341,51 @@ template_add_templates(templateobject *self, templateobject *other)
345
341
}
346
342
347
343
static 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 )
349
379
{
350
380
if (PyObject_TypeCheck (self , & _PyTemplate_Type ) &&
351
381
PyObject_TypeCheck (other , & _PyTemplate_Type )) {
352
- return template_add_templates ((templateobject * ) self , (templateobject * ) other );
382
+ return template_concat_templates ((templateobject * ) self , (templateobject * ) other );
353
383
}
354
384
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 );
356
386
}
357
387
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 );
359
389
}
360
390
else {
361
391
Py_RETURN_NOTIMPLEMENTED ;
@@ -402,8 +432,8 @@ static PyGetSetDef template_getset[] = {
402
432
{NULL },
403
433
};
404
434
405
- static PyNumberMethods template_as_number = {
406
- .nb_add = template_add ,
435
+ static PySequenceMethods template_as_sequence = {
436
+ .sq_concat = template_concat ,
407
437
};
408
438
409
439
PyTypeObject _PyTemplate_Type = {
@@ -413,7 +443,7 @@ PyTypeObject _PyTemplate_Type = {
413
443
.tp_basicsize = sizeof (templateobject ),
414
444
.tp_itemsize = 0 ,
415
445
.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 ,
417
447
.tp_new = (newfunc ) template_new ,
418
448
.tp_dealloc = (destructor ) template_dealloc ,
419
449
.tp_repr = (reprfunc ) template_repr ,
0 commit comments