Skip to content

Commit c9b252c

Browse files
authored
gh-116946: Revert GC protocol for immutable empty heap types (GH-138266, GH-138288, GH-138289) (#138338)
* Revert "gh-116946: fully implement GC protocol for `bz2` objects (#138266)" This reverts commit 9be91f6. * Revert "gh-116946: fully implement GC protocol for `lzma` objects (#138288)" This reverts commit 3ea16f9. * Revert "gh-116946: fully implement GC protocol for `_hashlib` objects (#138289)" This reverts commit 6f1dd95.
1 parent 9b38c66 commit c9b252c

File tree

3 files changed

+22
-56
lines changed

3 files changed

+22
-56
lines changed

Modules/_bz2module.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,13 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
381381
static void
382382
BZ2Compressor_dealloc(PyObject *op)
383383
{
384-
PyTypeObject *tp = Py_TYPE(op);
385-
PyObject_GC_UnTrack(op);
386384
BZ2Compressor *self = _BZ2Compressor_CAST(op);
387385
BZ2_bzCompressEnd(&self->bzs);
388386
if (self->lock != NULL) {
389387
PyThread_free_lock(self->lock);
390388
}
391-
tp->tp_free(self);
389+
PyTypeObject *tp = Py_TYPE(self);
390+
tp->tp_free((PyObject *)self);
392391
Py_DECREF(tp);
393392
}
394393

@@ -421,7 +420,7 @@ static PyType_Spec bz2_compressor_type_spec = {
421420
// bz2_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
422421
// which prevents to create a subclass.
423422
// So calling PyType_GetModuleState() in this file is always safe.
424-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
423+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
425424
.slots = bz2_compressor_type_slots,
426425
};
427426

@@ -688,19 +687,19 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
688687
static void
689688
BZ2Decompressor_dealloc(PyObject *op)
690689
{
691-
PyTypeObject *tp = Py_TYPE(op);
692-
PyObject_GC_UnTrack(op);
693690
BZ2Decompressor *self = _BZ2Decompressor_CAST(op);
694691

695-
if (self->input_buffer != NULL) {
692+
if(self->input_buffer != NULL) {
696693
PyMem_Free(self->input_buffer);
697694
}
698695
BZ2_bzDecompressEnd(&self->bzs);
699696
Py_CLEAR(self->unused_data);
700697
if (self->lock != NULL) {
701698
PyThread_free_lock(self->lock);
702699
}
703-
tp->tp_free(self);
700+
701+
PyTypeObject *tp = Py_TYPE(self);
702+
tp->tp_free((PyObject *)self);
704703
Py_DECREF(tp);
705704
}
706705

@@ -752,7 +751,7 @@ static PyType_Spec bz2_decompressor_type_spec = {
752751
// bz2_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
753752
// which prevents to create a subclass.
754753
// So calling PyType_GetModuleState() in this file is always safe.
755-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
754+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
756755
.slots = bz2_decompressor_type_slots,
757756
};
758757

Modules/_hashopenssl.c

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -752,9 +752,7 @@ py_wrapper_EVP_MD_CTX_new(void)
752752
static HASHobject *
753753
new_hash_object(PyTypeObject *type)
754754
{
755-
assert(type != NULL);
756-
assert(type->tp_alloc != NULL);
757-
HASHobject *retval = (HASHobject *)type->tp_alloc(type, 0);
755+
HASHobject *retval = PyObject_New(HASHobject, type);
758756
if (retval == NULL) {
759757
return NULL;
760758
}
@@ -794,21 +792,13 @@ _hashlib_HASH_hash(HASHobject *self, const void *vp, Py_ssize_t len)
794792
static void
795793
_hashlib_HASH_dealloc(PyObject *op)
796794
{
797-
PyTypeObject *tp = Py_TYPE(op);
798-
PyObject_GC_UnTrack(op);
799795
HASHobject *self = HASHobject_CAST(op);
796+
PyTypeObject *tp = Py_TYPE(self);
800797
EVP_MD_CTX_free(self->ctx);
801-
tp->tp_free(self);
798+
PyObject_Free(self);
802799
Py_DECREF(tp);
803800
}
804801

805-
static int
806-
_hashlib_HASH_traverse(PyObject *op, visitproc visit, void *arg)
807-
{
808-
Py_VISIT(Py_TYPE(op));
809-
return 0;
810-
}
811-
812802
static int
813803
_hashlib_HASH_copy_locked(HASHobject *self, EVP_MD_CTX *new_ctx_p)
814804
{
@@ -1003,7 +993,6 @@ PyDoc_STRVAR(HASHobject_type_doc,
1003993

1004994
static PyType_Slot HASHobject_type_slots[] = {
1005995
{Py_tp_dealloc, _hashlib_HASH_dealloc},
1006-
{Py_tp_traverse, _hashlib_HASH_traverse},
1007996
{Py_tp_repr, _hashlib_HASH_repr},
1008997
{Py_tp_doc, (char *)HASHobject_type_doc},
1009998
{Py_tp_methods, HASH_methods},
@@ -1019,7 +1008,6 @@ static PyType_Spec HASHobject_type_spec = {
10191008
| Py_TPFLAGS_BASETYPE
10201009
| Py_TPFLAGS_DISALLOW_INSTANTIATION
10211010
| Py_TPFLAGS_IMMUTABLETYPE
1022-
| Py_TPFLAGS_HAVE_GC
10231011
),
10241012
.slots = HASHobject_type_slots
10251013
};
@@ -1177,8 +1165,6 @@ PyDoc_STRVAR(HASHXOFobject_type_doc,
11771165
"digest_size -- number of bytes in this hashes output");
11781166

11791167
static PyType_Slot HASHXOFobject_type_slots[] = {
1180-
{Py_tp_dealloc, _hashlib_HASH_dealloc},
1181-
{Py_tp_traverse, _hashlib_HASH_traverse},
11821168
{Py_tp_doc, (char *)HASHXOFobject_type_doc},
11831169
{Py_tp_methods, HASHXOFobject_methods},
11841170
{Py_tp_getset, HASHXOFobject_getsets},
@@ -1193,7 +1179,6 @@ static PyType_Spec HASHXOFobject_type_spec = {
11931179
| Py_TPFLAGS_BASETYPE
11941180
| Py_TPFLAGS_DISALLOW_INSTANTIATION
11951181
| Py_TPFLAGS_IMMUTABLETYPE
1196-
| Py_TPFLAGS_HAVE_GC
11971182
),
11981183
.slots = HASHXOFobject_type_slots
11991184
};
@@ -1917,8 +1902,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
19171902
goto error;
19181903
}
19191904

1920-
assert(state->HMAC_type != NULL);
1921-
self = (HMACobject *)state->HMAC_type->tp_alloc(state->HMAC_type, 0);
1905+
self = PyObject_New(HMACobject, state->HMAC_type);
19221906
if (self == NULL) {
19231907
goto error;
19241908
}
@@ -2024,8 +2008,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
20242008
return NULL;
20252009
}
20262010

2027-
PyTypeObject *type = Py_TYPE(self);
2028-
retval = (HMACobject *)type->tp_alloc(type, 0);
2011+
retval = PyObject_New(HMACobject, Py_TYPE(self));
20292012
if (retval == NULL) {
20302013
HMAC_CTX_free(ctx);
20312014
return NULL;
@@ -2039,24 +2022,16 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
20392022
static void
20402023
_hmac_dealloc(PyObject *op)
20412024
{
2042-
PyTypeObject *tp = Py_TYPE(op);
2043-
PyObject_GC_UnTrack(op);
20442025
HMACobject *self = HMACobject_CAST(op);
2026+
PyTypeObject *tp = Py_TYPE(self);
20452027
if (self->ctx != NULL) {
20462028
HMAC_CTX_free(self->ctx);
20472029
self->ctx = NULL;
20482030
}
2049-
tp->tp_free(self);
2031+
PyObject_Free(self);
20502032
Py_DECREF(tp);
20512033
}
20522034

2053-
static int
2054-
_hashlib_HMAC_traverse(PyObject *op, visitproc visit, void *arg)
2055-
{
2056-
Py_VISIT(Py_TYPE(op));
2057-
return 0;
2058-
}
2059-
20602035
static PyObject *
20612036
_hmac_repr(PyObject *op)
20622037
{
@@ -2223,21 +2198,15 @@ static PyType_Slot HMACtype_slots[] = {
22232198
{Py_tp_doc, (char *)hmactype_doc},
22242199
{Py_tp_repr, _hmac_repr},
22252200
{Py_tp_dealloc, _hmac_dealloc},
2226-
{Py_tp_traverse, _hashlib_HMAC_traverse},
22272201
{Py_tp_methods, HMAC_methods},
22282202
{Py_tp_getset, HMAC_getset},
22292203
{0, NULL}
22302204
};
22312205

22322206
PyType_Spec HMACtype_spec = {
2233-
.name = "_hashlib.HMAC",
2234-
.basicsize = sizeof(HMACobject),
2235-
.flags = (
2236-
Py_TPFLAGS_DEFAULT
2237-
| Py_TPFLAGS_DISALLOW_INSTANTIATION
2238-
| Py_TPFLAGS_IMMUTABLETYPE
2239-
| Py_TPFLAGS_HAVE_GC
2240-
),
2207+
"_hashlib.HMAC", /* name */
2208+
sizeof(HMACobject), /* basicsize */
2209+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
22412210
.slots = HMACtype_slots,
22422211
};
22432212

Modules/_lzmamodule.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -866,13 +866,12 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
866866
static void
867867
Compressor_dealloc(PyObject *op)
868868
{
869-
PyTypeObject *tp = Py_TYPE(op);
870-
PyObject_GC_UnTrack(op);
871869
Compressor *self = Compressor_CAST(op);
872870
lzma_end(&self->lzs);
873871
if (self->lock != NULL) {
874872
PyThread_free_lock(self->lock);
875873
}
874+
PyTypeObject *tp = Py_TYPE(self);
876875
tp->tp_free(self);
877876
Py_DECREF(tp);
878877
}
@@ -934,7 +933,7 @@ static PyType_Spec lzma_compressor_type_spec = {
934933
// lzma_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
935934
// which prevents to create a subclass.
936935
// So calling PyType_GetModuleState() in this file is always safe.
937-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
936+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
938937
.slots = lzma_compressor_type_slots,
939938
};
940939

@@ -1315,8 +1314,6 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
13151314
static void
13161315
Decompressor_dealloc(PyObject *op)
13171316
{
1318-
PyTypeObject *tp = Py_TYPE(op);
1319-
PyObject_GC_UnTrack(op);
13201317
Decompressor *self = Decompressor_CAST(op);
13211318
if(self->input_buffer != NULL)
13221319
PyMem_Free(self->input_buffer);
@@ -1326,6 +1323,7 @@ Decompressor_dealloc(PyObject *op)
13261323
if (self->lock != NULL) {
13271324
PyThread_free_lock(self->lock);
13281325
}
1326+
PyTypeObject *tp = Py_TYPE(self);
13291327
tp->tp_free(self);
13301328
Py_DECREF(tp);
13311329
}
@@ -1383,7 +1381,7 @@ static PyType_Spec lzma_decompressor_type_spec = {
13831381
// lzma_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
13841382
// which prevents to create a subclass.
13851383
// So calling PyType_GetModuleState() in this file is always safe.
1386-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
1384+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
13871385
.slots = lzma_decompressor_type_slots,
13881386
};
13891387

0 commit comments

Comments
 (0)