@@ -68,15 +68,23 @@ typedef struct {
6868} templateobject ;
6969
7070static templateobject *
71- template_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
71+ template_from_strings_interpolations (PyTypeObject * type , PyObject * strings , PyObject * interpolations )
7272{
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 ) {
7575 return NULL ;
7676 }
7777
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" );
8088 return NULL ;
8189 }
8290
@@ -101,7 +109,6 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
101109 last_was_str = 0 ;
102110 }
103111 else {
104- Py_DECREF (self );
105112 PyErr_SetString (PyExc_TypeError , "Template.__new__ *args need to be of type 'str' or 'Interpolation'" );
106113 return NULL ;
107114 }
@@ -112,13 +119,11 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
112119
113120 PyObject * strings = PyTuple_New (stringslen );
114121 if (!strings ) {
115- Py_DECREF (self );
116122 return NULL ;
117123 }
118124
119125 PyObject * interpolations = PyTuple_New (interpolationslen );
120126 if (!interpolations ) {
121- Py_DECREF (self );
122127 Py_DECREF (strings );
123128 return NULL ;
124129 }
@@ -133,7 +138,9 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
133138 PyObject * concat = PyUnicode_Concat (laststring , item );
134139 Py_DECREF (laststring );
135140 if (!concat ) {
136- goto error ;
141+ Py_DECREF (strings );
142+ Py_DECREF (interpolations );
143+ return NULL ;
137144 }
138145 PyTuple_SET_ITEM (strings , stringsidx - 1 , concat );
139146 }
@@ -154,15 +161,10 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
154161 PyTuple_SET_ITEM (strings , stringsidx ++ , & _Py_STR (empty ));
155162 }
156163
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 );
163165 Py_DECREF (strings );
164166 Py_DECREF (interpolations );
165- return NULL ;
167+ return template ;
166168}
167169
168170static void
@@ -219,19 +221,6 @@ template_iter(templateobject *self)
219221 return iter ;
220222}
221223
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-
235224static PyObject *
236225template_interpolations_copy (PyObject * interpolations ) {
237226 Py_ssize_t interpolationslen = PyTuple_GET_SIZE (interpolations );
@@ -360,12 +349,10 @@ template_concat_templates(templateobject *self, templateobject *other)
360349 return NULL ;
361350 }
362351
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 );
365353 Py_DECREF (newstrings );
366354 Py_DECREF (newinterpolations );
367-
368- return newtemplate ;
355+ return (PyObject * ) newtemplate ;
369356}
370357
371358static PyObject *
@@ -382,12 +369,10 @@ template_concat_template_str(templateobject *self, PyObject *other)
382369 return NULL ;
383370 }
384371
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 );
387373 Py_DECREF (newstrings );
388374 Py_DECREF (newinterpolations );
389-
390- return newtemplate ;
375+ return (PyObject * ) newtemplate ;
391376}
392377
393378static PyObject *
@@ -404,12 +389,10 @@ template_concat_str_template(templateobject *self, PyObject *other)
404389 return NULL ;
405390 }
406391
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 );
409393 Py_DECREF (newstrings );
410394 Py_DECREF (newinterpolations );
411-
412- return newtemplate ;
395+ return (PyObject * ) newtemplate ;
413396}
414397
415398PyObject *
0 commit comments