8000 bpo-1635741: Port hashlib modules to multiphase init (PEP 489) by koubaa · Pull Request #21818 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-1635741: Port hashlib modules to multiphase init (PEP 489) #21818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Sep 6, 2020
Next Next commit
port sha1
  • Loading branch information
koubaa committed Aug 31, 2020
commit 4ae1a3f9d4badfb33892e0438c4dd475bcea252d
125 changes: 63 additions & 62 deletions Modules/sha1module.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,20 @@ sha1_done(struct sha1_state *sha1, unsigned char *out)
* ------------------------------------------------------------------------
*/

static PyTypeObject SHA1type;
typedef struct {
PyTypeObject* sha1_type;
} SHA1State;

static inline SHA1State* sha1_get_state(PyObject *module) {
void *state = PyModule_GetState(module);
assert(state != NULL);
return (SHA1State *)state;
}

static SHA1object *
newSHA1object(void)
newSHA1object(SHA1State *st)
{
return (SHA1object *)PyObject_New(SHA1object, &SHA1type);
return (SHA1object *)PyObject_New(SHA1object, st->sha1_type);
}


Expand All @@ -326,9 +333,10 @@ static PyObject *
SHA1Type_copy_impl(SHA1object *self)
/*[clinic end generated code: output=b4e001264620f02a input=b7eae10df6f89b36]*/
{
SHA1object *newobj;
SHA1State *st = PyType_GetModuleState(Py_TYPE(self));

if ((newobj = newSHA1object()) == NULL)
SHA1object *newobj;
if ((newobj = newSHA1object(st)) == NULL)
return NULL;

newobj->hash_state = self->hash_state;
Expand Down Expand Up @@ -422,7 +430,6 @@ sha1_get_digest_size(PyObject *self, void *closure)
return PyLong_FromLong(SHA1_DIGESTSIZE);
}


static PyGetSetDef SHA1_getseters[] = {
{"block_size",
(getter)SHA1_get_block_size, NULL,
Expand All @@ -439,40 +446,19 @@ static PyGetSetDef SHA1_getseters[] = {
{NULL} /* Sentinel */
};

static PyTypeObject SHA1type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_sha1.sha1", /*tp_name*/
sizeof(SHA1object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
SHA1_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*/
SHA1_methods, /* tp_methods */
NULL, /* tp_members */
SHA1_getseters, /* tp_getset */
static PyType_Slot sha1_type_slots[] = {
{Py_tp_dealloc, SHA1_dealloc},
{Py_tp_methods, SHA1_methods},
{Py_tp_getset, SHA1_getseters},
{0,0}
};

static PyType_Spec sha1_type_spec = {
.name = "_sha1.sha1",
.basicsize = sizeof(SHA1object),
.flags = Py_TPFLAGS_DEFAULT,
.slots = sha1_type_slots
};

/* The single module-level function: new() */

Expand All @@ -496,7 +482,8 @@ _sha1_sha1_impl(PyObject *module, PyObject *string, int usedforsecurity)
if (string)
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);

if ((new = newSHA1object()) == NULL) {
SHA1State *st = sha1_get_state(module);
if ((new = newSHA1object(st)) == NULL) {
if (string)
PyBuffer_Release(&buf);
return NULL;
Expand Down Expand Up @@ -526,37 +513,51 @@ static struct PyMethodDef SHA1_functions[] = {
{NULL, NULL} /* Sentinel */
};

static int sha1_exec(PyObject *module) {
SHA1State* st = sha1_get_state(module);

st->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec(
module, &sha1_type_spec, NULL);

if (st->sha1_type == NULL) {
return -1;
}

//cannot use PyModule_AddType becuase "SHA1Type"
//isn't the same as _PyType_Name(st->sha1_type)
if (PyType_Ready(st->sha1_type) < 0) {
return -1;
}

Py_INCREF(st->sha1_type);
if (PyModule_AddObject(module,
"SHA1Type",
(PyObject *)st->sha1_type) < 0) {
Py_DECREF(st->sha1_type);
return -1;
}

return 0;
}


/* Initialize this module. */

static PyModuleDef_Slot _sha1_slots[] = {
{Py_mod_exec, sha1_exec},
{0, NULL}
};

static struct PyModuleDef _sha1module = {
PyModuleDef_HEAD_INIT,
"_sha1",
NULL,
-1,
SHA1_functions,
NULL,
NULL,
NULL,
NULL
.m_name = "_sha1",
.m_size = sizeof(SHA1State),
.m_methods = SHA1_functions,
.m_slots = _sha1_slots
};

PyMODINIT_FUNC
PyInit__sha1(void)
{
PyObject *m;

Py_SET_TYPE(&SHA1type, &PyType_Type);
if (PyType_Ready(&SHA1type) < 0) {
return NULL;
}

m = PyModule_Create(&_sha1module);
if (m == NULL) {
return NULL;
}

Py_INCREF((PyObject *)&SHA1type);
PyModule_AddObject(m, "SHA1Type", (PyObject *)&SHA1type);
return m;
return PyModuleDef_Init(&_sha1module);
}
0