@@ -11,9 +11,14 @@ typedef struct {
1111 int from_strings ;
1212} templateiterobject ;
1313
14+ #define _PyTemplateIter_CheckExact (op ) Py_IS_TYPE((op), &_PyTemplateIter_Type)
15+ #define templateiterobject_CAST (op ) \
16+ (assert(_PyTemplateIter_CheckExact(op)), _Py_CAST(templateiterobject*, (op)))
17+
1418static PyObject *
15- templateiter_next (templateiterobject * self )
19+ templateiter_next (PyObject * op )
1620{
21+ templateiterobject * self = templateiterobject_CAST (op );
1722 PyObject * item ;
1823 if (self -> from_strings ) {
1924 item = PyIter_Next (self -> stringsiter );
@@ -30,17 +35,26 @@ templateiter_next(templateiterobject *self)
3035}
3136
3237static void
33- templateiter_dealloc (templateiterobject * self )
38+ templateiter_dealloc (PyObject * op )
39+ {
40+ PyObject_GC_UnTrack (op );
41+ Py_TYPE (op )-> tp_clear (op );
42+ Py_TYPE (op )-> tp_free (op );
43+ }
44+
45+ static int
46+ templateiter_clear (PyObject * op )
3447{
35- PyObject_GC_UnTrack ( self );
48+ templateiterobject * self = templateiterobject_CAST ( op );
3649 Py_CLEAR (self -> stringsiter );
3750 Py_CLEAR (self -> interpolationsiter );
38- Py_TYPE ( self ) -> tp_free ( self ) ;
51+ return 0 ;
3952}
4053
4154static int
42- templateiter_traverse (templateiterobject * self , visitproc visit , void * arg )
55+ templateiter_traverse (PyObject * op , visitproc visit , void * arg )
4356{
57+ templateiterobject * self = templateiterobject_CAST (op );
4458 Py_VISIT (self -> stringsiter );
4559 Py_VISIT (self -> interpolationsiter );
4660 return 0 ;
@@ -54,11 +68,12 @@ PyTypeObject _PyTemplateIter_Type = {
5468 .tp_itemsize = 0 ,
5569 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC ,
5670 .tp_alloc = PyType_GenericAlloc ,
57- .tp_dealloc = (destructor ) templateiter_dealloc ,
71+ .tp_dealloc = templateiter_dealloc ,
72+ .tp_clear = templateiter_clear ,
5873 .tp_free = PyObject_GC_Del ,
59- .tp_traverse = ( traverseproc ) templateiter_traverse ,
74+ .tp_traverse = templateiter_traverse ,
6075 .tp_iter = PyObject_SelfIter ,
61- .tp_iternext = ( iternextfunc ) templateiter_next ,
76+ .tp_iternext = templateiter_next ,
6277};
6378
6479typedef struct {
@@ -159,12 +174,19 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
159174
160175static void
161176template_dealloc (PyObject * op )
177+ {
178+ PyObject_GC_UnTrack (op );
179+ Py_TYPE (op )-> tp_clear (op );
180+ Py_TYPE (op )-> tp_free (op );
181+ }
182+
183+ static int
184+ template_clear (PyObject * op )
162185{
163186 templateobject * self = templateobject_CAST (op );
164- PyObject_GC_UnTrack (self );
165187 Py_CLEAR (self -> strings );
166188 Py_CLEAR (self -> interpolations );
167- Py_TYPE ( self ) -> tp_free ( self ) ;
189+ return 0 ;
168190}
169191
170192static int
@@ -413,6 +435,7 @@ PyTypeObject _PyTemplate_Type = {
413435 .tp_new = template_new ,
414436 .tp_alloc = PyType_GenericAlloc ,
415437 .tp_dealloc = template_dealloc ,
438+ .tp_clear = template_clear ,
416439 .tp_free = PyObject_GC_Del ,
417440 .tp_repr = template_repr ,
418441 .tp_members = template_members ,
0 commit comments