@@ -138,6 +138,7 @@ typedef struct py_hmac_hinfo {
138138
139139typedef struct hmacmodule_state {
140140 _Py_hashtable_t * hinfo_table ;
141+ PyObject * unknown_hash_error ;
141142} hmacmodule_state ;
142143
143144static inline hmacmodule_state *
@@ -328,13 +329,34 @@ hmacmodule_init_hash_info_table(hmacmodule_state *state)
328329 return state -> hinfo_table == NULL ? -1 : 0 ;
329330}
330331
332+ static int
333+ hmacmodule_init_exceptions (PyObject * module , hmacmodule_state * state )
334+ {
335+ #define ADD_EXC (ATTR , NAME , BASE ) \
336+ do { \
337+ state->ATTR = PyErr_NewException("_hmac." NAME, BASE, NULL); \
338+ if (state->ATTR == NULL) { \
339+ return -1; \
340+ } \
341+ if (PyModule_AddObjectRef(module, NAME, state->ATTR) < 0) { \
342+ return -1; \
343+ } \
344+ } while (0)
345+ ADD_EXC (unknown_hash_error , "UnknownHashError" , PyExc_ValueError );
346+ #undef ADD_EXC
347+ return 0 ;
348+ }
349+
331350static int
332351hmacmodule_exec (PyObject * module )
333352{
334353 hmacmodule_state * state = get_hmacmodule_state (module );
335354 if (hmacmodule_init_hash_info_table (state ) < 0 ) {
336355 return -1 ;
337356 }
357+ if (hmacmodule_init_exceptions (module , state ) < 0 ) {
358+ return -1 ;
359+ }
338360 return 0 ;
339361}
340362
@@ -343,6 +365,7 @@ hmacmodule_traverse(PyObject *mod, visitproc visit, void *arg)
343365{
344366 Py_VISIT (Py_TYPE (mod ));
345367 hmacmodule_state * state = get_hmacmodule_state (mod );
368+ Py_VISIT (state -> unknown_hash_error );
346369 return 0 ;
347370}
348371
@@ -354,6 +377,7 @@ hmacmodule_clear(PyObject *mod)
354377 _Py_hashtable_destroy (state -> hinfo_table );
355378 state -> hinfo_table = NULL ;
356379 }
380+ Py_CLEAR (state -> unknown_hash_error );
357381 return 0 ;
358382}
359383
0 commit comments