Skip to content
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions Modules/_bz2module.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
if (catch_bz2_error(bzerror))
goto error;

PyObject_GC_Track(self);
return (PyObject *)self;

error:
Expand All @@ -381,13 +382,14 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
static void
BZ2Compressor_dealloc(PyObject *op)
{
PyTypeObject *tp = Py_TYPE(op);
PyObject_GC_UnTrack(op);
BZ2Compressor *self = _BZ2Compressor_CAST(op);
BZ2_bzCompressEnd(&self->bzs);
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject *)self);
PyObject_GC_Del(self);
Py_DECREF(tp);
}

Expand Down Expand Up @@ -420,7 +422,7 @@ static PyType_Spec bz2_compressor_type_spec = {
// bz2_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
.slots = bz2_compressor_type_slots,
};

Expand Down Expand Up @@ -677,6 +679,7 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
if (catch_bz2_error(bzerror))
goto error;

PyObject_GC_Track(self);
return (PyObject *)self;

error:
Expand All @@ -687,8 +690,9 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
static void
BZ2Decompressor_dealloc(PyObject *op)
{
PyTypeObject *tp = Py_TYPE(op);
PyObject_GC_UnTrack(op);
BZ2Decompressor *self = _BZ2Decompressor_CAST(op);

if(self->input_buffer != NULL) {
PyMem_Free(self->input_buffer);
}
Expand All @@ -697,9 +701,7 @@ BZ2Decompressor_dealloc(PyObject *op)
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}

PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject *)self);
PyObject_GC_Del(self);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also prefer tp_free over PyObject_GC_Del. The new docs say that too:

Do not call this directly to free an object’s memory; call the type’s tp_free slot instead.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I just swa that as well, so I'm going to add another commit (I'm modifying the other 3 PRs at the same time)

Py_DECREF(tp);
}

Expand Down Expand Up @@ -751,7 +753,7 @@ static PyType_Spec bz2_decompressor_type_spec = {
// bz2_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
.slots = bz2_decompressor_type_slots,
};

Expand Down
Loading