diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 26412cb62430c9..a8f5c675f2b5d9 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -2480,7 +2480,19 @@ hashlib_free(void *m) (void)hashlib_clear((PyObject *)m); } -/* Py_mod_exec functions */ +// --- Py_mod_exec helpers ---------------------------------------------------- + +static int +hashlib_make_type(PyTypeObject **out, + PyObject *module, PyType_Spec *specs, PyObject *bases) +{ + assert(out != NULL); + *out = (PyTypeObject *)PyType_FromModuleAndSpec(module, specs, bases); + return *out == NULL ? -1 : PyModule_AddType(module, *out); +} + +// --- Py_mod_exec functions -------------------------------------------------- + static int hashlib_init_hashtable(PyObject *module) { @@ -2498,15 +2510,10 @@ static int hashlib_init_HASH_type(PyObject *module) { _hashlibstate *state = get_hashlib_state(module); - - state->HASH_type = (PyTypeObject *)PyType_FromSpec(&HASHobject_type_spec); - if (state->HASH_type == NULL) { - return -1; - } - if (PyModule_AddType(module, state->HASH_type) < 0) { - return -1; - } - return 0; + return hashlib_make_type( + &state->HASH_type, + module, &HASHobject_type_spec, NULL + ); } static int @@ -2514,37 +2521,24 @@ hashlib_init_HASHXOF_type(PyObject *module) { #ifdef PY_OPENSSL_HAS_SHAKE _hashlibstate *state = get_hashlib_state(module); - - if (state->HASH_type == NULL) { - return -1; - } - - state->HASHXOF_type = (PyTypeObject *)PyType_FromSpecWithBases( - &HASHXOFobject_type_spec, (PyObject *)state->HASH_type + assert(state->HASH_type != NULL); + return hashlib_make_type( + &state->HASHXOF_type, + module, &HASHXOFobject_type_spec, (PyObject *)state->HASH_type ); - if (state->HASHXOF_type == NULL) { - return -1; - } - if (PyModule_AddType(module, state->HASHXOF_type) < 0) { - return -1; - } -#endif +#else return 0; +#endif } static int hashlib_init_hmactype(PyObject *module) { _hashlibstate *state = get_hashlib_state(module); - - state->HMAC_type = (PyTypeObject *)PyType_FromSpec(&HMACtype_spec); - if (state->HMAC_type == NULL) { - return -1; - } - if (PyModule_AddType(module, state->HMAC_type) < 0) { - return -1; - } - return 0; + return hashlib_make_type( + &state->HMAC_type, + module, &HMACtype_spec, NULL + ); } static int pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy