@@ -68,15 +68,23 @@ typedef struct {
68
68
} templateobject ;
69
69
70
70
static templateobject *
71
- template_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
71
+ template_from_strings_interpolations (PyTypeObject * type , PyObject * strings , PyObject * interpolations )
72
72
{
73
- if ( kwds != NULL ) {
74
- PyErr_SetString ( PyExc_TypeError , "Template.__new__ only accepts *args arguments" );
73
+ templateobject * template = ( templateobject * ) type -> tp_alloc ( type , 0 );
74
+ if ( template == NULL ) {
75
75
return NULL ;
76
76
}
77
77
78
- templateobject * self = (templateobject * ) type -> tp_alloc (type , 0 );
79
- if (!self ) {
78
+ template -> strings = Py_NewRef (strings );
79
+ template -> interpolations = Py_NewRef (interpolations );
80
+ return template ;
81
+ }
82
+
83
+ static templateobject *
84
+ template_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
85
+ {
86
+ if (kwds != NULL ) {
87
+ PyErr_SetString (PyExc_TypeError , "Template.__new__ only accepts *args arguments" );
80
88
return NULL ;
81
89
}
82
90
@@ -101,7 +109,6 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
101
109
last_was_str = 0 ;
102
110
}
103
111
else {
104
- Py_DECREF (self );
105
112
PyErr_SetString (PyExc_TypeError , "Template.__new__ *args need to be of type 'str' or 'Interpolation'" );
106
113
return NULL ;
107
114
}
@@ -112,13 +119,11 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
112
119
113
120
PyObject * strings = PyTuple_New (stringslen );
114
121
if (!strings ) {
115
- Py_DECREF (self );
116
122
return NULL ;
117
123
}
118
124
119
125
PyObject * interpolations = PyTuple_New (interpolationslen );
120
126
if (!interpolations ) {
121
- Py_DECREF (self );
122
127
Py_DECREF (strings );
123
128
return NULL ;
124
129
}
@@ -133,7 +138,9 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
133
138
PyObject * concat = PyUnicode_Concat (laststring , item );
134
139
Py_DECREF (laststring );
135
140
if (!concat ) {
136
- goto error ;
141
+ Py_DECREF (strings );
142
+ Py_DECREF (interpolations );
143
+ return NULL ;
137
144
}
138
145
PyTuple_SET_ITEM (strings , stringsidx - 1 , concat );
139
146
}
@@ -154,15 +161,10 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
154
161
PyTuple_SET_ITEM (strings , stringsidx ++ , & _Py_STR (empty ));
155
162
}
156
163
157
- self -> strings = strings ;
158
- self -> interpolations = interpolations ;
159
- return self ;
160
-
161
- error :
162
- Py_DECREF (self );
164
+ templateobject * template = template_from_strings_interpolations (type , strings , interpolations );
163
165
Py_DECREF (strings );
164
166
Py_DECREF (interpolations );
165
- return NULL ;
167
+ return template ;
166
168
}
167
169
168
170
static void
@@ -219,19 +221,6 @@ template_iter(templateobject *self)
219
221
return iter ;
220
222
}
221
223
222
- static PyObject *
223
- template_from_strings_interpolations (PyTypeObject * type , PyObject * strings , PyObject * interpolations )
224
- {
225
- PyObject * template = type -> tp_alloc (type , 0 );
226
- if (template == NULL ) {
227
- return NULL ;
228
- }
229
-
230
- ((templateobject * ) template )-> strings = Py_NewRef (strings );
231
- ((templateobject * ) template )-> interpolations = Py_NewRef (interpolations );
232
- return template ;
233
- }
234
-
235
224
static PyObject *
236
225
template_interpolations_copy (PyObject * interpolations ) {
237
226
Py_ssize_t interpolationslen = PyTuple_GET_SIZE (interpolations );
@@ -360,12 +349,10 @@ template_concat_templates(templateobject *self, templateobject *other)
360
349
return NULL ;
361
350
}
362
351
363
- PyObject * newtemplate = template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
364
-
352
+ templateobject * newtemplate = template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
365
353
Py_DECREF (newstrings );
366
354
Py_DECREF (newinterpolations );
367
-
368
- return newtemplate ;
355
+ return (PyObject * ) newtemplate ;
369
356
}
370
357
371
358
static PyObject *
@@ -382,12 +369,10 @@ template_concat_template_str(templateobject *self, PyObject *other)
382
369
return NULL ;
383
370
}
384
371
385
- PyObject * newtemplate = template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
386
-
372
+ templateobject * newtemplate = template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
387
373
Py_DECREF (newstrings );
388
374
Py_DECREF (newinterpolations );
389
-
390
- return newtemplate ;
375
+ return (PyObject * ) newtemplate ;
391
376
}
392
377
393
378
static PyObject *
@@ -404,12 +389,10 @@ template_concat_str_template(templateobject *self, PyObject *other)
404
389
return NULL ;
405
390
}
406
391
407
- PyObject * newtemplate = template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
408
-
392
+ templateobject * newtemplate = template_from_strings_interpolations (Py_TYPE (self ), newstrings , newinterpolations );
409
393
Py_DECREF (newstrings );
410
394
Py_DECREF (newinterpolations );
411
-
412
- return newtemplate ;
395
+ return (PyObject * ) newtemplate ;
413
396
}
414
397
415
398
PyObject *
0 commit comments