Skip to content

Commit d3af172

Browse files
committed
set associated module to _hashlib heap types
1 parent d7e12a3 commit d3af172

2 files changed

Lines changed: 36 additions & 33 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Associate the :mod:`!_hashlib` module to the C heap types
2+
:class:`!_hashlib.HASH`, :class:`!_hashlib.HASHXOF`, and
3+
:class:`!_hashlib.HMAC`. Patch by Bénédikt Tran.

Modules/_hashopenssl.c

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,25 @@ hashlib_free(void *m)
24802480
(void)hashlib_clear((PyObject *)m);
24812481
}
24822482

2483-
/* Py_mod_exec functions */
2483+
// --- Py_mod_exec helpers ----------------------------------------------------
2484+
2485+
static int
2486+
hashlib_make_type(PyTypeObject **out,
2487+
PyObject *module, PyType_Spec *specs, PyObject *bases)
2488+
{
2489+
assert(out != NULL);
2490+
*out = (PyTypeObject *)PyType_FromModuleAndSpec(module, specs, bases);
2491+
if (*out == NULL) {
2492+
return -1;
2493+
}
2494+
if (PyModule_AddType(module, *out) < 0) {
2495+
return -1;
2496+
}
2497+
return 0;
2498+
}
2499+
2500+
// --- Py_mod_exec functions --------------------------------------------------
2501+
24842502
static int
24852503
hashlib_init_hashtable(PyObject *module)
24862504
{
@@ -2498,53 +2516,35 @@ static int
24982516
hashlib_init_HASH_type(PyObject *module)
24992517
{
25002518
_hashlibstate *state = get_hashlib_state(module);
2501-
2502-
state->HASH_type = (PyTypeObject *)PyType_FromSpec(&HASHobject_type_spec);
2503-
if (state->HASH_type == NULL) {
2504-
return -1;
2505-
}
2506-
if (PyModule_AddType(module, state->HASH_type) < 0) {
2507-
return -1;
2508-
}
2509-
return 0;
2519+
return hashlib_make_type(
2520+
&state->HASH_type,
2521+
module, &HASHobject_type_spec, NULL
2522+
);
25102523
}
25112524

25122525
static int
25132526
hashlib_init_HASHXOF_type(PyObject *module)
25142527
{
25152528
#ifdef PY_OPENSSL_HAS_SHAKE
25162529
_hashlibstate *state = get_hashlib_state(module);
2517-
2518-
if (state->HASH_type == NULL) {
2519-
return -1;
2520-
}
2521-
2522-
state->HASHXOF_type = (PyTypeObject *)PyType_FromSpecWithBases(
2523-
&HASHXOFobject_type_spec, (PyObject *)state->HASH_type
2530+
assert(state->HASH_type != NULL);
2531+
return hashlib_make_type(
2532+
&state->HASHXOF_type,
2533+
module, &HASHXOFobject_type_spec, (PyObject *)state->HASH_type
25242534
);
2525-
if (state->HASHXOF_type == NULL) {
2526-
return -1;
2527-
}
2528-
if (PyModule_AddType(module, state->HASHXOF_type) < 0) {
2529-
return -1;
2530-
}
2531-
#endif
2535+
#else
25322536
return 0;
2537+
#endif
25332538
}
25342539

25352540
static int
25362541
hashlib_init_hmactype(PyObject *module)
25372542
{
25382543
_hashlibstate *state = get_hashlib_state(module);
2539-
2540-
state->HMAC_type = (PyTypeObject *)PyType_FromSpec(&HMACtype_spec);
2541-
if (state->HMAC_type == NULL) {
2542-
return -1;
2543-
}
2544-
if (PyModule_AddType(module, state->HMAC_type) < 0) {
2545-
return -1;
2546-
}
2547-
return 0;
2544+
return hashlib_make_type(
2545+
&state->HMAC_type,
2546+
module, &HMACtype_spec, NULL
2547+
);
25482548
}
25492549

25502550
static int

0 commit comments

Comments
 (0)