Skip to content

Commit 3802c6f

Browse files
Revert "pythongh-116946: remove unnecessary gc from immutable types (python#139073)"
This reverts commit 1588413.
1 parent f39dea3 commit 3802c6f

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

Modules/_bz2module.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,13 @@ BZ2Compressor_dealloc(PyObject *op)
391391
Py_DECREF(tp);
392392
}
393393

394+
static int
395+
BZ2Compressor_traverse(PyObject *self, visitproc visit, void *arg)
396+
{
397+
Py_VISIT(Py_TYPE(self));
398+
return 0;
399+
}
400+
394401
static PyMethodDef BZ2Compressor_methods[] = {
395402
_BZ2_BZ2COMPRESSOR_COMPRESS_METHODDEF
396403
_BZ2_BZ2COMPRESSOR_FLUSH_METHODDEF
@@ -402,6 +409,7 @@ static PyType_Slot bz2_compressor_type_slots[] = {
402409
{Py_tp_methods, BZ2Compressor_methods},
403410
{Py_tp_new, _bz2_BZ2Compressor},
404411
{Py_tp_doc, (char *)_bz2_BZ2Compressor__doc__},
412+
{Py_tp_traverse, BZ2Compressor_traverse},
405413
{0, 0}
406414
};
407415

@@ -693,6 +701,13 @@ BZ2Decompressor_dealloc(PyObject *op)
693701
Py_DECREF(tp);
694702
}
695703

704+
static int
705+
BZ2Decompressor_traverse(PyObject *self, visitproc visit, void *arg)
706+
{
707+
Py_VISIT(Py_TYPE(self));
708+
return 0;
709+
}
710+
696711
static PyMethodDef BZ2Decompressor_methods[] = {
697712
_BZ2_BZ2DECOMPRESSOR_DECOMPRESS_METHODDEF
698713
{NULL}
@@ -723,6 +738,7 @@ static PyType_Slot bz2_decompressor_type_slots[] = {
723738
{Py_tp_doc, (char *)_bz2_BZ2Decompressor__doc__},
724739
{Py_tp_members, BZ2Decompressor_members},
725740
{Py_tp_new, _bz2_BZ2Decompressor},
741+
{Py_tp_traverse, BZ2Decompressor_traverse},
726742
{0, 0}
727743
};
728744

Modules/_lzmamodule.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,13 @@ static PyMethodDef Compressor_methods[] = {
882882
{NULL}
883883
};
884884

885+
static int
886+
Compressor_traverse(PyObject *self, visitproc visit, void *arg)
887+
{
888+
Py_VISIT(Py_TYPE(self));
889+
return 0;
890+
}
891+
885892
PyDoc_STRVAR(Compressor_doc,
886893
"LZMACompressor(format=FORMAT_XZ, check=-1, preset=None, filters=None)\n"
887894
"\n"
@@ -915,6 +922,7 @@ static PyType_Slot lzma_compressor_type_slots[] = {
915922
{Py_tp_methods, Compressor_methods},
916923
{Py_tp_new, Compressor_new},
917924
{Py_tp_doc, (char *)Compressor_doc},
925+
{Py_tp_traverse, Compressor_traverse},
918926
{0, 0}
919927
};
920928

@@ -1317,6 +1325,13 @@ Decompressor_dealloc(PyObject *op)
13171325
Py_DECREF(tp);
13181326
}
13191327

1328+
static int
1329+
Decompressor_traverse(PyObject *self, visitproc visit, void *arg)
1330+
{
1331+
Py_VISIT(Py_TYPE(self));
1332+
return 0;
1333+
}
1334+
13201335
static PyMethodDef Decompressor_methods[] = {
13211336
_LZMA_LZMADECOMPRESSOR_DECOMPRESS_METHODDEF
13221337
{NULL}
@@ -1351,6 +1366,7 @@ static PyType_Slot lzma_decompressor_type_slots[] = {
13511366
{Py_tp_methods, Decompressor_methods},
13521367
{Py_tp_new, _lzma_LZMADecompressor},
13531368
{Py_tp_doc, (char *)_lzma_LZMADecompressor__doc__},
1369+
{Py_tp_traverse, Decompressor_traverse},
13541370
{Py_tp_members, Decompressor_members},
13551371
{0, 0}
13561372
};

Modules/_ssl.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5793,6 +5793,7 @@ memory_bio_dealloc(PyObject *op)
57935793
{
57945794
PySSLMemoryBIO *self = PySSLMemoryBIO_CAST(op);
57955795
PyTypeObject *tp = Py_TYPE(self);
5796+
PyObject_GC_UnTrack(self);
57965797
(void)BIO_free(self->bio);
57975798
tp->tp_free(self);
57985799
Py_DECREF(tp);
@@ -5956,13 +5957,15 @@ static PyType_Slot PySSLMemoryBIO_slots[] = {
59565957
{Py_tp_getset, memory_bio_getsetlist},
59575958
{Py_tp_new, _ssl_MemoryBIO},
59585959
{Py_tp_dealloc, memory_bio_dealloc},
5960+
{Py_tp_traverse, _PyObject_VisitType},
59595961
{0, 0},
59605962
};
59615963

59625964
static PyType_Spec PySSLMemoryBIO_spec = {
59635965
.name = "_ssl.MemoryBIO",
59645966
.basicsize = sizeof(PySSLMemoryBIO),
5965-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
5967+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
5968+
Py_TPFLAGS_HAVE_GC),
59665969
.slots = PySSLMemoryBIO_slots,
59675970
};
59685971

Modules/_threadmodule.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ static void
660660
PyThreadHandleObject_dealloc(PyObject *op)
661661
{
662662
PyThreadHandleObject *self = PyThreadHandleObject_CAST(op);
663+
PyObject_GC_UnTrack(self);
663664
PyTypeObject *tp = Py_TYPE(self);
664665
ThreadHandle_decref(self->handle);
665666
tp->tp_free(self);
@@ -747,6 +748,7 @@ static PyType_Slot ThreadHandle_Type_slots[] = {
747748
{Py_tp_dealloc, PyThreadHandleObject_dealloc},
748749
{Py_tp_repr, PyThreadHandleObject_repr},
749750
{Py_tp_getset, ThreadHandle_getsetlist},
751+
{Py_tp_traverse, _PyObject_VisitType},
750752
{Py_tp_methods, ThreadHandle_methods},
751753
{Py_tp_new, PyThreadHandleObject_tp_new},
752754
{0, 0}
@@ -756,7 +758,7 @@ static PyType_Spec ThreadHandle_Type_spec = {
756758
"_thread._ThreadHandle",
757759
sizeof(PyThreadHandleObject),
758760
0,
759-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
761+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC,
760762
ThreadHandle_Type_slots,
761763
};
762764

Modules/hmacmodule.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ _hmac_new_impl(PyObject *module, PyObject *keyobj, PyObject *msgobj,
756756
return NULL;
757757
}
758758

759-
HMACObject *self = PyObject_New(HMACObject, state->hmac_type);
759+
HMACObject *self = PyObject_GC_New(HMACObject, state->hmac_type);
760760
if (self == NULL) {
761761
return NULL;
762762
}
@@ -791,6 +791,7 @@ _hmac_new_impl(PyObject *module, PyObject *keyobj, PyObject *msgobj,
791791
#endif
792792
}
793793
assert(rc == 0);
794+
PyObject_GC_Track(self);
794795
return (PyObject *)self;
795796

796797
error_on_key:
@@ -851,7 +852,7 @@ _hmac_HMAC_copy_impl(HMACObject *self, PyTypeObject *cls)
851852
/*[clinic end generated code: output=a955bfa55b65b215 input=17b2c0ad0b147e36]*/
852853
{
853854
hmacmodule_state *state = get_hmacmodule_state_by_cls(cls);
854-
HMACObject *copy = PyObject_New(HMACObject, state->hmac_type);
855+
HMACObject *copy = PyObject_GC_New(HMACObject, state->hmac_type);
855856
if (copy == NULL) {
856857
return NULL;
857858
}
@@ -869,6 +870,7 @@ _hmac_HMAC_copy_impl(HMACObject *self, PyTypeObject *cls)
869870
}
870871

871872
HASHLIB_INIT_MUTEX(copy);
873+
PyObject_GC_Track(copy);
872874
return (PyObject *)copy;
873875
}
874876

@@ -1024,6 +1026,7 @@ static void
10241026
HMACObject_dealloc(PyObject *op)
10251027
{
10261028
PyTypeObject *type = Py_TYPE(op);
1029+
PyObject_GC_UnTrack(op);
10271030
(void)HMACObject_clear(op);
10281031
type->tp_free(op);
10291032
Py_DECREF(type);
@@ -1048,7 +1051,9 @@ static PyType_Slot HMACObject_Type_slots[] = {
10481051
{Py_tp_repr, HMACObject_repr},
10491052
{Py_tp_methods, HMACObject_methods},
10501053
{Py_tp_getset, HMACObject_getsets},
1054+
{Py_tp_clear, HMACObject_clear},
10511055
{Py_tp_dealloc, HMACObject_dealloc},
1056+
{Py_tp_traverse, _PyObject_VisitType},
10521057
{0, NULL} /* sentinel */
10531058
};
10541059

@@ -1058,7 +1063,8 @@ static PyType_Spec HMAC_Type_spec = {
10581063
.flags = Py_TPFLAGS_DEFAULT
10591064
| Py_TPFLAGS_DISALLOW_INSTANTIATION
10601065
| Py_TPFLAGS_HEAPTYPE
1061-
| Py_TPFLAGS_IMMUTABLETYPE,
1066+
| Py_TPFLAGS_IMMUTABLETYPE
1067+
| Py_TPFLAGS_HAVE_GC,
10621068
.slots = HMACObject_Type_slots,
10631069
};
10641070

0 commit comments

Comments
 (0)