@@ -11,9 +11,13 @@ typedef struct {
1111 int from_strings ;
1212} templateiterobject ;
1313
14+ #define templateiterobject_CAST (op ) \
15+ (assert(_PyTemplateIter_CheckExact(op)), _Py_CAST(templateiterobject*, (op)))
16+
1417static PyObject *
15- templateiter_next (templateiterobject * self )
18+ templateiter_next (PyObject * op )
1619{
20+ templateiterobject * self = templateiterobject_CAST (op );
1721 PyObject * item ;
1822 if (self -> from_strings ) {
1923 item = PyIter_Next (self -> stringsiter );
@@ -30,17 +34,26 @@ templateiter_next(templateiterobject *self)
3034}
3135
3236static void
33- templateiter_dealloc (templateiterobject * self )
37+ templateiter_dealloc (PyObject * op )
38+ {
39+ PyObject_GC_UnTrack (op );
40+ Py_TYPE (op )-> tp_clear (op );
41+ Py_TYPE (op )-> tp_free (op );
42+ }
43+
44+ static int
45+ templateiter_clear (PyObject * op )
3446{
35- PyObject_GC_UnTrack ( self );
47+ templateiterobject * self = templateiterobject_CAST ( op );
3648 Py_CLEAR (self -> stringsiter );
3749 Py_CLEAR (self -> interpolationsiter );
38- Py_TYPE ( self ) -> tp_free ( self ) ;
50+ return 0 ;
3951}
4052
4153static int
42- templateiter_traverse (templateiterobject * self , visitproc visit , void * arg )
54+ templateiter_traverse (PyObject * op , visitproc visit , void * arg )
4355{
56+ templateiterobject * self = templateiterobject_CAST (op );
4457 Py_VISIT (self -> stringsiter );
4558 Py_VISIT (self -> interpolationsiter );
4659 return 0 ;
@@ -54,11 +67,12 @@ PyTypeObject _PyTemplateIter_Type = {
5467 .tp_itemsize = 0 ,
5568 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC ,
5669 .tp_alloc = PyType_GenericAlloc ,
57- .tp_dealloc = (destructor ) templateiter_dealloc ,
70+ .tp_dealloc = templateiter_dealloc ,
71+ .tp_clear = templateiter_clear ,
5872 .tp_free = PyObject_GC_Del ,
59- .tp_traverse = ( traverseproc ) templateiter_traverse ,
73+ .tp_traverse = templateiter_traverse ,
6074 .tp_iter = PyObject_SelfIter ,
61- .tp_iternext = ( iternextfunc ) templateiter_next ,
75+ .tp_iternext = templateiter_next ,
6276};
6377
6478typedef struct {
@@ -159,12 +173,19 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
159173
160174static void
161175template_dealloc (PyObject * op )
176+ {
177+ PyObject_GC_UnTrack (op );
178+ Py_TYPE (op )-> tp_clear (op );
179+ Py_TYPE (op )-> tp_free (op );
180+ }
181+
182+ static int
183+ template_clear (PyObject * op )
162184{
163185 templateobject * self = templateobject_CAST (op );
164- PyObject_GC_UnTrack (self );
165186 Py_CLEAR (self -> strings );
166187 Py_CLEAR (self -> interpolations );
167- Py_TYPE ( self ) -> tp_free ( self ) ;
188+ return 0 ;
168189}
169190
170191static int
@@ -413,6 +434,7 @@ PyTypeObject _PyTemplate_Type = {
413434 .tp_new = template_new ,
414435 .tp_alloc = PyType_GenericAlloc ,
415436 .tp_dealloc = template_dealloc ,
437+ .tp_clear = template_clear ,
416438 .tp_free = PyObject_GC_Del ,
417439 .tp_repr = template_repr ,
418440 .tp_members = template_members ,
0 commit comments