Skip to content

Commit aea6888

Browse files
authored
simplify _hacl_convert_errno
1 parent 0b2f4e4 commit aea6888

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

Modules/hmacmodule.c

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -493,46 +493,40 @@ narrow_hmac_hash_kind(hmacmodule_state *state, HMAC_Hash_Kind kind)
493493
static int
494494
_hacl_convert_errno(hacl_errno_t code, PyObject *algorithm)
495495
{
496+
assert(PyGILState_GetThisThreadState() != NULL);
497+
if (code == Hacl_Streaming_Types_Success) {
498+
return 0;
499+
}
500+
PyGILState_STATE gstate = PyGILState_Ensure();
496501
switch (code) {
497-
case Hacl_Streaming_Types_Success: {
498-
return 0;
499-
}
500502
case Hacl_Streaming_Types_InvalidAlgorithm: {
501-
PyGILState_STATE gstate = PyGILState_Ensure();
502503
// only makes sense if an algorithm is known at call time
503504
assert(algorithm != NULL);
504505
assert(PyUnicode_CheckExact(algorithm));
505506
PyErr_Format(PyExc_ValueError, "invalid algorithm: %U", algorithm);
506-
PyGILState_Release(gstate);
507-
return -1;
507+
break;
508508
}
509509
case Hacl_Streaming_Types_InvalidLength: {
510-
PyGILState_STATE gstate = PyGILState_Ensure();
511510
PyErr_SetString(PyExc_ValueError, "invalid length");
512-
PyGILState_Release(gstate);
513-
return -1;
511+
break;
514512
}
515513
case Hacl_Streaming_Types_MaximumLengthExceeded: {
516-
PyGILState_STATE gstate = PyGILState_Ensure();
517514
PyErr_SetString(PyExc_OverflowError, "maximum length exceeded");
518-
PyGILState_Release(gstate);
519-
return -1;
515+
break;
520516
}
521517
case Hacl_Streaming_Types_OutOfMemory: {
522-
PyGILState_STATE gstate = PyGILState_Ensure();
523518
PyErr_NoMemory();
524-
PyGILState_Release(gstate);
525-
return -1;
519+
break;
526520
}
527521
default: {
528-
PyGILState_STATE gstate = PyGILState_Ensure();
529522
PyErr_Format(PyExc_RuntimeError,
530523
"HACL* internal routine failed with error code: %d",
531524
code);
532-
PyGILState_Release(gstate);
533-
return -1;
525+
break;
534526
}
535527
}
528+
PyGILState_Release(gstate);
529+
return -1;
536530
}
537531

538532
/*

0 commit comments

Comments
 (0)