Skip to content

Commit 9d00697

Browse files
authored
bpo-1635741: Port sha256 module to multiphase init (PEP 489) (GH-21189)
1 parent 148f329 commit 9d00697

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Port :mod:`sha256` to multiphase initialization

Modules/sha256module.c

+30-28
Original file line numberDiff line numberDiff line change
@@ -681,43 +681,45 @@ static struct PyMethodDef SHA_functions[] = {
681681
{NULL, NULL} /* Sentinel */
682682
};
683683

684-
685-
/* Initialize this module. */
686-
687-
static struct PyModuleDef _sha256module = {
688-
PyModuleDef_HEAD_INIT,
689-
"_sha256",
690-
NULL,
691-
-1,
692-
SHA_functions,
693-
NULL,
694-
NULL,
695-
NULL,
696-
NULL
697-
};
698-
699-
PyMODINIT_FUNC
700-
PyInit__sha256(void)
684+
static int sha256_exec(PyObject *module)
701685
{
702-
PyObject *m;
703-
704686
Py_SET_TYPE(&SHA224type, &PyType_Type);
705687
if (PyType_Ready(&SHA224type) < 0) {
706-
return NULL;
688+
return -1;
707689
}
708690
Py_SET_TYPE(&SHA256type, &PyType_Type);
709691
if (PyType_Ready(&SHA256type) < 0) {
710-
return NULL;
692+
return -1;
711693
}
712694

713-
m = PyModule_Create(&_sha256module);
714-
if (m == NULL)
715-
return NULL;
716-
717695
Py_INCREF((PyObject *)&SHA224type);
718-
PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
696+
if (PyModule_AddObject(module, "SHA224Type", (PyObject *)&SHA224type) < 0) {
697+
Py_DECREF((PyObject *)&SHA224type);
698+
return -1;
699+
}
719700
Py_INCREF((PyObject *)&SHA256type);
720-
PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
721-
return m;
701+
if (PyModule_AddObject(module, "SHA256Type", (PyObject *)&SHA256type) < 0) {
702+
Py_DECREF((PyObject *)&SHA256type);
703+
return -1;
704+
}
705+
return 0;
706+
}
707+
708+
static PyModuleDef_Slot _sha256_slots[] = {
709+
{Py_mod_exec, sha256_exec},
710+
{0, NULL}
711+
};
712+
713+
static struct PyModuleDef _sha256module = {
714+
PyModuleDef_HEAD_INIT,
715+
.m_name = "_sha256",
716+
.m_methods = SHA_functions,
717+
.m_slots = _sha256_slots,
718+
};
722719

720+
/* Initialize this module. */
721+
PyMODINIT_FUNC
722+
PyInit__sha256(void)
723+
{
724+
return PyModuleDef_Init(&_sha256module);
723725
}

0 commit comments

Comments
 (0)