@@ -263,7 +263,9 @@ static compobject *
263
263
newcompobject (PyTypeObject * type )
264
264
{
265
265
compobject * self ;
266
- self = PyObject_New (compobject , type );
266
+ assert (type != NULL );
267
+ assert (type -> tp_alloc != NULL );
268
+ self = _compobject_CAST (type -> tp_alloc (type , 0 ));
267
269
if (self == NULL )
268
270
return NULL ;
269
271
self -> eof = 0 ;
@@ -706,33 +708,41 @@ zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
706
708
}
707
709
708
710
static void
709
- Dealloc ( compobject * self )
711
+ compobject_dealloc_impl ( PyObject * op , int ( * dealloc )( z_streamp ) )
710
712
{
711
- PyTypeObject * type = Py_TYPE (self );
713
+ PyTypeObject * type = Py_TYPE (op );
714
+ PyObject_GC_UnTrack (op );
715
+ compobject * self = _compobject_CAST (op );
716
+ if (self -> is_initialised ) {
717
+ (void )dealloc (& self -> zst );
718
+ }
712
719
PyThread_free_lock (self -> lock );
713
720
Py_XDECREF (self -> unused_data );
714
721
Py_XDECREF (self -> unconsumed_tail );
715
722
Py_XDECREF (self -> zdict );
716
- PyObject_Free (self );
723
+ type -> tp_free (self );
717
724
Py_DECREF (type );
718
725
}
719
726
727
+ static int
728
+ compobject_traverse (PyObject * op , visitproc visit , void * arg )
729
+ {
730
+ compobject * self = _compobject_CAST (op );
731
+ Py_VISIT (Py_TYPE (op ));
732
+ Py_VISIT (self -> zdict );
733
+ return 0 ;
734
+ }
735
+
720
736
static void
721
737
Comp_dealloc (PyObject * op )
722
738
{
723
- compobject * self = _compobject_CAST (op );
724
- if (self -> is_initialised )
725
- (void )deflateEnd (& self -> zst );
726
- Dealloc (self );
739
+ compobject_dealloc_impl (op , & deflateEnd );
727
740
}
728
741
729
742
static void
730
743
Decomp_dealloc (PyObject * op )
731
744
{
732
- compobject * self = _compobject_CAST (op );
733
- if (self -> is_initialised )
734
- (void )inflateEnd (& self -> zst );
735
- Dealloc (self );
745
+ compobject_dealloc_impl (op , & inflateEnd );
736
746
}
737
747
738
748
/*[clinic input]
@@ -1357,6 +1367,8 @@ typedef struct {
1357
1367
char needs_input ;
1358
1368
} ZlibDecompressor ;
1359
1369
1370
+ #define ZlibDecompressor_CAST (op ) ((ZlibDecompressor *)(op))
1371
+
1360
1372
/*[clinic input]
1361
1373
class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
1362
1374
[clinic start generated code]*/
@@ -1365,19 +1377,29 @@ class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
1365
1377
static void
1366
1378
ZlibDecompressor_dealloc (PyObject * op )
1367
1379
{
1368
- ZlibDecompressor * self = (ZlibDecompressor * )op ;
1369
- PyObject * type = (PyObject * )Py_TYPE (self );
1380
+ PyTypeObject * type = Py_TYPE (op );
1381
+ PyObject_GC_UnTrack (op );
1382
+ ZlibDecompressor * self = ZlibDecompressor_CAST (op );
1370
1383
PyThread_free_lock (self -> lock );
1371
1384
if (self -> is_initialised ) {
1372
1385
inflateEnd (& self -> zst );
1373
1386
}
1374
1387
PyMem_Free (self -> input_buffer );
1375
1388
Py_CLEAR (self -> unused_data );
1376
1389
Py_CLEAR (self -> zdict );
1377
- PyObject_Free (self );
1390
+ type -> tp_free (self );
1378
1391
Py_DECREF (type );
1379
1392
}
1380
1393
1394
+ static int
1395
+ ZlibDecompressor_traverse (PyObject * op , visitproc visit , void * arg )
1396
+ {
1397
+ ZlibDecompressor * self = ZlibDecompressor_CAST (op );
1398
+ Py_VISIT (Py_TYPE (op ));
1399
+ Py_VISIT (self -> zdict );
1400
+ return 0 ;
1401
+ }
1402
+
1381
1403
static int
1382
1404
set_inflate_zdict_ZlibDecompressor (zlibstate * state , ZlibDecompressor * self )
1383
1405
{
@@ -1729,7 +1751,9 @@ ZlibDecompressor__new__(PyTypeObject *cls,
1729
1751
args , kwargs , format , keywords , & wbits , & zdict )) {
1730
1752
return NULL ;
1731
1753
}
1732
- ZlibDecompressor * self = PyObject_New (ZlibDecompressor , cls );
1754
+
1755
+ assert (cls != NULL && cls -> tp_alloc != NULL );
1756
+ ZlibDecompressor * self = ZlibDecompressor_CAST (cls -> tp_alloc (cls , 0 ));
1733
1757
if (self == NULL ) {
1734
1758
return NULL ;
1735
1759
}
@@ -1937,19 +1961,25 @@ static PyMethodDef zlib_methods[] =
1937
1961
1938
1962
static PyType_Slot Comptype_slots [] = {
1939
1963
{Py_tp_dealloc , Comp_dealloc },
1964
+ {Py_tp_traverse , compobject_traverse },
1940
1965
{Py_tp_methods , comp_methods },
1941
1966
{0 , 0 },
1942
1967
};
1943
1968
1944
1969
static PyType_Spec Comptype_spec = {
1945
1970
.name = "zlib.Compress" ,
1946
1971
.basicsize = sizeof (compobject ),
1947
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
1972
+ .flags = (
1973
+ Py_TPFLAGS_DEFAULT
1974
+ | Py_TPFLAGS_DISALLOW_INSTANTIATION
1975
+ | Py_TPFLAGS_HAVE_GC
1976
+ ),
1948
1977
.slots = Comptype_slots ,
1949
1978
};
1950
1979
1951
1980
static PyType_Slot Decomptype_slots [] = {
1952
1981
{Py_tp_dealloc , Decomp_dealloc },
1982
+ {Py_tp_traverse , compobject_traverse },
1953
1983
{Py_tp_methods , Decomp_methods },
1954
1984
{Py_tp_members , Decomp_members },
1955
1985
{0 , 0 },
@@ -1958,12 +1988,17 @@ static PyType_Slot Decomptype_slots[] = {
1958
1988
static PyType_Spec Decomptype_spec = {
1959
1989
.name = "zlib.Decompress" ,
1960
1990
.basicsize = sizeof (compobject ),
1961
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
1991
+ .flags = (
1992
+ Py_TPFLAGS_DEFAULT
1993
+ | Py_TPFLAGS_DISALLOW_INSTANTIATION
1994
+ | Py_TPFLAGS_HAVE_GC
1995
+ ),
1962
1996
.slots = Decomptype_slots ,
1963
1997
};
1964
1998
1965
1999
static PyType_Slot ZlibDecompressor_type_slots [] = {
1966
2000
{Py_tp_dealloc , ZlibDecompressor_dealloc },
2001
+ {Py_tp_traverse , ZlibDecompressor_traverse },
1967
2002
{Py_tp_members , ZlibDecompressor_members },
1968
2003
{Py_tp_new , ZlibDecompressor__new__ },
1969
2004
{Py_tp_doc , (char * )ZlibDecompressor__new____doc__ },
@@ -1978,7 +2013,11 @@ static PyType_Spec ZlibDecompressor_type_spec = {
1978
2013
// ZlibDecompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
1979
2014
// which prevents to create a subclass.
1980
2015
// So calling PyType_GetModuleState() in this file is always safe.
1981
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ),
2016
+ .flags = (
2017
+ Py_TPFLAGS_DEFAULT
2018
+ | Py_TPFLAGS_IMMUTABLETYPE
2019
+ | Py_TPFLAGS_HAVE_GC
2020
+ ),
1982
2021
.slots = ZlibDecompressor_type_slots ,
1983
2022
};
1984
2023
PyDoc_STRVAR (zlib_module_documentation ,
0 commit comments