mirror of
https://github.com/python/cpython.git
synced 2025-11-24 12:20:42 +00:00
[3.14] gh-116946: fully implement GC protocol for zlib objects (GH-138290) (#138327)
This commit is contained in:
parent
664d17f97a
commit
c8cc39d938
1 changed files with 58 additions and 19 deletions
|
|
@ -263,7 +263,9 @@ static compobject *
|
|||
newcompobject(PyTypeObject *type)
|
||||
{
|
||||
compobject *self;
|
||||
self = PyObject_New(compobject, type);
|
||||
assert(type != NULL);
|
||||
assert(type->tp_alloc != NULL);
|
||||
self = _compobject_CAST(type->tp_alloc(type, 0));
|
||||
if (self == NULL)
|
||||
return NULL;
|
||||
self->eof = 0;
|
||||
|
|
@ -706,33 +708,41 @@ zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
|
|||
}
|
||||
|
||||
static void
|
||||
Dealloc(compobject *self)
|
||||
compobject_dealloc_impl(PyObject *op, int (*dealloc)(z_streamp))
|
||||
{
|
||||
PyTypeObject *type = Py_TYPE(self);
|
||||
PyTypeObject *type = Py_TYPE(op);
|
||||
PyObject_GC_UnTrack(op);
|
||||
compobject *self = _compobject_CAST(op);
|
||||
if (self->is_initialised) {
|
||||
(void)dealloc(&self->zst);
|
||||
}
|
||||
PyThread_free_lock(self->lock);
|
||||
Py_XDECREF(self->unused_data);
|
||||
Py_XDECREF(self->unconsumed_tail);
|
||||
Py_XDECREF(self->zdict);
|
||||
PyObject_Free(self);
|
||||
type->tp_free(self);
|
||||
Py_DECREF(type);
|
||||
}
|
||||
|
||||
static int
|
||||
compobject_traverse(PyObject *op, visitproc visit, void *arg)
|
||||
{
|
||||
compobject *self = _compobject_CAST(op);
|
||||
Py_VISIT(Py_TYPE(op));
|
||||
Py_VISIT(self->zdict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
Comp_dealloc(PyObject *op)
|
||||
{
|
||||
compobject *self = _compobject_CAST(op);
|
||||
if (self->is_initialised)
|
||||
(void)deflateEnd(&self->zst);
|
||||
Dealloc(self);
|
||||
compobject_dealloc_impl(op, &deflateEnd);
|
||||
}
|
||||
|
||||
static void
|
||||
Decomp_dealloc(PyObject *op)
|
||||
{
|
||||
compobject *self = _compobject_CAST(op);
|
||||
if (self->is_initialised)
|
||||
(void)inflateEnd(&self->zst);
|
||||
Dealloc(self);
|
||||
compobject_dealloc_impl(op, &inflateEnd);
|
||||
}
|
||||
|
||||
/*[clinic input]
|
||||
|
|
@ -1357,6 +1367,8 @@ typedef struct {
|
|||
char needs_input;
|
||||
} ZlibDecompressor;
|
||||
|
||||
#define ZlibDecompressor_CAST(op) ((ZlibDecompressor *)(op))
|
||||
|
||||
/*[clinic input]
|
||||
class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
|
||||
[clinic start generated code]*/
|
||||
|
|
@ -1365,8 +1377,9 @@ class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
|
|||
static void
|
||||
ZlibDecompressor_dealloc(PyObject *op)
|
||||
{
|
||||
ZlibDecompressor *self = (ZlibDecompressor*)op;
|
||||
PyObject *type = (PyObject *)Py_TYPE(self);
|
||||
PyTypeObject *type = Py_TYPE(op);
|
||||
PyObject_GC_UnTrack(op);
|
||||
ZlibDecompressor *self = ZlibDecompressor_CAST(op);
|
||||
PyThread_free_lock(self->lock);
|
||||
if (self->is_initialised) {
|
||||
inflateEnd(&self->zst);
|
||||
|
|
@ -1374,10 +1387,19 @@ ZlibDecompressor_dealloc(PyObject *op)
|
|||
PyMem_Free(self->input_buffer);
|
||||
Py_CLEAR(self->unused_data);
|
||||
Py_CLEAR(self->zdict);
|
||||
PyObject_Free(self);
|
||||
type->tp_free(self);
|
||||
Py_DECREF(type);
|
||||
}
|
||||
|
||||
static int
|
||||
ZlibDecompressor_traverse(PyObject *op, visitproc visit, void *arg)
|
||||
{
|
||||
ZlibDecompressor *self = ZlibDecompressor_CAST(op);
|
||||
Py_VISIT(Py_TYPE(op));
|
||||
Py_VISIT(self->zdict);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
set_inflate_zdict_ZlibDecompressor(zlibstate *state, ZlibDecompressor *self)
|
||||
{
|
||||
|
|
@ -1729,7 +1751,9 @@ ZlibDecompressor__new__(PyTypeObject *cls,
|
|||
args, kwargs, format, keywords, &wbits, &zdict)) {
|
||||
return NULL;
|
||||
}
|
||||
ZlibDecompressor *self = PyObject_New(ZlibDecompressor, cls);
|
||||
|
||||
assert(cls != NULL && cls->tp_alloc != NULL);
|
||||
ZlibDecompressor *self = ZlibDecompressor_CAST(cls->tp_alloc(cls, 0));
|
||||
if (self == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
|
@ -1937,6 +1961,7 @@ static PyMethodDef zlib_methods[] =
|
|||
|
||||
static PyType_Slot Comptype_slots[] = {
|
||||
{Py_tp_dealloc, Comp_dealloc},
|
||||
{Py_tp_traverse, compobject_traverse},
|
||||
{Py_tp_methods, comp_methods},
|
||||
{0, 0},
|
||||
};
|
||||
|
|
@ -1944,12 +1969,17 @@ static PyType_Slot Comptype_slots[] = {
|
|||
static PyType_Spec Comptype_spec = {
|
||||
.name = "zlib.Compress",
|
||||
.basicsize = sizeof(compobject),
|
||||
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
|
||||
.flags = (
|
||||
Py_TPFLAGS_DEFAULT
|
||||
| Py_TPFLAGS_DISALLOW_INSTANTIATION
|
||||
| Py_TPFLAGS_HAVE_GC
|
||||
),
|
||||
.slots= Comptype_slots,
|
||||
};
|
||||
|
||||
static PyType_Slot Decomptype_slots[] = {
|
||||
{Py_tp_dealloc, Decomp_dealloc},
|
||||
{Py_tp_traverse, compobject_traverse},
|
||||
{Py_tp_methods, Decomp_methods},
|
||||
{Py_tp_members, Decomp_members},
|
||||
{0, 0},
|
||||
|
|
@ -1958,12 +1988,17 @@ static PyType_Slot Decomptype_slots[] = {
|
|||
static PyType_Spec Decomptype_spec = {
|
||||
.name = "zlib.Decompress",
|
||||
.basicsize = sizeof(compobject),
|
||||
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION,
|
||||
.flags = (
|
||||
Py_TPFLAGS_DEFAULT
|
||||
| Py_TPFLAGS_DISALLOW_INSTANTIATION
|
||||
| Py_TPFLAGS_HAVE_GC
|
||||
),
|
||||
.slots = Decomptype_slots,
|
||||
};
|
||||
|
||||
static PyType_Slot ZlibDecompressor_type_slots[] = {
|
||||
{Py_tp_dealloc, ZlibDecompressor_dealloc},
|
||||
{Py_tp_traverse, ZlibDecompressor_traverse},
|
||||
{Py_tp_members, ZlibDecompressor_members},
|
||||
{Py_tp_new, ZlibDecompressor__new__},
|
||||
{Py_tp_doc, (char *)ZlibDecompressor__new____doc__},
|
||||
|
|
@ -1978,7 +2013,11 @@ static PyType_Spec ZlibDecompressor_type_spec = {
|
|||
// ZlibDecompressor_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 = ZlibDecompressor_type_slots,
|
||||
};
|
||||
PyDoc_STRVAR(zlib_module_documentation,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue