From d3af172a7353464aa99c2530050ab002caa7aaa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 29 Jul 2025 11:49:05 +0200 Subject: [PATCH 1/3] set associated module to `_hashlib` heap types --- ...-07-29-11-46-36.gh-issue-137199.zO8rD6.rst | 3 + Modules/_hashopenssl.c | 66 +++++++++---------- 2 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst diff --git a/Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst b/Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst new file mode 100644 index 00000000000000..3be3f769dcced5 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst @@ -0,0 +1,3 @@ +Associate the :mod:`!_hashlib` module to the C heap types +:class:`!_hashlib.HASH`, :class:`!_hashlib.HASHXOF`, and +:class:`!_hashlib.HMAC`. Patch by Bénédikt Tran. diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 26412cb62430c9..0888b90cc4b3df 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -2480,7 +2480,25 @@ 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); + if (*out == NULL) { + return -1; + } + if (PyModule_AddType(module, *out) < 0) { + return -1; + } + return 0; +} + +// --- Py_mod_exec functions -------------------------------------------------- + static int hashlib_init_hashtable(PyObject *module) { @@ -2498,15 +2516,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 +2527,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 From 1821904fc525cd9c47cfa3364db0e085ea6df118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 29 Jul 2025 13:49:08 +0200 Subject: [PATCH 2/3] remove NEWS entry --- .../Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst diff --git a/Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst b/Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst deleted file mode 100644 index 3be3f769dcced5..00000000000000 --- a/Misc/NEWS.d/next/Library/2025-07-29-11-46-36.gh-issue-137199.zO8rD6.rst +++ /dev/null @@ -1,3 +0,0 @@ -Associate the :mod:`!_hashlib` module to the C heap types -:class:`!_hashlib.HASH`, :class:`!_hashlib.HASHXOF`, and -:class:`!_hashlib.HMAC`. Patch by Bénédikt Tran. From 49ceb599e7cd61848a5152d7f232fd62e04bcef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Tue, 29 Jul 2025 14:41:13 +0200 Subject: [PATCH 3/3] condense helper --- Modules/_hashopenssl.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 0888b90cc4b3df..a8f5c675f2b5d9 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -2488,13 +2488,7 @@ hashlib_make_type(PyTypeObject **out, { assert(out != NULL); *out = (PyTypeObject *)PyType_FromModuleAndSpec(module, specs, bases); - if (*out == NULL) { - return -1; - } - if (PyModule_AddType(module, *out) < 0) { - return -1; - } - return 0; + return *out == NULL ? -1 : PyModule_AddType(module, *out); } // --- Py_mod_exec functions -------------------------------------------------- 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