Skip to content

Commit 90fc2a1

Browse files
picnixzlkollar
authored andcommitted
pythongh-116946: fully implement GC protocol for bz2 objects (python#138266)
1 parent ad67a6b commit 90fc2a1

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Modules/_bz2module.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,14 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
381381
static void
382382
BZ2Compressor_dealloc(PyObject *op)
383383
{
384+
PyTypeObject *tp = Py_TYPE(op);
385+
PyObject_GC_UnTrack(op);
384386
BZ2Compressor *self = _BZ2Compressor_CAST(op);
385387
BZ2_bzCompressEnd(&self->bzs);
386388
if (self->lock != NULL) {
387389
PyThread_free_lock(self->lock);
388390
}
389-
PyTypeObject *tp = Py_TYPE(self);
390-
tp->tp_free((PyObject *)self);
391+
tp->tp_free(self);
391392
Py_DECREF(tp);
392393
}
393394

@@ -420,7 +421,7 @@ static PyType_Spec bz2_compressor_type_spec = {
420421
// bz2_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
421422
// which prevents to create a subclass.
422423
// So calling PyType_GetModuleState() in this file is always safe.
423-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
424+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
424425
.slots = bz2_compressor_type_slots,
425426
};
426427

@@ -687,19 +688,19 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
687688
static void
688689
BZ2Decompressor_dealloc(PyObject *op)
689690
{
691+
PyTypeObject *tp = Py_TYPE(op);
692+
PyObject_GC_UnTrack(op);
690693
BZ2Decompressor *self = _BZ2Decompressor_CAST(op);
691694

692-
if(self->input_buffer != NULL) {
695+
if (self->input_buffer != NULL) {
693696
PyMem_Free(self->input_buffer);
694697
}
695698
BZ2_bzDecompressEnd(&self->bzs);
696699
Py_CLEAR(self->unused_data);
697700
if (self->lock != NULL) {
698701
PyThread_free_lock(self->lock);
699702
}
700-
701-
PyTypeObject *tp = Py_TYPE(self);
702-
tp->tp_free((PyObject *)self);
703+
tp->tp_free(self);
703704
Py_DECREF(tp);
704705
}
705706

@@ -751,7 +752,7 @@ static PyType_Spec bz2_decompressor_type_spec = {
751752
// bz2_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
752753
// which prevents to create a subclass.
753754
// So calling PyType_GetModuleState() in this file is always safe.
754-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
755+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
755756
.slots = bz2_decompressor_type_slots,
756757
};
757758

0 commit comments

Comments
 (0)