FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[3.13] gh-116946: Revert GC protocol for immutable empty heap types (GH-138322, GH-138323, GH-138326) by picnixz · Pull Request #138337 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 11 additions & 17 deletions Modules/_bz2module.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ typedef struct {
PyThread_type_lock lock;
} BZ2Decompressor;

#define _BZ2Compressor_CAST(op) ((BZ2Compressor *)(op))
#define _BZ2Decompressor_CAST(op) ((BZ2Decompressor *)(op))

/* Helper functions. */

static int
Expand Down Expand Up @@ -379,21 +376,19 @@ _bz2_BZ2Compressor_impl(PyTypeObject *type, int compresslevel)
}

static void
BZ2Compressor_dealloc(PyObject *op)
BZ2Compressor_dealloc(BZ2Compressor *self)
{
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);
}

static int
BZ2Compressor_traverse(PyObject *self, visitproc visit, void *arg)
BZ2Compressor_traverse(BZ2Compressor *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
Expand Down Expand Up @@ -421,7 +416,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,
};

Expand Down Expand Up @@ -685,11 +680,8 @@ _bz2_BZ2Decompressor_impl(PyTypeObject *type)
}

static void
BZ2Decompressor_dealloc(PyObject *op)
BZ2Decompressor_dealloc(BZ2Decompressor *self)
{
PyTypeObject *tp = Py_TYPE(op);
PyObject_GC_UnTrack(op);
BZ2Decompressor *self = _BZ2Decompressor_CAST(op);
if(self->input_buffer != NULL) {
PyMem_Free(self->input_buffer);
}
Expand All @@ -698,12 +690,14 @@ 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);
}

static int
BZ2Decompressor_traverse(PyObject *self, visitproc visit, void *arg)
BZ2Decompressor_traverse(BZ2Decompressor *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
Expand Down Expand Up @@ -750,7 +744,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,
};

Expand Down
91 changes: 23 additions & 68 deletions Modules/_hashopenssl.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ typedef struct {
PyMutex mutex; /* OpenSSL context lock */
} EVPobject;

#define EVPobject_CAST(op) ((EVPobject *)(op))

typedef struct {
PyObject_HEAD
HMAC_CTX *ctx; /* OpenSSL hmac context */
Expand All @@ -292,8 +290,6 @@ typedef struct {
PyMutex mutex; /* HMAC context lock */
} HMACobject;

#define HMACobject_CAST(op) ((HMACobject *)(op))

#include "clinic/_hashopenssl.c.h"
/*[clinic input]
module _hashlib
Expand Down Expand Up @@ -501,9 +497,7 @@ py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type
static EVPobject *
newEVPobject(PyTypeObject *type)
{
assert(type != NULL);
assert(type->tp_alloc != NULL);
EVPobject *retval = (EVPobject *)type->tp_alloc(type, 0);
EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, type);
if (retval == NULL) {
return NULL;
}
Expand Down Expand Up @@ -542,23 +536,14 @@ EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
/* Internal methods for a hash object */

static void
EVP_dealloc(PyObject *op)
EVP_dealloc(EVPobject *self)
{
PyTypeObject *tp = Py_TYPE(op);
PyObject_GC_UnTrack(op);
EVPobject *self = EVPobject_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
EVP_traverse(PyObject *op, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(op));
return 0;
}

static int
locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
{
Expand Down Expand Up @@ -796,7 +781,6 @@ PyDoc_STRVAR(hashtype_doc,

static PyType_Slot EVPtype_slots[] = {
{Py_tp_dealloc, EVP_dealloc},
{Py_tp_traverse, EVP_traverse},
{Py_tp_repr, EVP_repr},
{Py_tp_doc, (char *)hashtype_doc},
{Py_tp_methods, EVP_methods},
Expand All @@ -805,16 +789,11 @@ static PyType_Slot EVPtype_slots[] = {
};

static PyType_Spec EVPtype_spec = {
.name = "_hashlib.HASH",
.basicsize = sizeof(EVPobject),
.flags = (
Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_DISALLOW_INSTANTIATION
| Py_TPFLAGS_IMMUTABLETYPE
| Py_TPFLAGS_HAVE_GC
),
.slots = EVPtype_slots
"_hashlib.HASH", /*tp_name*/
sizeof(EVPobject), /*tp_basicsize*/
0, /*tp_itemsize*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
EVPtype_slots
};

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

static PyType_Slot EVPXOFtype_slots[] = {
{Py_tp_dealloc, EVP_dealloc},
{Py_tp_traverse, EVP_traverse},
{Py_tp_doc, (char *)hashxoftype_doc},
{Py_tp_methods, EVPXOF_methods},
{Py_tp_getset, EVPXOF_getseters},
{0, 0},
};

static PyType_Spec EVPXOFtype_spec = {
.name = "_hashlib.HASHXOF",
.basicsize = sizeof(EVPobject),
.flags = (
Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_DISALLOW_INSTANTIATION
| Py_TPFLAGS_IMMUTABLETYPE
| Py_TPFLAGS_HAVE_GC
),
.slots = EVPXOFtype_slots
"_hashlib.HASHXOF", /*tp_name*/
sizeof(EVPobject), /*tp_basicsize*/
0, /*tp_itemsize*/
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
EVPXOFtype_slots
};


Expand Down Expand Up @@ -1687,8 +1659,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
}

_hashlibstate *state = get_hashlib_state(module);
assert(state->HMACtype != NULL);
self = (HMACobject *)state->HMACtype->tp_alloc(state->HMACtype, 0);
self = PyObject_New(HMACobject, state->HMACtype);
if (self == NULL) {
goto error;
}
Expand Down Expand Up @@ -1793,8 +1764,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;
Expand All @@ -1806,26 +1776,17 @@ _hashlib_HMAC_copy_impl(HMACobject *self)
}

static void
_hmac_dealloc(PyObject *op)
_hmac_dealloc(HMACobject *self)
{
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
_hmac_traverse(PyObject *op, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(op));
return 0;
}

static PyObject *
_hmac_repr(HMACobject *self)
{
Expand Down Expand Up @@ -1993,22 +1954,16 @@ digest_size -- number of bytes in digest() output\n");
static PyType_Slot HMACtype_slots[] = {
{Py_tp_doc, (char *)hmactype_doc},
{Py_tp_repr, (reprfunc)_hmac_repr},
{Py_tp_dealloc, _hmac_dealloc},
{Py_tp_traverse, _hmac_traverse},
{Py_tp_dealloc,(destructor)_hmac_dealloc},
{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,
};

Expand Down
27 changes: 10 additions & 17 deletions Modules/_lzmamodule.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,6 @@ typedef struct {
PyThread_type_lock lock;
} Decompressor;

#define Compressor_CAST(op) ((Compressor *)(op))
#define Decompressor_CAST(op) ((Decompressor *)(op))

/* Helper functions. */

static int
Expand Down Expand Up @@ -860,16 +857,14 @@ Compressor_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
}

static void
Compressor_dealloc(PyObject *op)
Compressor_dealloc(Compressor *self)
{
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);
}
tp->tp_free(self);
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject *)self);
Py_DECREF(tp);
}

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

static int
Compressor_traverse(PyObject *self, visitproc visit, void *arg)
Compressor_traverse(Compressor *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
Expand Down Expand Up @@ -930,7 +925,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,
};

Expand Down Expand Up @@ -1309,11 +1304,8 @@ _lzma_LZMADecompressor_impl(PyTypeObject *type, int format,
}

static void
Decompressor_dealloc(PyObject *op)
Decompressor_dealloc(Decompressor *self)
{
PyTypeObject *tp = Py_TYPE(op);
PyObject_GC_UnTrack(op);
Decompressor *self = Decompressor_CAST(op);
if(self->input_buffer != NULL)
PyMem_Free(self->input_buffer);

Expand All @@ -1322,12 +1314,13 @@ Decompressor_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);
}

static int
Decompressor_traverse(PyObject *self, visitproc visit, void *arg)
Decompressor_traverse(Decompressor *self, visitproc visit, void *arg)
{
Py_VISIT(Py_TYPE(self));
return 0;
Expand Down Expand Up @@ -1379,7 +1372,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,
};

Expand Down

Back | FazBrowse Home | New Git URL