11/* t-string Template object implementation */
22
33#include "Python.h"
4- #include "pycore_interpolation.h" // _PyInterpolation_Check ()
4+ #include "pycore_interpolation.h" // _PyInterpolation_CheckExact ()
55#include "pycore_template.h"
66
77typedef struct {
@@ -67,6 +67,9 @@ typedef struct {
6767 PyObject * interpolations ;
6868} templateobject ;
6969
70+ #define templateobject_CAST (op ) \
71+ (assert(_PyTemplate_CheckExact(op)), _Py_CAST(templateobject*, (op)))
72+
7073static templateobject *
7174template_from_strings_interpolations (PyTypeObject * type , PyObject * strings , PyObject * interpolations )
7275{
@@ -80,7 +83,7 @@ template_from_strings_interpolations(PyTypeObject *type, PyObject *strings, PyOb
8083 return template ;
8184}
8285
83- static templateobject *
86+ static PyObject *
8487template_new (PyTypeObject * type , PyObject * args , PyObject * kwds )
8588{
8689 if (kwds != NULL ) {
@@ -101,7 +104,7 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
101104 }
102105 last_was_str = 1 ;
103106 }
104- else if (_PyInterpolation_Check (item )) {
107+ else if (_PyInterpolation_CheckExact (item )) {
105108 if (!last_was_str ) {
106109 stringslen ++ ;
107110 }
@@ -149,7 +152,7 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
149152 }
150153 last_was_str = 1 ;
151154 }
152- else if (_PyInterpolation_Check (item )) {
155+ else if (_PyInterpolation_CheckExact (item )) {
153156 if (!last_was_str ) {
154157 PyTuple_SET_ITEM (strings , stringsidx ++ , & _Py_STR (empty ));
155158 }
@@ -164,38 +167,42 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
164167 templateobject * template = template_from_strings_interpolations (type , strings , interpolations );
165168 Py_DECREF (strings );
166169 Py_DECREF (interpolations );
167- return template ;
170+ return ( PyObject * ) template ;
168171}
169172
170173static void
171- template_dealloc (templateobject * self )
174+ template_dealloc (PyObject * op )
172175{
176+ templateobject * self = templateobject_CAST (op );
173177 PyObject_GC_UnTrack (self );
174178 Py_CLEAR (self -> strings );
175179 Py_CLEAR (self -> interpolations );
176180 Py_TYPE (self )-> tp_free (self );
177181}
178182
179183static int
180- template_traverse (templateobject * self , visitproc visit , void * arg )
184+ template_traverse (PyObject * op , visitproc visit , void * arg )
181185{
186+ templateobject * self = templateobject_CAST (op );
182187 Py_VISIT (self -> strings );
183188 Py_VISIT (self -> interpolations );
184189 return 0 ;
185190}
186191
187192static PyObject *
188- template_repr (templateobject * self )
193+ template_repr (PyObject * op )
189194{
195+ templateobject * self = templateobject_CAST (op );
190196 return PyUnicode_FromFormat ("%s(strings=%R, interpolations=%R)" ,
191197 _PyType_Name (Py_TYPE (self )),
192198 self -> strings ,
193199 self -> interpolations );
194200}
195201
196- static templateiterobject *
197- template_iter (templateobject * self )
202+ static PyObject *
203+ template_iter (PyObject * op )
198204{
205+ templateobject * self = templateobject_CAST (op );
199206 templateiterobject * iter = PyObject_GC_New (templateiterobject , & _PyTemplateIter_Type );
200207 if (iter == NULL ) {
201208 return NULL ;
@@ -218,7 +225,7 @@ template_iter(templateobject *self)
218225 iter -> interpolationsiter = interpolationsiter ;
219226 iter -> from_strings = 1 ;
220227 PyObject_GC_Track (iter );
221- return iter ;
228+ return ( PyObject * ) iter ;
222229}
223230
224231static PyObject *
@@ -398,13 +405,13 @@ template_concat_str_template(templateobject *self, PyObject *other)
398405PyObject *
399406_PyTemplate_Concat (PyObject * self , PyObject * other )
400407{
401- if (_PyTemplate_Check (self ) && _PyTemplate_Check (other )) {
408+ if (_PyTemplate_CheckExact (self ) && _PyTemplate_CheckExact (other )) {
402409 return template_concat_templates ((templateobject * ) self , (templateobject * ) other );
403410 }
404- else if ((_PyTemplate_Check (self )) && PyUnicode_Check (other )) {
411+ else if ((_PyTemplate_CheckExact (self )) && PyUnicode_Check (other )) {
405412 return template_concat_template_str ((templateobject * ) self , other );
406413 }
407- else if (PyUnicode_Check (self ) && (_PyTemplate_Check (other ))) {
414+ else if (PyUnicode_Check (self ) && (_PyTemplate_CheckExact (other ))) {
408415 return template_concat_str_template ((templateobject * ) other , self );
409416 }
410417 else {
@@ -413,8 +420,9 @@ _PyTemplate_Concat(PyObject *self, PyObject *other)
413420}
414421
415422static PyObject *
416- template_values_get (templateobject * self , void * Py_UNUSED (data ))
423+ template_values_get (PyObject * op , void * Py_UNUSED (data ))
417424{
425+ templateobject * self = templateobject_CAST (op );
418426 PyObject * values = PyTuple_New (PyTuple_GET_SIZE (self -> interpolations ));
419427 if (values == NULL ) {
420428 return NULL ;
@@ -448,7 +456,7 @@ static PyMemberDef template_members[] = {
448456};
449457
450458static PyGetSetDef template_getset [] = {
451- {"values" , ( getter ) template_values_get , NULL , "Values of interpolations" , NULL },
459+ {"values" , template_values_get , NULL , "Values of interpolations" , NULL },
452460 {NULL },
453461};
454462
@@ -464,15 +472,15 @@ PyTypeObject _PyTemplate_Type = {
464472 .tp_itemsize = 0 ,
465473 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC ,
466474 .tp_as_sequence = & template_as_sequence ,
467- .tp_new = ( newfunc ) template_new ,
475+ .tp_new = template_new ,
468476 .tp_alloc = PyType_GenericAlloc ,
469- .tp_dealloc = ( destructor ) template_dealloc ,
477+ .tp_dealloc = template_dealloc ,
470478 .tp_free = PyObject_GC_Del ,
471- .tp_repr = ( reprfunc ) template_repr ,
479+ .tp_repr = template_repr ,
472480 .tp_members = template_members ,
473481 .tp_getset = template_getset ,
474- .tp_iter = ( getiterfunc ) template_iter ,
475- .tp_traverse = ( traverseproc ) template_traverse ,
482+ .tp_iter = template_iter ,
483+ .tp_traverse = template_traverse ,
476484};
477485
478486PyObject *
0 commit comments