bpo-1635741: Convert _sha256 types to heap types (GH-22134)
Convert the _sha256 extension module types to heap types.
This commit is contained in:
parent
15dcdb2113
commit
52a2df135c
|
@ -0,0 +1 @@
|
||||||
|
Convert the :mod:`_sha256` extension module types to heap types.
|
|
@ -9,15 +9,26 @@ PyDoc_STRVAR(SHA256Type_copy__doc__,
|
||||||
"Return a copy of the hash object.");
|
"Return a copy of the hash object.");
|
||||||
|
|
||||||
#define SHA256TYPE_COPY_METHODDEF \
|
#define SHA256TYPE_COPY_METHODDEF \
|
||||||
{"copy", (PyCFunction)SHA256Type_copy, METH_NOARGS, SHA256Type_copy__doc__},
|
{"copy", (PyCFunction)(void(*)(void))SHA256Type_copy, METH_METHOD|METH_FASTCALL|METH_KEYWORDS, SHA256Type_copy__doc__},
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
SHA256Type_copy_impl(SHAobject *self);
|
SHA256Type_copy_impl(SHAobject *self, PyTypeObject *cls);
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
SHA256Type_copy(SHAobject *self, PyObject *Py_UNUSED(ignored))
|
SHA256Type_copy(SHAobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
|
||||||
{
|
{
|
||||||
return SHA256Type_copy_impl(self);
|
PyObject *return_value = NULL;
|
||||||
|
static const char * const _keywords[] = { NULL};
|
||||||
|
static _PyArg_Parser _parser = {":copy", _keywords, 0};
|
||||||
|
|
||||||
|
if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser
|
||||||
|
)) {
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
return_value = SHA256Type_copy_impl(self, cls);
|
||||||
|
|
||||||
|
exit:
|
||||||
|
return return_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
PyDoc_STRVAR(SHA256Type_digest__doc__,
|
PyDoc_STRVAR(SHA256Type_digest__doc__,
|
||||||
|
@ -166,4 +177,4 @@ skip_optional_kwonly:
|
||||||
exit:
|
exit:
|
||||||
return return_value;
|
return return_value;
|
||||||
}
|
}
|
||||||
/*[clinic end generated code: output=c8cca8adbe72ec9a input=a9049054013a1b77]*/
|
/*[clinic end generated code: output=b7283f75c9d08f30 input=a9049054013a1b77]*/
|
||||||
|
|
|
@ -51,6 +51,19 @@ typedef struct {
|
||||||
|
|
||||||
#include "clinic/sha256module.c.h"
|
#include "clinic/sha256module.c.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
PyTypeObject* sha224_type;
|
||||||
|
PyTypeObject* sha256_type;
|
||||||
|
} _sha256_state;
|
||||||
|
|
||||||
|
static inline _sha256_state*
|
||||||
|
_sha256_get_state(PyObject *module)
|
||||||
|
{
|
||||||
|
void *state = PyModule_GetState(module);
|
||||||
|
assert(state != NULL);
|
||||||
|
return (_sha256_state *)state;
|
||||||
|
}
|
||||||
|
|
||||||
/* When run on a little-endian CPU we need to perform byte reversal on an
|
/* When run on a little-endian CPU we need to perform byte reversal on an
|
||||||
array of longwords. */
|
array of longwords. */
|
||||||
|
|
||||||
|
@ -365,20 +378,17 @@ sha_final(unsigned char digest[SHA_DIGESTSIZE], SHAobject *sha_info)
|
||||||
* ------------------------------------------------------------------------
|
* ------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static PyTypeObject SHA224type;
|
|
||||||
static PyTypeObject SHA256type;
|
|
||||||
|
|
||||||
|
|
||||||
static SHAobject *
|
static SHAobject *
|
||||||
newSHA224object(void)
|
newSHA224object(_sha256_state *state)
|
||||||
{
|
{
|
||||||
return (SHAobject *)PyObject_New(SHAobject, &SHA224type);
|
return (SHAobject *)PyObject_New(SHAobject, state->sha224_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
static SHAobject *
|
static SHAobject *
|
||||||
newSHA256object(void)
|
newSHA256object(_sha256_state *state)
|
||||||
{
|
{
|
||||||
return (SHAobject *)PyObject_New(SHAobject, &SHA256type);
|
return (SHAobject *)PyObject_New(SHAobject, state->sha256_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Internal methods for a hash object */
|
/* Internal methods for a hash object */
|
||||||
|
@ -386,7 +396,9 @@ newSHA256object(void)
|
||||||
static void
|
static void
|
||||||
SHA_dealloc(PyObject *ptr)
|
SHA_dealloc(PyObject *ptr)
|
||||||
{
|
{
|
||||||
|
PyTypeObject *tp = Py_TYPE(ptr);
|
||||||
PyObject_Del(ptr);
|
PyObject_Del(ptr);
|
||||||
|
Py_DECREF(tp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -395,21 +407,25 @@ SHA_dealloc(PyObject *ptr)
|
||||||
/*[clinic input]
|
/*[clinic input]
|
||||||
SHA256Type.copy
|
SHA256Type.copy
|
||||||
|
|
||||||
|
cls:defining_class
|
||||||
|
|
||||||
Return a copy of the hash object.
|
Return a copy of the hash object.
|
||||||
[clinic start generated code]*/
|
[clinic start generated code]*/
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
SHA256Type_copy_impl(SHAobject *self)
|
SHA256Type_copy_impl(SHAobject *self, PyTypeObject *cls)
|
||||||
/*[clinic end generated code: output=1a8bbd66a0c9c168 input=f58840a618d4f2a7]*/
|
/*[clinic end generated code: output=9273f92c382be12f input=3137146fcb88e212]*/
|
||||||
{
|
{
|
||||||
SHAobject *newobj;
|
SHAobject *newobj;
|
||||||
|
_sha256_state *state = PyType_GetModuleState(cls);
|
||||||
if (Py_IS_TYPE(self, &SHA256type)) {
|
if (Py_IS_TYPE(self, state->sha256_type)) {
|
||||||
if ( (newobj = newSHA256object())==NULL)
|
if ( (newobj = newSHA256object(state)) == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if ( (newobj = newSHA224object())==NULL)
|
if ( (newobj = newSHA224object(state))==NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SHAcopy(self, newobj);
|
SHAcopy(self, newobj);
|
||||||
|
@ -517,74 +533,27 @@ static PyMemberDef SHA_members[] = {
|
||||||
{NULL} /* Sentinel */
|
{NULL} /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyTypeObject SHA224type = {
|
static PyType_Slot sha256_types_slots[] = {
|
||||||
PyVarObject_HEAD_INIT(NULL, 0)
|
{Py_tp_dealloc, SHA_dealloc},
|
||||||
"_sha256.sha224", /*tp_name*/
|
{Py_tp_methods, SHA_methods},
|
||||||
sizeof(SHAobject), /*tp_basicsize*/
|
{Py_tp_members, SHA_members},
|
||||||
0, /*tp_itemsize*/
|
{Py_tp_getset, SHA_getseters},
|
||||||
/* methods */
|
{0,0}
|
||||||
SHA_dealloc, /*tp_dealloc*/
|
|
||||||
0, /*tp_vectorcall_offset*/
|
|
||||||
0, /*tp_getattr*/
|
|
||||||
0, /*tp_setattr*/
|
|
||||||
0, /*tp_as_async*/
|
|
||||||
0, /*tp_repr*/
|
|
||||||
0, /*tp_as_number*/
|
|
||||||
0, /*tp_as_sequence*/
|
|
||||||
0, /*tp_as_mapping*/
|
|
||||||
0, /*tp_hash*/
|
|
||||||
0, /*tp_call*/
|
|
||||||
0, /*tp_str*/
|
|
||||||
0, /*tp_getattro*/
|
|
||||||
0, /*tp_setattro*/
|
|
||||||
0, /*tp_as_buffer*/
|
|
||||||
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
||||||
0, /*tp_doc*/
|
|
||||||
0, /*tp_traverse*/
|
|
||||||
0, /*tp_clear*/
|
|
||||||
0, /*tp_richcompare*/
|
|
||||||
0, /*tp_weaklistoffset*/
|
|
||||||
0, /*tp_iter*/
|
|
||||||
0, /*tp_iternext*/
|
|
||||||
SHA_methods, /* tp_methods */
|
|
||||||
SHA_members, /* tp_members */
|
|
||||||
SHA_getseters, /* tp_getset */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static PyTypeObject SHA256type = {
|
static PyType_Spec sha224_type_spec = {
|
||||||
PyVarObject_HEAD_INIT(NULL, 0)
|
.name = "_sha256.sha224",
|
||||||
"_sha256.sha256", /*tp_name*/
|
.basicsize = sizeof(SHAobject),
|
||||||
sizeof(SHAobject), /*tp_basicsize*/
|
.flags = Py_TPFLAGS_DEFAULT,
|
||||||
0, /*tp_itemsize*/
|
.slots = sha256_types_slots
|
||||||
/* methods */
|
|
||||||
SHA_dealloc, /*tp_dealloc*/
|
|
||||||
0, /*tp_vectorcall_offset*/
|
|
||||||
0, /*tp_getattr*/
|
|
||||||
0, /*tp_setattr*/
|
|
||||||
0, /*tp_as_async*/
|
|
||||||
0, /*tp_repr*/
|
|
||||||
0, /*tp_as_number*/
|
|
||||||
0, /*tp_as_sequence*/
|
|
||||||
0, /*tp_as_mapping*/
|
|
||||||
0, /*tp_hash*/
|
|
||||||
0, /*tp_call*/
|
|
||||||
0, /*tp_str*/
|
|
||||||
0, /*tp_getattro*/
|
|
||||||
0, /*tp_setattro*/
|
|
||||||
0, /*tp_as_buffer*/
|
|
||||||
Py_TPFLAGS_DEFAULT, /*tp_flags*/
|
|
||||||
0, /*tp_doc*/
|
|
||||||
0, /*tp_traverse*/
|
|
||||||
0, /*tp_clear*/
|
|
||||||
0, /*tp_richcompare*/
|
|
||||||
0, /*tp_weaklistoffset*/
|
|
||||||
0, /*tp_iter*/
|
|
||||||
0, /*tp_iternext*/
|
|
||||||
SHA_methods, /* tp_methods */
|
|
||||||
SHA_members, /* tp_members */
|
|
||||||
SHA_getseters, /* tp_getset */
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static PyType_Spec sha256_type_spec = {
|
||||||
|
.name = "_sha256.sha256",
|
||||||
|
.basicsize = sizeof(SHAobject),
|
||||||
|
.flags = Py_TPFLAGS_DEFAULT,
|
||||||
|
.slots = sha256_types_slots
|
||||||
|
};
|
||||||
|
|
||||||
/* The single module-level function: new() */
|
/* The single module-level function: new() */
|
||||||
|
|
||||||
|
@ -602,15 +571,19 @@ static PyObject *
|
||||||
_sha256_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
_sha256_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
||||||
/*[clinic end generated code: output=a1de327e8e1185cf input=9be86301aeb14ea5]*/
|
/*[clinic end generated code: output=a1de327e8e1185cf input=9be86301aeb14ea5]*/
|
||||||
{
|
{
|
||||||
SHAobject *new;
|
|
||||||
Py_buffer buf;
|
Py_buffer buf;
|
||||||
|
|
||||||
if (string)
|
if (string) {
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
|
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
|
||||||
|
}
|
||||||
|
|
||||||
if ((new = newSHA256object()) == NULL) {
|
_sha256_state *state = PyModule_GetState(module);
|
||||||
if (string)
|
|
||||||
|
SHAobject *new;
|
||||||
|
if ((new = newSHA256object(state)) == NULL) {
|
||||||
|
if (string) {
|
||||||
PyBuffer_Release(&buf);
|
PyBuffer_Release(&buf);
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -618,8 +591,9 @@ _sha256_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
if (string)
|
if (string) {
|
||||||
PyBuffer_Release(&buf);
|
PyBuffer_Release(&buf);
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (string) {
|
if (string) {
|
||||||
|
@ -644,15 +618,17 @@ static PyObject *
|
||||||
_sha256_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
_sha256_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
||||||
/*[clinic end generated code: output=08be6b36569bc69c input=9fcfb46e460860ac]*/
|
/*[clinic end generated code: output=08be6b36569bc69c input=9fcfb46e460860ac]*/
|
||||||
{
|
{
|
||||||
SHAobject *new;
|
|
||||||
Py_buffer buf;
|
Py_buffer buf;
|
||||||
|
if (string) {
|
||||||
if (string)
|
|
||||||
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
|
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
|
||||||
|
}
|
||||||
|
|
||||||
if ((new = newSHA224object()) == NULL) {
|
_sha256_state *state = PyModule_GetState(module);
|
||||||
if (string)
|
SHAobject *new;
|
||||||
|
if ((new = newSHA224object(state)) == NULL) {
|
||||||
|
if (string) {
|
||||||
PyBuffer_Release(&buf);
|
PyBuffer_Release(&buf);
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -660,8 +636,9 @@ _sha256_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
|
||||||
|
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_DECREF(new);
|
Py_DECREF(new);
|
||||||
if (string)
|
if (string) {
|
||||||
PyBuffer_Release(&buf);
|
PyBuffer_Release(&buf);
|
||||||
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (string) {
|
if (string) {
|
||||||
|
@ -681,25 +658,56 @@ static struct PyMethodDef SHA_functions[] = {
|
||||||
{NULL, NULL} /* Sentinel */
|
{NULL, NULL} /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static int
|
||||||
|
_sha256_traverse(PyObject *module, visitproc visit, void *arg)
|
||||||
|
{
|
||||||
|
_sha256_state *state = _sha256_get_state(module);
|
||||||
|
Py_VISIT(state->sha224_type);
|
||||||
|
Py_VISIT(state->sha256_type);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_sha256_clear(PyObject *module)
|
||||||
|
{
|
||||||
|
_sha256_state *state = _sha256_get_state(module);
|
||||||
|
Py_CLEAR(state->sha224_type);
|
||||||
|
Py_CLEAR(state->sha256_type);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
_sha256_free(void *module)
|
||||||
|
{
|
||||||
|
_sha256_clear((PyObject *)module);
|
||||||
|
}
|
||||||
|
|
||||||
static int sha256_exec(PyObject *module)
|
static int sha256_exec(PyObject *module)
|
||||||
{
|
{
|
||||||
Py_SET_TYPE(&SHA224type, &PyType_Type);
|
_sha256_state *state = _sha256_get_state(module);
|
||||||
if (PyType_Ready(&SHA224type) < 0) {
|
|
||||||
return -1;
|
state->sha224_type = (PyTypeObject *)PyType_FromModuleAndSpec(
|
||||||
}
|
module, &sha224_type_spec, NULL);
|
||||||
Py_SET_TYPE(&SHA256type, &PyType_Type);
|
|
||||||
if (PyType_Ready(&SHA256type) < 0) {
|
if (state->sha224_type == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Py_INCREF((PyObject *)&SHA224type);
|
state->sha256_type = (PyTypeObject *)PyType_FromModuleAndSpec(
|
||||||
if (PyModule_AddObject(module, "SHA224Type", (PyObject *)&SHA224type) < 0) {
|
module, &sha256_type_spec, NULL);
|
||||||
Py_DECREF((PyObject *)&SHA224type);
|
|
||||||
|
if (state->sha256_type == NULL) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
Py_INCREF((PyObject *)&SHA256type);
|
|
||||||
if (PyModule_AddObject(module, "SHA256Type", (PyObject *)&SHA256type) < 0) {
|
Py_INCREF((PyObject *)state->sha224_type);
|
||||||
Py_DECREF((PyObject *)&SHA256type);
|
if (PyModule_AddObject(module, "SHA224Type", (PyObject *)state->sha224_type) < 0) {
|
||||||
|
Py_DECREF((PyObject *)state->sha224_type);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
Py_INCREF((PyObject *)state->sha256_type);
|
||||||
|
if (PyModule_AddObject(module, "SHA256Type", (PyObject *)state->sha256_type) < 0) {
|
||||||
|
Py_DECREF((PyObject *)state->sha256_type);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -713,8 +721,12 @@ static PyModuleDef_Slot _sha256_slots[] = {
|
||||||
static struct PyModuleDef _sha256module = {
|
static struct PyModuleDef _sha256module = {
|
||||||
PyModuleDef_HEAD_INIT,
|
PyModuleDef_HEAD_INIT,
|
||||||
.m_name = "_sha256",
|
.m_name = "_sha256",
|
||||||
|
.m_size = sizeof(_sha256_state),
|
||||||
.m_methods = SHA_functions,
|
.m_methods = SHA_functions,
|
||||||
.m_slots = _sha256_slots,
|
.m_slots = _sha256_slots,
|
||||||
|
.m_traverse = _sha256_traverse,
|
||||||
|
.m_clear = _sha256_clear,
|
||||||
|
.m_free = _sha256_free
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Initialize this module. */
|
/* Initialize this module. */
|
||||||
|
|
Loading…
Reference in New Issue