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
Prev Previous commit
Next Next commit
port md5
  • Loading branch information
koubaa committed Aug 31, 2020
commit 8add6b16a95c1a6afe4ad5239705eb8f1ad08ee4
124 changes: 61 additions & 63 deletions Modules/md5module.c
8000
Original file line number Diff line number Diff line change
Expand Up @@ -318,16 +318,22 @@ md5_done(struct md5_state *md5, unsigned char *out)
* ------------------------------------------------------------------------
*/

static PyTypeObject MD5type;
typedef struct {
PyTypeObject* md5_type;
} MD5State;

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

static MD5object *
newMD5object(void)
newMD5object(MD5State * st)
{
return (MD5object *)PyObject_New(MD5object, &MD5type);
return (MD5object *)PyObject_New(MD5object, st->md5_type);
}


/* Internal methods for a hash object */

static void
Expand All @@ -349,9 +355,10 @@ static PyObject *
MD5Type_copy_impl(MD5object *self)
/*[clinic end generated code: output=596eb36852f02071 input=2c09e6d2493f3079]*/
{
MD5object *newobj;
MD5State *st = PyType_GetModuleState(Py_TYPE(self));

if ((newobj = newMD5object())==NULL)
MD5object *newobj;
if ((newobj = newMD5object(st))==NULL)
return NULL;

newobj->hash_state = self->hash_state;
Expand Down Expand Up @@ -445,7 +452,6 @@ md5_get_digest_size(PyObject *self, void *closure)
return PyLong_FromLong(MD5_DIGESTSIZE);
}


static PyGetSetDef MD5_getseters[] = {
{"block_size",
(getter)MD5_get_block_size, NULL,
Expand All @@ -462,40 +468,19 @@ static PyGetSetDef MD5_getseters[] = {
{NULL} /* Sentinel */
};

static PyTypeObject MD5type = {
PyVarObject_HEAD_INIT(NULL, 0)
"_md5.md5", /*tp_name*/
sizeof(MD5object), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
MD5_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*/
MD5_methods, /* tp_methods */
NULL, /* tp_members */
MD5_getseters, /* tp_getset */
static PyType_Slot md5_type_slots[] = {
{Py_tp_dealloc, MD5_dealloc},
{Py_tp_methods, MD5_methods},
{Py_tp_getset, MD5_getseters},
{0,0}
};

static PyType_Spec md5_type_spec = {
.name = "_md5.md5",
.basicsize = sizeof(MD5object),
.flags = Py_TPFLAGS_DEFAULT,
.slots = md5_type_slots
};

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

Expand All @@ -519,7 +504,8 @@ _md5_md5_impl(PyObject *module, PyObject *string, int usedforsecurity)
if (string)
GET_BUFFER_VIEW_OR_ERROUT(string, &buf);

if ((new = newMD5object()) == NULL) {
MD5State *st = md5_get_state(module);
if ((new = newMD5object(st)) == NULL) {
if (string)
PyBuffer_Release(&buf);
return NULL;
Expand Down Expand Up @@ -551,35 +537,47 @@ static struct PyMethodDef MD5_functions[] = {


/* Initialize this module. */
static int md5_exec(PyObject *m) {
MD5State *st = md5_get_state(m);

st->md5_type = (PyTypeObject *)PyType_FromModuleAndSpec(
m, &md5_type_spec, NULL);

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

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

Py_INCREF((PyObject *)st->md5_type);
if (PyModule_AddObject(m, "MD5Type", (PyObject *)st->md5_type) < 0) {
Py_DECREF(st->md5_type);
return -1;
}

return 0;
}

static PyModuleDef_Slot _md5_slots[] = {
{Py_mod_exec, md5_exec},
{0, NULL}
};


static struct PyModuleDef _md5module = {
PyModuleDef_HEAD_INIT,
"_md5",
NULL,
-1,
MD5_functions,
NULL,
NULL,
NULL,
NULL
.m_name = "_md5",
.m_size = sizeof(MD5State),
.m_methods = MD5_functions,
.m_slots = _md5_slots
};

PyMODINIT_FUNC
PyInit__md5(void)
{
PyObject *m;

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

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

Py_INCREF((PyObject *)&MD5type);
PyModule_AddObject(m, "MD5Type", (PyObject *)&MD5type);
return m;
return PyModuleDef_Init(&_md5module);
}
0