@@ -221,6 +221,8 @@ typedef struct
221
221
PyThread_type_lock lock ;
222
222
} compobject ;
223
223
224
+ #define _compobject_CAST (op ) ((compobject *)op)
225
+
224
226
static void
225
227
zlib_error (zlibstate * state , z_stream zst , int err , const char * msg )
226
228
{
@@ -261,7 +263,9 @@ static compobject *
261
263
newcompobject (PyTypeObject * type )
262
264
{
263
265
compobject * self ;
264
- self = PyObject_New (compobject , type );
266
+ assert (type != NULL );
267
+ assert (type -> tp_alloc != NULL );
268
+ self = _compobject_CAST (type -> tp_alloc (type , 0 ));
265
269
if (self == NULL )
266
270
return NULL ;
267
271
self -> eof = 0 ;
@@ -704,31 +708,41 @@ zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
704
708
}
705
709
706
710
static void
707
- Dealloc ( compobject * self )
711
+ compobject_dealloc_impl ( PyObject * op , int ( * dealloc )( z_streamp ) )
708
712
{
709
- PyObject * type = (PyObject * )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
+ }
710
719
PyThread_free_lock (self -> lock );
711
720
Py_XDECREF (self -> unused_data );
712
721
Py_XDECREF (self -> unconsumed_tail );
713
722
Py_XDECREF (self -> zdict );
714
- PyObject_Free (self );
723
+ type -> tp_free (self );
715
724
Py_DECREF (type );
716
725
}
717
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
+
718
736
static void
719
- Comp_dealloc (compobject * self )
737
+ Comp_dealloc (PyObject * op )
720
738
{
721
- if (self -> is_initialised )
722
- deflateEnd (& self -> zst );
723
- Dealloc (self );
739
+ compobject_dealloc_impl (op , & deflateEnd );
724
740
}
725
741
726
742
static void
727
- Decomp_dealloc (compobject * self )
743
+ Decomp_dealloc (PyObject * op )
728
744
{
729
- if (self -> is_initialised )
730
- inflateEnd (& self -> zst );
731
- Dealloc (self );
745
+ compobject_dealloc_impl (op , & inflateEnd );
732
746
}
733
747
734
748
/*[clinic input]
@@ -1353,26 +1367,39 @@ typedef struct {
1353
1367
char needs_input ;
1354
1368
} ZlibDecompressor ;
1355
1369
1370
+ #define ZlibDecompressor_CAST (op ) ((ZlibDecompressor *)(op))
1371
+
1356
1372
/*[clinic input]
1357
1373
class zlib.ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
1358
1374
[clinic start generated code]*/
1359
1375
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=0658178ab94645df]*/
1360
1376
1361
1377
static void
1362
- ZlibDecompressor_dealloc (ZlibDecompressor * self )
1378
+ ZlibDecompressor_dealloc (PyObject * op )
1363
1379
{
1364
- PyObject * type = (PyObject * )Py_TYPE (self );
1380
+ PyTypeObject * type = Py_TYPE (op );
1381
+ PyObject_GC_UnTrack (op );
1382
+ ZlibDecompressor * self = ZlibDecompressor_CAST (op );
1365
1383
PyThread_free_lock (self -> lock );
1366
1384
if (self -> is_initialised ) {
1367
1385
inflateEnd (& self -> zst );
1368
1386
}
1369
1387
PyMem_Free (self -> input_buffer );
1370
1388
Py_CLEAR (self -> unused_data );
1371
1389
Py_CLEAR (self -> zdict );
1372
- PyObject_Free (self );
1390
+ type -> tp_free (self );
1373
1391
Py_DECREF (type );
1374
1392
}
1375
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
+
1376
1403
static int
1377
1404
set_inflate_zdict_ZlibDecompressor (zlibstate * state , ZlibDecompressor * self )
1378
1405
{
@@ -1724,7 +1751,7 @@ ZlibDecompressor__new__(PyTypeObject *cls,
1724
1751
args , kwargs , format , keywords , & wbits , & zdict )) {
1725
1752
return NULL ;
1726
1753
}
1727
- ZlibDecompressor * self = PyObject_New ( ZlibDecompressor , cls );
1754
+ ZlibDecompressor * self = ZlibDecompressor_CAST ( cls -> tp_alloc ( cls , 0 ) );
1728
1755
if (self == NULL ) {
1729
1756
return NULL ;
1730
1757
}
@@ -1932,19 +1959,25 @@ static PyMethodDef zlib_methods[] =
1932
1959
1933
1960
static PyType_Slot Comptype_slots [] = {
1934
1961
{Py_tp_dealloc , Comp_dealloc },
1962
+ {Py_tp_traverse , compobject_traverse },
1935
1963
{Py_tp_methods , comp_methods },
1936
1964
{0 , 0 },
1937
1965
};
1938
1966
1939
1967
static PyType_Spec Comptype_spec = {
1940
1968
.name = "zlib.Compress" ,
1941
1969
.basicsize = sizeof (compobject ),
1942
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
1970
+ .flags = (
1971
+ Py_TPFLAGS_DEFAULT
1972
+ | Py_TPFLAGS_DISALLOW_INSTANTIATION
1973
+ | Py_TPFLAGS_HAVE_GC
1974
+ ),
1943
1975
.slots = Comptype_slots ,
1944
1976
};
1945
1977
1946
1978
static PyType_Slot Decomptype_slots [] = {
1947
1979
{Py_tp_dealloc , Decomp_dealloc },
1980
+ {Py_tp_traverse , compobject_traverse },
1948
1981
{Py_tp_methods , Decomp_methods },
1949
1982
{Py_tp_members , Decomp_members },
1950
1983
{0 , 0 },
@@ -1953,12 +1986,17 @@ static PyType_Slot Decomptype_slots[] = {
1953
1986
static PyType_Spec Decomptype_spec = {
1954
1987
.name = "zlib.Decompress" ,
1955
1988
.basicsize = sizeof (compobject ),
1956
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
1989
+ .flags = (
1990
+ Py_TPFLAGS_DEFAULT
1991
+ | Py_TPFLAGS_DISALLOW_INSTANTIATION
1992
+ | Py_TPFLAGS_HAVE_GC
1993
+ ),
1957
1994
.slots = Decomptype_slots ,
1958
1995
};
1959
1996
1960
1997
static PyType_Slot ZlibDecompressor_type_slots [] = {
1961
1998
{Py_tp_dealloc , ZlibDecompressor_dealloc },
1999
+ {Py_tp_traverse , ZlibDecompressor_traverse },
1962
2000
{Py_tp_members , ZlibDecompressor_members },
1963
2001
{Py_tp_new , ZlibDecompressor__new__ },
1964
2002
{Py_tp_doc , (char * )ZlibDecompressor__new____doc__ },
@@ -1973,7 +2011,11 @@ static PyType_Spec ZlibDecompressor_type_spec = {
1973
2011
// ZlibDecompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
1974
2012
// which prevents to create a subclass.
1975
2013
// So calling PyType_GetModuleState() in this file is always safe.
1976
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ),
2014
+ .flags = (
2015
+ Py_TPFLAGS_DEFAULT
2016
+ | Py_TPFLAGS_IMMUTABLETYPE
2017
+ | Py_TPFLAGS_HAVE_GC
2018
+ ),
1977
2019
.slots = ZlibDecompressor_type_slots ,
1978
2020
};
1979
2021
PyDoc_STRVAR (zlib_module_documentation ,
0 commit comments