Skip to content

Commit 940a6da

Browse files
committed
Make dealloc use tp_free or don't allow inheritance
When a class is a base type, it must use its type's tp_free function to trigger the real free, instead of PyObjec_Del(). We do not always follow this convention, so let's give it a once-over and make sure we do that or that it's not a base type. Many of the types have the flag set in the struct, but do not pass the allocator function at init time, which makes them not really be a base. Remove the flag for those types.
1 parent e4d4511 commit 940a6da

14 files changed

+20
-24
lines changed

TODO.txt

-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,3 @@ Other
1414
- Make the Py_LOCAL_INLINE macro to work with Python 2.6, 2.7 and 3.1
1515
- Use surrogateescape in Python 3, see PEP-383
1616
- Expose the ODB (Repository.odb)
17-
- According to Python documentation, tp_dealloc must call tp_free (instead of
18-
PyObject_Del or similar) if the type is subclassable. So, go through the
19-
code and switch to tp_free, or make the type not subclassable, on a case by
20-
case basis.

src/blame.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ PyTypeObject BlameHunkType = {
170170
0, /* tp_getattro */
171171
0, /* tp_setattro */
172172
0, /* tp_as_buffer */
173-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
173+
Py_TPFLAGS_DEFAULT, /* tp_flags */
174174
BlameHunk__doc__, /* tp_doc */
175175
0, /* tp_traverse */
176176
0, /* tp_clear */
@@ -233,7 +233,7 @@ PyTypeObject BlameIterType = {
233233
0, /* tp_getattro */
234234
0, /* tp_setattro */
235235
0, /* tp_as_buffer */
236-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
236+
Py_TPFLAGS_DEFAULT, /* tp_flags */
237237
BlameIter__doc__, /* tp_doc */
238238
0, /* tp_traverse */
239239
0, /* tp_clear */
@@ -362,7 +362,7 @@ PyTypeObject BlameType = {
362362
0, /* tp_getattro */
363363
0, /* tp_setattro */
364364
0, /* tp_as_buffer */
365-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
365+
Py_TPFLAGS_DEFAULT, /* tp_flags */
366366
Blame__doc__, /* tp_doc */
367367
0, /* tp_traverse */
368368
0, /* tp_clear */

src/blob.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ PyTypeObject BlobType = {
181181
0, /* tp_getattro */
182182
0, /* tp_setattro */
183183
0, /* tp_as_buffer */
184-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
184+
Py_TPFLAGS_DEFAULT, /* tp_flags */
185185
Blob__doc__, /* tp_doc */
186186
0, /* tp_traverse */
187187
0, /* tp_clear */

src/commit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ PyTypeObject CommitType = {
259259
0, /* tp_getattro */
260260
0, /* tp_setattro */
261261
0, /* tp_as_buffer */
262-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
262+
Py_TPFLAGS_DEFAULT, /* tp_flags */
263263
Commit__doc__, /* tp_doc */
264264
0, /* tp_traverse */
265265
0, /* tp_clear */

src/config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ void
9292
Config_dealloc(Config *self)
9393
{
9494
git_config_free(self->config);
95-
PyObject_Del(self);
95+
Py_TYPE(self)->tp_free(self);
9696
}
9797

9898
PyDoc_STRVAR(Config_get_global_config__doc__,

src/diff.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ PyTypeObject PatchType = {
207207
0, /* tp_getattro */
208208
0, /* tp_setattro */
209209
0, /* tp_as_buffer */
210-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
210+
Py_TPFLAGS_DEFAULT, /* tp_flags */
211211
Patch__doc__, /* tp_doc */
212212
0, /* tp_traverse */
213213
0, /* tp_clear */
@@ -269,7 +269,7 @@ PyTypeObject DiffIterType = {
269269
0, /* tp_getattro */
270270
0, /* tp_setattro */
271271
0, /* tp_as_buffer */
272-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
272+
Py_TPFLAGS_DEFAULT, /* tp_flags */
273273
DiffIter__doc__, /* tp_doc */
274274
0, /* tp_traverse */
275275
0, /* tp_clear */

src/index.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Index_dealloc(Index* self)
7171
PyObject_GC_UnTrack(self);
7272
Py_XDECREF(self->repo);
7373
git_index_free(self->index);
74-
PyObject_GC_Del(self);
74+
Py_TYPE(self)->tp_free(self);
7575
}
7676

7777
int
@@ -561,7 +561,7 @@ void
561561
IndexIter_dealloc(IndexIter *self)
562562
{
563563
Py_CLEAR(self->owner);
564-
PyObject_Del(self);
564+
Py_TYPE(self)->tp_free(self);
565565
}
566566

567567
PyObject *

src/note.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ PyTypeObject NoteType = {
134134
0, /* tp_getattro */
135135
0, /* tp_setattro */
136136
0, /* tp_as_buffer */
137-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
137+
Py_TPFLAGS_DEFAULT, /* tp_flags */
138138
Note__doc__, /* tp_doc */
139139
0, /* tp_traverse */
140140
0, /* tp_clear */
@@ -200,7 +200,7 @@ PyTypeObject NoteIterType = {
200200
0, /* tp_getattro */
201201
0, /* tp_setattro */
202202
0, /* tp_as_buffer */
203-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
203+
Py_TPFLAGS_DEFAULT, /* tp_flags */
204204
NoteIter__doc__, /* tp_doc */
205205
0, /* tp_traverse */
206206
0, /* tp_clear */

src/object.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Object_dealloc(Object* self)
4545
{
4646
Py_CLEAR(self->repo);
4747
git_object_free(self->obj);
48-
PyObject_Del(self);
48+
Py_TYPE(self)->tp_free(self);
4949
}
5050

5151

src/reference.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ PyTypeObject RefLogIterType = {
9797
0, /* tp_getattro */
9898
0, /* tp_setattro */
9999
0, /* tp_as_buffer */
100-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
100+
Py_TPFLAGS_DEFAULT, /* tp_flags */
101101
RefLogIterType__doc__, /* tp_doc */
102102
0, /* tp_traverse */
103103
0, /* tp_clear */

src/remote.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ PyTypeObject RemoteType = {
658658
0, /* tp_getattro */
659659
0, /* tp_setattro */
660660
0, /* tp_as_buffer */
661-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
661+
Py_TPFLAGS_DEFAULT, /* tp_flags */
662662
Remote__doc__, /* tp_doc */
663663
0, /* tp_traverse */
664664
0, /* tp_clear */

src/repository.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Repository_dealloc(Repository *self)
105105
Py_CLEAR(self->index);
106106
Py_CLEAR(self->config);
107107
git_repository_free(self->repo);
108-
PyObject_GC_Del(self);
108+
Py_TYPE(self)->tp_free(self);
109109
}
110110

111111
int

src/tag.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ PyTypeObject TagType = {
151151
0, /* tp_getattro */
152152
0, /* tp_setattro */
153153
0, /* tp_as_buffer */
154-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
154+
Py_TPFLAGS_DEFAULT, /* tp_flags */
155155
Tag__doc__, /* tp_doc */
156156
0, /* tp_traverse */
157157
0, /* tp_clear */

src/tree.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ PyTypeObject TreeEntryType = {
170170
0, /* tp_getattro */
171171
0, /* tp_setattro */
172172
0, /* tp_as_buffer */
173-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
173+
Py_TPFLAGS_DEFAULT, /* tp_flags */
174174
TreeEntry__doc__, /* tp_doc */
175175
0, /* tp_traverse */
176176
0, /* tp_clear */
@@ -518,7 +518,7 @@ PyTypeObject TreeType = {
518518
0, /* tp_getattro */
519519
0, /* tp_setattro */
520520
0, /* tp_as_buffer */
521-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
521+
Py_TPFLAGS_DEFAULT, /* tp_flags */
522522
Tree__doc__, /* tp_doc */
523523
0, /* tp_traverse */
524524
0, /* tp_clear */
@@ -589,7 +589,7 @@ PyTypeObject TreeIterType = {
589589
0, /* tp_getattro */
590590
0, /* tp_setattro */
591591
0, /* tp_as_buffer */
592-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
592+
Py_TPFLAGS_DEFAULT, /* tp_flags */
593593
TreeIter__doc__, /* tp_doc */
594594
0, /* tp_traverse */
595595
0, /* tp_clear */

0 commit comments

Comments
 (0)