https://github.com/python/cpython/commit/c9b252c2c007cd2cec3b33fc7476ae85dede8e42
commit: c9b252c2c007cd2cec3b33fc7476ae85dede8e42
branch: main
author: Bénédikt Tran <[email protected]>
committer: kumaraditya303 <[email protected]>
date: 2025-09-01T21:15:11+05:30
summary:
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 9be91f6a20ed2fd9b491c3e779dc45c7392f60ca.
* Revert "gh-116946: fully implement GC protocol for `lzma` objects (#138288)"
This reverts commit 3ea16f990f81e1e3b2892f1dfd213937b1df2a68.
* Revert "gh-116946: fully implement GC protocol for `_hashlib` objects
(#138289)"
This reverts commit 6f1dd9551a69c8c76d066a04e94db6dbc6c7597c.
files:
M Modules/_bz2module.c
M Modules/_hashopenssl.c
M Modules/_lzmamodule.c
diff --git a/Modules/_bz2module.c b/Modules/_bz2module.c
index d988901933703e..914172684158a1 100644
--- a/Modules/_bz2module.c
+++ b/Modules/_bz2module.c
@@ -381,14 +381,13 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int
compresslevel)
static void
BZ2Compressor_dealloc(PyObject *op)
{
- PyTypeObject *tp = Py_TYPE(op);
- PyObject_GC_UnTrack(op);
BZ2Compressor *self = _BZ2Compressor_CAST(op);
BZ2_bzCompressEnd(&self->bzs);
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}
- tp->tp_free(self);
+ PyTypeObject *tp = Py_TYPE(self);
+ tp->tp_free((PyObject *)self);
Py_DECREF(tp);
}
@@ -421,7 +420,7 @@ static PyType_Spec bz2_compressor_type_spec = {
// bz2_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_HAVE_GC),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
.slots = bz2_compressor_type_slots,
};
@@ -688,11 +687,9 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
static void
BZ2Decompressor_dealloc(PyObject *op)
{
- PyTypeObject *tp = Py_TYPE(op);
- PyObject_GC_UnTrack(op);
BZ2Decompressor *self = _BZ2Decompressor_CAST(op);
- if (self->input_buffer != NULL) {
+ if(self->input_buffer != NULL) {
PyMem_Free(self->input_buffer);
}
BZ2_bzDecompressEnd(&self->bzs);
@@ -700,7 +697,9 @@ BZ2Decompressor_dealloc(PyObject *op)
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}
- tp->tp_free(self);
+
+ PyTypeObject *tp = Py_TYPE(self);
+ tp->tp_free((PyObject *)self);
Py_DECREF(tp);
}
@@ -752,7 +751,7 @@ static PyType_Spec bz2_decompressor_type_spec = {
// bz2_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_HAVE_GC),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
.slots = bz2_decompressor_type_slots,
};
diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c
index 9d79fc08dcfcac..a6496d0f04f2d0 100644
--- a/Modules/_hashopenssl.c
+++ b/Modules/_hashopenssl.c
@@ -752,9 +752,7 @@ py_wrapper_EVP_MD_CTX_new(void)
static HASHobject *
new_hash_object(PyTypeObject *type)
{
- assert(type != NULL);
- assert(type->tp_alloc != NULL);
- HASHobject *retval = (HASHobject *)type->tp_alloc(type, 0);
+ HASHobject *retval = PyObject_New(HASHobject, type);
if (retval == NULL) {
return NULL;
}
@@ -794,21 +792,13 @@ _hashlib_HASH_hash(HASHobject *self, const void *vp,
Py_ssize_t len)
static void
_hashlib_HASH_dealloc(PyObject *op)
{
- PyTypeObject *tp = Py_TYPE(op);
- PyObject_GC_UnTrack(op);
HASHobject *self = HASHobject_CAST(op);
+ PyTypeObject *tp = Py_TYPE(self);
EVP_MD_CTX_free(self->ctx);
- tp->tp_free(self);
+ PyObject_Free(self);
Py_DECREF(tp);
}
-static int
-_hashlib_HASH_traverse(PyObject *op, visitproc visit, void *arg)
-{
- Py_VISIT(Py_TYPE(op));
- return 0;
-}
-
static int
_hashlib_HASH_copy_locked(HASHobject *self, EVP_MD_CTX *new_ctx_p)
{
@@ -1003,7 +993,6 @@ PyDoc_STRVAR(HASHobject_type_doc,
static PyType_Slot HASHobject_type_slots[] = {
{Py_tp_dealloc, _hashlib_HASH_dealloc},
- {Py_tp_traverse, _hashlib_HASH_traverse},
{Py_tp_repr, _hashlib_HASH_repr},
{Py_tp_doc, (char *)HASHobject_type_doc},
{Py_tp_methods, HASH_methods},
@@ -1019,7 +1008,6 @@ static PyType_Spec HASHobject_type_spec = {
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_DISALLOW_INSTANTIATION
| Py_TPFLAGS_IMMUTABLETYPE
- | Py_TPFLAGS_HAVE_GC
),
.slots = HASHobject_type_slots
};
@@ -1177,8 +1165,6 @@ PyDoc_STRVAR(HASHXOFobject_type_doc,
"digest_size -- number of bytes in this hashes output");
static PyType_Slot HASHXOFobject_type_slots[] = {
- {Py_tp_dealloc, _hashlib_HASH_dealloc},
- {Py_tp_traverse, _hashlib_HASH_traverse},
{Py_tp_doc, (char *)HASHXOFobject_type_doc},
{Py_tp_methods, HASHXOFobject_methods},
{Py_tp_getset, HASHXOFobject_getsets},
@@ -1193,7 +1179,6 @@ static PyType_Spec HASHXOFobject_type_spec = {
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_DISALLOW_INSTANTIATION
| Py_TPFLAGS_IMMUTABLETYPE
- | Py_TPFLAGS_HAVE_GC
),
.slots = HASHXOFobject_type_slots
};
@@ -1917,8 +1902,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key,
PyObject *msg_obj,
goto error;
}
- assert(state->HMAC_type != NULL);
- self = (HMACobject *)state->HMAC_type->tp_alloc(state->HMAC_type, 0);
+ self = PyObject_New(HMACobject, state->HMAC_type);
if (self == NULL) {
goto error;
}
@@ -2024,8 +2008,7 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
return NULL;
}
- PyTypeObject *type = Py_TYPE(self);
- retval = (HMACobject *)type->tp_alloc(type, 0);
+ retval = PyObject_New(HMACobject, Py_TYPE(self));
if (retval == NULL) {
HMAC_CTX_free(ctx);
return NULL;
@@ -2039,24 +2022,16 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
static void
_hmac_dealloc(PyObject *op)
{
- PyTypeObject *tp = Py_TYPE(op);
- PyObject_GC_UnTrack(op);
HMACobject *self = HMACobject_CAST(op);
+ PyTypeObject *tp = Py_TYPE(self);
if (self->ctx != NULL) {
HMAC_CTX_free(self->ctx);
self->ctx = NULL;
}
- tp->tp_free(self);
+ PyObject_Free(self);
Py_DECREF(tp);
}
-static int
-_hashlib_HMAC_traverse(PyObject *op, visitproc visit, void *arg)
-{
- Py_VISIT(Py_TYPE(op));
- return 0;
-}
-
static PyObject *
_hmac_repr(PyObject *op)
{
@@ -2223,21 +2198,15 @@ static PyType_Slot HMACtype_slots[] = {
{Py_tp_doc, (char *)hmactype_doc},
{Py_tp_repr, _hmac_repr},
{Py_tp_dealloc, _hmac_dealloc},
- {Py_tp_traverse, _hashlib_HMAC_traverse},
{Py_tp_methods, HMAC_methods},
{Py_tp_getset, HMAC_getset},
{0, NULL}
};
PyType_Spec HMACtype_spec = {
- .name = "_hashlib.HMAC",
- .basicsize = sizeof(HMACobject),
- .flags = (
- Py_TPFLAGS_DEFAULT
- | Py_TPFLAGS_DISALLOW_INSTANTIATION
- | Py_TPFLAGS_IMMUTABLETYPE
- | Py_TPFLAGS_HAVE_GC
- ),
+ "_hashlib.HMAC", /* name */
+ sizeof(HMACobject), /* basicsize */
+ .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
Py_TPFLAGS_IMMUTABLETYPE,
.slots = HMACtype_slots,
};
diff --git a/Modules/_lzmamodule.c b/Modules/_lzmamodule.c
index bcc9ea7a7bba68..0b0b1bc765bbc9 100644
--- a/Modules/_lzmamodule.c
+++ b/Modules/_lzmamodule.c
@@ -866,13 +866,12 @@ Compressor_new(PyTypeObject *type, PyObject *args,
PyObject *kwargs)
static void
Compressor_dealloc(PyObject *op)
{
- PyTypeObject *tp = Py_TYPE(op);
- PyObject_GC_UnTrack(op);
Compressor *self = Compressor_CAST(op);
lzma_end(&self->lzs);
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}
+ PyTypeObject *tp = Py_TYPE(self);
tp->tp_free(self);
Py_DECREF(tp);
}
@@ -934,7 +933,7 @@ static PyType_Spec lzma_compressor_type_spec = {
// lzma_compressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_HAVE_GC),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
.slots = lzma_compressor_type_slots,
};
@@ -1315,8 +1314,6 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int
format,
static void
Decompressor_dealloc(PyObject *op)
{
- PyTypeObject *tp = Py_TYPE(op);
- PyObject_GC_UnTrack(op);
Decompressor *self = Decompressor_CAST(op);
if(self->input_buffer != NULL)
PyMem_Free(self->input_buffer);
@@ -1326,6 +1323,7 @@ Decompressor_dealloc(PyObject *op)
if (self->lock != NULL) {
PyThread_free_lock(self->lock);
}
+ PyTypeObject *tp = Py_TYPE(self);
tp->tp_free(self);
Py_DECREF(tp);
}
@@ -1383,7 +1381,7 @@ static PyType_Spec lzma_decompressor_type_spec = {
// lzma_decompressor_type_spec does not have Py_TPFLAGS_BASETYPE flag
// which prevents to create a subclass.
// So calling PyType_GetModuleState() in this file is always safe.
- .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE |
Py_TPFLAGS_HAVE_GC),
+ .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE),
.slots = lzma_decompressor_type_slots,
};
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]