@@ -139,6 +139,7 @@ typedef struct py_hmac_hinfo {
139139
140140typedef struct hmacmodule_state {
141141 _Py_hashtable_t * hinfo_table ;
142+ PyObject * unknown_hash_error ;
142143} hmacmodule_state ;
143144
144145static inline hmacmodule_state *
@@ -333,13 +334,34 @@ hmacmodule_init_hash_info_table(hmacmodule_state *state)
333334 return 0 ;
334335}
335336
337+ static int
338+ hmacmodule_init_exceptions (PyObject * module , hmacmodule_state * state )
339+ {
340+ #define ADD_EXC (ATTR , NAME , BASE ) \
341+ do { \
342+ state->ATTR = PyErr_NewException("_hmac." NAME, BASE, NULL); \
343+ if (state->ATTR == NULL) { \
344+ return -1; \
345+ } \
346+ if (PyModule_AddObjectRef(module, NAME, state->ATTR) < 0) { \
347+ return -1; \
348+ } \
349+ } while (0)
350+ ADD_EXC (unknown_hash_error , "UnknownHashError" , PyExc_ValueError );
351+ #undef ADD_EXC
352+ return 0 ;
353+ }
354+
336355static int
337356hmacmodule_exec (PyObject * module )
338357{
339358 hmacmodule_state * state = get_hmacmodule_state (module );
340359 if (hmacmodule_init_hash_info_table (state ) < 0 ) {
341360 return -1 ;
342361 }
362+ if (hmacmodule_init_exceptions (module , state ) < 0 ) {
363+ return -1 ;
364+ }
343365 return 0 ;
344366}
345367
@@ -348,6 +370,7 @@ hmacmodule_traverse(PyObject *mod, visitproc visit, void *arg)
348370{
349371 Py_VISIT (Py_TYPE (mod ));
350372 hmacmodule_state * state = get_hmacmodule_state (mod );
373+ Py_VISIT (state -> unknown_hash_error );
351374 return 0 ;
352375}
353376
@@ -359,6 +382,7 @@ hmacmodule_clear(PyObject *mod)
359382 _Py_hashtable_destroy (state -> hinfo_table );
360383 state -> hinfo_table = NULL ;
361384 }
385+ Py_CLEAR (state -> unknown_hash_error );
362386 return 0 ;
363387}
364388
0 commit comments