Skip to content

Commit a38f026

Browse files
authored
[3.13] gh-116946: Revert GC protocol for immutable empty heap types (GH-138322, GH-138323, GH-138326) (#138337)
* Revert "[3.13] gh-116946: fully implement GC protocol for `bz2` objects (GH-138266) (#138322)" This reverts commit 90036f5. * Revert "[3.13] gh-116946: fully implement GC protocol for `lzma` objects (GH-138288) (#138323)" This reverts commit 828682d. * Revert "[3.13] gh-116946: fully implement GC protocol for `_hashlib` objects (GH-138289) (#138326)" This reverts commit 21b5932.
1 parent d25d2d6 commit a38f026

File tree

3 files changed

+44
-102
lines changed

3 files changed

+44
-102
lines changed

Modules/_bz2module.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,6 @@ typedef struct {
129129
PyThread_type_lock lock;
130130
} BZ2Decompressor;
131131

132-
#define _BZ2Compressor_CAST(op) ((BZ2Compressor *)(op))
133-
#define _BZ2Decompressor_CAST(op) ((BZ2Decompressor *)(op))
134-
135132
/* Helper functions. */
136133

137134
static int
@@ -379,21 +376,19 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
379376
}
380377

381378
static void
382-
BZ2Compressor_dealloc(PyObject *op)
379+
BZ2Compressor_dealloc(BZ2Compressor *self)
383380
{
384-
PyTypeObject *tp = Py_TYPE(op);
385-
PyObject_GC_UnTrack(op);
386-
BZ2Compressor *self = _BZ2Compressor_CAST(op);
387381
BZ2_bzCompressEnd(&self->bzs);
388382
if (self->lock != NULL) {
389383
PyThread_free_lock(self->lock);
390384
}
391-
tp->tp_free(self);
385+
PyTypeObject *tp = Py_TYPE(self);
386+
tp->tp_free((PyObject *)self);
392387
Py_DECREF(tp);
393388
}
394389

395390
static int
396-
BZ2Compressor_traverse(PyObject *self, visitproc visit, void *arg)
391+
BZ2Compressor_traverse(BZ2Compressor *self, visitproc visit, void *arg)
397392
{
398393
Py_VISIT(Py_TYPE(self));
399394
return 0;
@@ -421,7 +416,7 @@ static PyType_Spec bz2_compressor_type_spec = {
421416
// bz2_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
422417
// which prevents to create a subclass.
423418
// So calling PyType_GetModuleState() in this file is always safe.
424-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
419+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
425420
.slots = bz2_compressor_type_slots,
426421
};
427422

@@ -685,11 +680,8 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
685680
}
686681

687682
static void
688-
BZ2Decompressor_dealloc(PyObject *op)
683+
BZ2Decompressor_dealloc(BZ2Decompressor *self)
689684
{
690-
PyTypeObject *tp = Py_TYPE(op);
691-
PyObject_GC_UnTrack(op);
692-
BZ2Decompressor *self = _BZ2Decompressor_CAST(op);
693685
if(self->input_buffer != NULL) {
694686
PyMem_Free(self->input_buffer);
695687
}
@@ -698,12 +690,14 @@ BZ2Decompressor_dealloc(PyObject *op)
698690
if (self->lock != NULL) {
699691
PyThread_free_lock(self->lock);
700692
}
701-
tp->tp_free(self);
693+
694+
PyTypeObject *tp = Py_TYPE(self);
695+
tp->tp_free((PyObject *)self);
702696
Py_DECREF(tp);
703697
}
704698

705699
static int
706-
BZ2Decompressor_traverse(PyObject *self, visitproc visit, void *arg)
700+
BZ2Decompressor_traverse(BZ2Decompressor *self, visitproc visit, void *arg)
707701
{
708702
Py_VISIT(Py_TYPE(self));
709703
return 0;
@@ -750,7 +744,7 @@ static PyType_Spec bz2_decompressor_type_spec = {
750744
// bz2_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
751745
// which prevents to create a subclass.
752746
// So calling PyType_GetModuleState() in this file is always safe.
753-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
747+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
754748
.slots = bz2_decompressor_type_slots,
755749
};
756750

Modules/_hashopenssl.c

Lines changed: 23 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,6 @@ typedef struct {
282282
PyMutex mutex; /* OpenSSL context lock */
283283
} EVPobject;
284284

285-
#define EVPobject_CAST(op) ((EVPobject *)(op))
286-
287285
typedef struct {
288286
PyObject_HEAD
289287
HMAC_CTX *ctx; /* OpenSSL hmac context */
@@ -292,8 +290,6 @@ typedef struct {
292290
PyMutex mutex; /* HMAC context lock */
293291
} HMACobject;
294292

295-
#define HMACobject_CAST(op) ((HMACobject *)(op))
296-
297293
#include "clinic/_hashopenssl.c.h"
298294
/*[clinic input]
299295
module _hashlib
@@ -501,9 +497,7 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type
501497
static EVPobject *
502498
newEVPobject(PyTypeObject *type)
503499
{
504-
assert(type != NULL);
505-
assert(type->tp_alloc != NULL);
506-
EVPobject *retval = (EVPobject *)type->tp_alloc(type, 0);
500+
EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, type);
507501
if (retval == NULL) {
508502
return NULL;
509503
}
@@ -542,23 +536,14 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
542536
/* Internal methods for a hash object */
543537

544538
static void
545-
EVP_dealloc(PyObject *op)
539+
EVP_dealloc(EVPobject *self)
546540
{
547-
PyTypeObject *tp = Py_TYPE(op);
548-
PyObject_GC_UnTrack(op);
549-
EVPobject *self = EVPobject_CAST(op);
541+
PyTypeObject *tp = Py_TYPE(self);
550542
EVP_MD_CTX_free(self->ctx);
551-
tp->tp_free(self);
543+
PyObject_Free(self);
552544
Py_DECREF(tp);
553545
}
554546

555-
static int
556-
EVP_traverse(PyObject *op, visitproc visit, void *arg)
557-
{
558-
Py_VISIT(Py_TYPE(op));
559-
return 0;
560-
}
561-
562547
static int
563548
locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
564549
{
@@ -796,7 +781,6 @@ PyDoc_STRVAR(hashtype_doc,
796781

797782
static PyType_Slot EVPtype_slots[] = {
798783
{Py_tp_dealloc, EVP_dealloc},
799-
{Py_tp_traverse, EVP_traverse},
800784
{Py_tp_repr, EVP_repr},
801785
{Py_tp_doc, (char *)hashtype_doc},
802786
{Py_tp_methods, EVP_methods},
@@ -805,16 +789,11 @@ static PyType_Slot EVPtype_slots[] = {
805789
};
806790

807791
static PyType_Spec EVPtype_spec = {
808-
.name = "_hashlib.HASH",
809-
.basicsize = sizeof(EVPobject),
810-
.flags = (
811-
Py_TPFLAGS_DEFAULT
812-
| Py_TPFLAGS_BASETYPE
813-
| Py_TPFLAGS_DISALLOW_INSTANTIATION
814-
| Py_TPFLAGS_IMMUTABLETYPE
815-
| Py_TPFLAGS_HAVE_GC
816-
),
817-
.slots = EVPtype_slots
792+
"_hashlib.HASH", /*tp_name*/
793+
sizeof(EVPobject), /*tp_basicsize*/
794+
0, /*tp_itemsize*/
795+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
796+
EVPtype_slots
818797
};
819798

820799
#ifdef PY_OPENSSL_HAS_SHAKE
@@ -955,25 +934,18 @@ PyDoc_STRVAR(hashxoftype_doc,
955934
"digest_size -- number of bytes in this hashes output");
956935

957936
static PyType_Slot EVPXOFtype_slots[] = {
958-
{Py_tp_dealloc, EVP_dealloc},
959-
{Py_tp_traverse, EVP_traverse},
960937
{Py_tp_doc, (char *)hashxoftype_doc},
961938
{Py_tp_methods, EVPXOF_methods},
962939
{Py_tp_getset, EVPXOF_getseters},
963940
{0, 0},
964941
};
965942

966943
static PyType_Spec EVPXOFtype_spec = {
967-
.name = "_hashlib.HASHXOF",
968-
.basicsize = sizeof(EVPobject),
969-
.flags = (
970-
Py_TPFLAGS_DEFAULT
971-
| Py_TPFLAGS_BASETYPE
972-
| Py_TPFLAGS_DISALLOW_INSTANTIATION
973-
| Py_TPFLAGS_IMMUTABLETYPE
974-
| Py_TPFLAGS_HAVE_GC
975-
),
976-
.slots = EVPXOFtype_slots
944+
"_hashlib.HASHXOF", /*tp_name*/
945+
sizeof(EVPobject), /*tp_basicsize*/
946+
0, /*tp_itemsize*/
947+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
948+
EVPXOFtype_slots
977949
};
978950

979951

@@ -1687,8 +1659,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
16871659
}
16881660

16891661
_hashlibstate *state = get_hashlib_state(module);
1690-
assert(state->HMACtype != NULL);
1691-
self = (HMACobject *)state->HMACtype->tp_alloc(state->HMACtype, 0);
1662+
self = PyObject_New(HMACobject, state->HMACtype);
16921663
if (self == NULL) {
16931664
goto error;
16941665
}
@@ -1793,8 +1764,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
17931764
return NULL;
17941765
}
17951766

1796-
PyTypeObject *type = Py_TYPE(self);
1797-
retval = (HMACobject *)type->tp_alloc(type, 0);
1767+
retval = PyObject_New(HMACobject, Py_TYPE(self));
17981768
if (retval == NULL) {
17991769
HMAC_CTX_free(ctx);
18001770
return NULL;
@@ -1806,26 +1776,17 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
18061776
}
18071777

18081778
static void
1809-
_hmac_dealloc(PyObject *op)
1779+
_hmac_dealloc(HMACobject *self)
18101780
{
1811-
PyTypeObject *tp = Py_TYPE(op);
1812-
PyObject_GC_UnTrack(op);
1813-
HMACobject *self = HMACobject_CAST(op);
1781+
PyTypeObject *tp = Py_TYPE(self);
18141782
if (self->ctx != NULL) {
18151783
HMAC_CTX_free(self->ctx);
18161784
self->ctx = NULL;
18171785
}
1818-
tp->tp_free(self);
1786+
PyObject_Free(self);
18191787
Py_DECREF(tp);
18201788
}
18211789

1822-
static int
1823-
_hmac_traverse(PyObject *op, visitproc visit, void *arg)
1824-
{
1825-
Py_VISIT(Py_TYPE(op));
1826-
return 0;
1827-
}
1828-
18291790
static PyObject *
18301791
_hmac_repr(HMACobject *self)
18311792
{
@@ -1993,22 +1954,16 @@ digest_size -- number of bytes in digest() output\n");
19931954
static PyType_Slot HMACtype_slots[] = {
19941955
{Py_tp_doc, (char *)hmactype_doc},
19951956
{Py_tp_repr, (reprfunc)_hmac_repr},
1996-
{Py_tp_dealloc, _hmac_dealloc},
1997-
{Py_tp_traverse, _hmac_traverse},
1957+
{Py_tp_dealloc,(destructor)_hmac_dealloc},
19981958
{Py_tp_methods, HMAC_methods},
19991959
{Py_tp_getset, HMAC_getset},
20001960
{0, NULL}
20011961
};
20021962

20031963
PyType_Spec HMACtype_spec = {
2004-
.name = "_hashlib.HMAC",
2005-
.basicsize = sizeof(HMACobject),
2006-
.flags = (
2007-
Py_TPFLAGS_DEFAULT
2008-
| Py_TPFLAGS_DISALLOW_INSTANTIATION
2009-
| Py_TPFLAGS_IMMUTABLETYPE
2010-
| Py_TPFLAGS_HAVE_GC
2011-
),
1964+
"_hashlib.HMAC", /* name */
1965+
sizeof(HMACobject), /* basicsize */
1966+
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
20121967
.slots = HMACtype_slots,
20131968
};
20141969

Modules/_lzmamodule.c

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,6 @@ typedef struct {
126126
PyThread_type_lock lock;
127127
} Decompressor;
128128

129-
#define Compressor_CAST(op) ((Compressor *)(op))
130-
#define Decompressor_CAST(op) ((Decompressor *)(op))
131-
132129
/* Helper functions. */
133130

134131
static int
@@ -860,16 +857,14 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
860857
}
861858

862859
static void
863-
Compressor_dealloc(PyObject *op)
860+
Compressor_dealloc(Compressor *self)
864861
{
865-
PyTypeObject *tp = Py_TYPE(op);
866-
PyObject_GC_UnTrack(op);
867-
Compressor *self = Compressor_CAST(op);
868862
lzma_end(&self->lzs);
869863
if (self->lock != NULL) {
870864
PyThread_free_lock(self->lock);
871865
}
872-
tp->tp_free(self);
866+
PyTypeObject *tp = Py_TYPE(self);
867+
tp->tp_free((PyObject *)self);
873868
Py_DECREF(tp);
874869
}
875870

@@ -880,7 +875,7 @@ static PyMethodDef Compressor_methods[] = {
880875
};
881876

882877
static int
883-
Compressor_traverse(PyObject *self, visitproc visit, void *arg)
878+
Compressor_traverse(Compressor *self, visitproc visit, void *arg)
884879
{
885880
Py_VISIT(Py_TYPE(self));
886881
return 0;
@@ -930,7 +925,7 @@ static PyType_Spec lzma_compressor_type_spec = {
930925
// lzma_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
931926
// which prevents to create a subclass.
932927
// So calling PyType_GetModuleState() in this file is always safe.
933-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
928+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
934929
.slots = lzma_compressor_type_slots,
935930
};
936931

@@ -1309,11 +1304,8 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
13091304
}
13101305

13111306
static void
1312-
Decompressor_dealloc(PyObject *op)
1307+
Decompressor_dealloc(Decompressor *self)
13131308
{
1314-
PyTypeObject *tp = Py_TYPE(op);
1315-
PyObject_GC_UnTrack(op);
1316-
Decompressor *self = Decompressor_CAST(op);
13171309
if(self->input_buffer != NULL)
13181310
PyMem_Free(self->input_buffer);
13191311

@@ -1322,12 +1314,13 @@ Decompressor_dealloc(PyObject *op)
13221314
if (self->lock != NULL) {
13231315
PyThread_free_lock(self->lock);
13241316
}
1325-
tp->tp_free(self);
1317+
PyTypeObject *tp = Py_TYPE(self);
1318+
tp->tp_free((PyObject *)self);
13261319
Py_DECREF(tp);
13271320
}
13281321

13291322
static int
1330-
Decompressor_traverse(PyObject *self, visitproc visit, void *arg)
1323+
Decompressor_traverse(Decompressor *self, visitproc visit, void *arg)
13311324
{
13321325
Py_VISIT(Py_TYPE(self));
13331326
return 0;
@@ -1379,7 +1372,7 @@ static PyType_Spec lzma_decompressor_type_spec = {
13791372
// lzma_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
13801373
// which prevents to create a subclass.
13811374
// So calling PyType_GetModuleState() in this file is always safe.
1382-
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
1375+
.flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
13831376
.slots = lzma_decompressor_type_slots,
13841377
};
13851378

0 commit comments

Comments
 (0)