Skip to content

Commit b24d269

Browse files
committed
Add tp_clear slot and remove UB casts
1 parent a21391a commit b24d269

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

Objects/interpolationobject.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,21 @@ interpolation_new_impl(PyTypeObject *type, PyObject *value,
7979

8080
static void
8181
interpolation_dealloc(PyObject *op)
82+
{
83+
PyObject_GC_UnTrack(op);
84+
Py_TYPE(op)->tp_clear(op);
85+
Py_TYPE(op)->tp_free(op);
86+
}
87+
88+
static int
89+
interpolation_clear(PyObject *op)
8290
{
8391
interpolationobject *self = interpolationobject_CAST(op);
84-
PyObject_GC_UnTrack(self);
8592
Py_CLEAR(self->value);
8693
Py_CLEAR(self->expression);
8794
Py_CLEAR(self->conversion);
8895
Py_CLEAR(self->format_spec);
89-
Py_TYPE(self)->tp_free(self);
96+
return 0;
9097
}
9198

9299
static int
@@ -127,6 +134,7 @@ PyTypeObject _PyInterpolation_Type = {
127134
.tp_new = interpolation_new,
128135
.tp_alloc = PyType_GenericAlloc,
129136
.tp_dealloc = interpolation_dealloc,
137+
.tp_clear = interpolation_clear,
130138
.tp_free = PyObject_GC_Del,
131139
.tp_repr = interpolation_repr,
132140
.tp_members = interpolation_members,

Objects/templateobject.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1418
static 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

3237
static 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

4154
static 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

6479
typedef struct {
@@ -159,12 +174,19 @@ template_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
159174

160175
static void
161176
template_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

170192
static 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

Comments
 (0)