@@ -273,7 +273,9 @@ static compobject *
273
273
newcompobject (PyTypeObject * type )
274
274
{
275
275
compobject * self ;
276
- self = PyObject_New (compobject , type );
276
+ assert (type != NULL );
277
+ assert (type -> tp_alloc != NULL );
278
+ self = _compobject_CAST (type -> tp_alloc (type , 0 ));
277
279
if (self == NULL )
278
280
return NULL ;
279
281
self -> eof = 0 ;
@@ -716,33 +718,41 @@ zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
716
718
}
717
719
718
720
static void
719
- Dealloc ( compobject * self )
721
+ compobject_dealloc_impl ( PyObject * op , int ( * dealloc )( z_streamp ) )
720
722
{
721
- PyTypeObject * type = Py_TYPE (self );
723
+ PyTypeObject * type = Py_TYPE (op );
724
+ PyObject_GC_UnTrack (op );
725
+ compobject * self = _compobject_CAST (op );
726
+ if (self -> is_initialised ) {
727
+ (void )dealloc (& self -> zst );
728
+ }
722
729
PyThread_free_lock (self -> lock );
723
730
Py_XDECREF (self -> unused_data );
724
731
Py_XDECREF (self -> unconsumed_tail );
725
732
Py_XDECREF (self -> zdict );
726
- PyObject_Free (self );
733
+ type -> tp_free (self );
727
734
Py_DECREF (type );
728
735
}
729
736
737
+ static int
738
+ compobject_traverse (PyObject * op , visitproc visit , void * arg )
739
+ {
740
+ compobject * self = _compobject_CAST (op );
741
+ Py_VISIT (Py_TYPE (op ));
742
+ Py_VISIT (self -> zdict );
743
+ return 0 ;
744
+ }
745
+
730
746
static void
731
747
Comp_dealloc (PyObject * op )
732
748
{
733
- compobject * self = _compobject_CAST (op );
734
- if (self -> is_initialised )
735
- (void )deflateEnd (& self -> zst );
736
- Dealloc (self );
749
+ compobject_dealloc_impl (op , & deflateEnd );
737
750
}
738
751
739
752
static void
740
753
Decomp_dealloc (PyObject * op )
741
754
{
742
- compobject * self = _compobject_CAST (op );
743
- if (self -> is_initialised )
744
- (void )inflateEnd (& self -> zst );
745
- Dealloc (self );
755
+ compobject_dealloc_impl (op , & inflateEnd );
746
756
}
747
757
748
758
/*[clinic input]
@@ -1368,6 +1378,8 @@ typedef struct {
1368
1378
char needs_input ;
1369
1379
} ZlibDecompressor ;
1370
1380
1381
+ #define ZlibDecompressor_CAST (op ) ((ZlibDecompressor *)(op))
1382
+
1371
1383
/*[clinic input]
1372
1384
class zlib._ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
1373
1385
[clinic start generated code]*/
@@ -1376,19 +1388,29 @@ class zlib._ZlibDecompressor "ZlibDecompressor *" "&ZlibDecompressorType"
1376
1388
static void
1377
1389
ZlibDecompressor_dealloc (PyObject * op )
1378
1390
{
1379
- ZlibDecompressor * self = (ZlibDecompressor * )op ;
1380
- PyObject * type = (PyObject * )Py_TYPE (self );
1391
+ PyTypeObject * type = Py_TYPE (op );
1392
+ PyObject_GC_UnTrack (op );
1393
+ ZlibDecompressor * self = ZlibDecompressor_CAST (op );
1381
1394
PyThread_free_lock (self -> lock );
1382
1395
if (self -> is_initialised ) {
1383
1396
inflateEnd (& self -> zst );
1384
1397
}
1385
1398
PyMem_Free (self -> input_buffer );
1386
1399
Py_CLEAR (self -> unused_data );
1387
1400
Py_CLEAR (self -> zdict );
1388
- PyObject_Free (self );
1401
+ type -> tp_free (self );
1389
1402
Py_DECREF (type );
1390
1403
}
1391
1404
1405
+ static int
1406
+ ZlibDecompressor_traverse (PyObject * op , visitproc visit , void * arg )
1407
+ {
1408
+ ZlibDecompressor * self = ZlibDecompressor_CAST (op );
1409
+ Py_VISIT (Py_TYPE (op ));
1410
+ Py_VISIT (self -> zdict );
1411
+ return 0 ;
1412
+ }
1413
+
1392
1414
static int
1393
1415
set_inflate_zdict_ZlibDecompressor (zlibstate * state , ZlibDecompressor * self )
1394
1416
{
@@ -1731,8 +1753,9 @@ static PyObject *
1731
1753
zlib__ZlibDecompressor_impl (PyTypeObject * type , int wbits , PyObject * zdict )
1732
1754
/*[clinic end generated code: output=1065607df0d33baa input=9ebad0be6de226e2]*/
1733
1755
{
1756
+ assert (type != NULL && type -> tp_alloc != NULL );
1734
1757
zlibstate * state = PyType_GetModuleState (type );
1735
- ZlibDecompressor * self = PyObject_New ( ZlibDecompressor , type );
1758
+ ZlibDecompressor * self = ZlibDecompressor_CAST ( type -> tp_alloc ( type , 0 ) );
1736
1759
if (self == NULL ) {
1737
1760
return NULL ;
1738
1761
}
@@ -2015,19 +2038,25 @@ static PyMethodDef zlib_methods[] =
2015
2038
2016
2039
static PyType_Slot Comptype_slots [] = {
2017
2040
{Py_tp_dealloc , Comp_dealloc },
2041
+ {Py_tp_traverse , compobject_traverse },
2018
2042
{Py_tp_methods , comp_methods },
2019
2043
{0 , 0 },
2020
2044
};
2021
2045
2022
2046
static PyType_Spec Comptype_spec = {
2023
2047
.name = "zlib.Compress" ,
2024
2048
.basicsize = sizeof (compobject ),
2025
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
2049
+ .flags = (
2050
+ Py_TPFLAGS_DEFAULT
2051
+ | Py_TPFLAGS_DISALLOW_INSTANTIATION
2052
+ | Py_TPFLAGS_HAVE_GC
2053
+ ),
2026
2054
.slots = Comptype_slots ,
2027
2055
};
2028
2056
2029
2057
static PyType_Slot Decomptype_slots [] = {
2030
2058
{Py_tp_dealloc , Decomp_dealloc },
2059
+ {Py_tp_traverse , compobject_traverse },
2031
2060
{Py_tp_methods , Decomp_methods },
2032
2061
{Py_tp_members , Decomp_members },
2033
2062
{0 , 0 },
@@ -2036,12 +2065,17 @@ static PyType_Slot Decomptype_slots[] = {
2036
2065
static PyType_Spec Decomptype_spec = {
2037
2066
.name = "zlib.Decompress" ,
2038
2067
.basicsize = sizeof (compobject ),
2039
- .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION ,
2068
+ .flags = (
2069
+ Py_TPFLAGS_DEFAULT
2070
+ | Py_TPFLAGS_DISALLOW_INSTANTIATION
2071
+ | Py_TPFLAGS_HAVE_GC
2072
+ ),
2040
2073
.slots = Decomptype_slots ,
2041
2074
};
2042
2075
2043
2076
static PyType_Slot ZlibDecompressor_type_slots [] = {
2044
2077
{Py_tp_dealloc , ZlibDecompressor_dealloc },
2078
+ {Py_tp_traverse , ZlibDecompressor_traverse },
2045
2079
{Py_tp_members , ZlibDecompressor_members },
2046
2080
{Py_tp_new , zlib__ZlibDecompressor },
2047
2081
{Py_tp_doc , (char * )zlib__ZlibDecompressor__doc__ },
@@ -2056,7 +2090,11 @@ static PyType_Spec ZlibDecompressor_type_spec = {
2056
2090
// ZlibDecompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
2057
2091
// which prevents to create a subclass.
2058
2092
// So calling PyType_GetModuleState() in this file is always safe.
2059
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE ),
2093
+ .flags = (
2094
+ Py_TPFLAGS_DEFAULT
2095
+ | Py_TPFLAGS_IMMUTABLETYPE
2096
+ | Py_TPFLAGS_HAVE_GC
2097
+ ),
2060
2098
.slots = ZlibDecompressor_type_slots ,
2061
2099
};
2062
2100
0 commit comments