@@ -611,9 +611,9 @@ static int
611611_hashlib_HASH_copy_locked (HASHobject * self , EVP_MD_CTX * new_ctx_p )
612612{
613613 int result ;
614- ENTER_HASHLIB (self );
614+ HASHLIB_ACQUIRE_LOCK (self );
615615 result = EVP_MD_CTX_copy (new_ctx_p , self -> ctx );
616- LEAVE_HASHLIB (self );
616+ HASHLIB_RELEASE_LOCK (self );
617617 return result ;
618618}
619619
@@ -733,29 +733,16 @@ static PyObject *
733733_hashlib_HASH_update_impl (HASHobject * self , PyObject * obj )
734734/*[clinic end generated code: output=62ad989754946b86 input=aa1ce20e3f92ceb6]*/
735735{
736- int result ;
736+ int rc ;
737737 Py_buffer view ;
738-
739738 GET_BUFFER_VIEW_OR_ERROUT (obj , & view );
740-
741- if (!self -> use_mutex && view .len >= HASHLIB_GIL_MINSIZE ) {
742- self -> use_mutex = true;
743- }
744- if (self -> use_mutex ) {
745- Py_BEGIN_ALLOW_THREADS
746- PyMutex_Lock (& self -> mutex );
747- result = _hashlib_HASH_hash (self , view .buf , view .len );
748- PyMutex_Unlock (& self -> mutex );
749- Py_END_ALLOW_THREADS
750- } else {
751- result = _hashlib_HASH_hash (self , view .buf , view .len );
752- }
753-
739+ Py_BEGIN_ALLOW_THREADS
740+ HASHLIB_ACQUIRE_LOCK (self );
741+ rc = _hashlib_HASH_hash (self , view .buf , view .len );
742+ HASHLIB_RELEASE_LOCK (self );
743+ Py_END_ALLOW_THREADS
754744 PyBuffer_Release (& view );
755-
756- if (result == -1 )
757- return NULL ;
758- Py_RETURN_NONE ;
745+ return rc < 0 ? NULL : Py_None ;
759746}
760747
761748static PyMethodDef HASH_methods [] = {
@@ -1060,15 +1047,11 @@ _hashlib_HASH(PyObject *module, const char *digestname, PyObject *data_obj,
10601047 }
10611048
10621049 if (view .buf && view .len ) {
1063- if (view .len >= HASHLIB_GIL_MINSIZE ) {
1064- /* We do not initialize self->lock here as this is the constructor
1065- * where it is not yet possible to have concurrent access. */
1066- Py_BEGIN_ALLOW_THREADS
1067- result = _hashlib_HASH_hash (self , view .buf , view .len );
1068- Py_END_ALLOW_THREADS
1069- } else {
1050+ /* Do not use self->mutex here as this is the constructor
1051+ * where it is not yet possible to have concurrent access. */
1052+ Py_BEGIN_ALLOW_THREADS
10701053 result = _hashlib_HASH_hash (self , view .buf , view .len );
1071- }
1054+ Py_END_ALLOW_THREADS
10721055 if (result == -1 ) {
10731056 assert (PyErr_Occurred ());
10741057 Py_CLEAR (self );
@@ -1701,7 +1684,7 @@ _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
17011684 HASHLIB_INIT_MUTEX (self );
17021685
17031686 if ((msg_obj != NULL ) && (msg_obj != Py_None )) {
1704- if (! _hmac_update (self , msg_obj )) {
1687+ if (_hmac_update (self , msg_obj ) < 0 ) {
17051688 goto error ;
17061689 }
17071690 }
@@ -1718,9 +1701,9 @@ static int
17181701locked_HMAC_CTX_copy (HMAC_CTX * new_ctx_p , HMACobject * self )
17191702{
17201703 int result ;
1721- ENTER_HASHLIB (self );
1704+ HASHLIB_ACQUIRE_LOCK (self );
17221705 result = HMAC_CTX_copy (new_ctx_p , self -> ctx );
1723- LEAVE_HASHLIB (self );
1706+ HASHLIB_RELEASE_LOCK (self );
17241707 return result ;
17251708}
17261709
@@ -1743,35 +1726,26 @@ _hashlib_hmac_digest_size(HMACobject *self)
17431726static int
17441727_hmac_update (HMACobject * self , PyObject * obj )
17451728{
1746- int r ;
1729+ int r = 1 ;
17471730 Py_buffer view = {0 };
17481731
1749- GET_BUFFER_VIEW_OR_ERROR (obj , & view , return 0 );
1750-
1751- if (!self -> use_mutex && view .len >= HASHLIB_GIL_MINSIZE ) {
1752- self -> use_mutex = true;
1753- }
1754- if (self -> use_mutex ) {
1732+ GET_BUFFER_VIEW_OR_ERROR (obj , & view , return - 1 );
1733+ if (view .len > 0 ) {
17551734 Py_BEGIN_ALLOW_THREADS
1756- PyMutex_Lock ( & self -> mutex );
1757- r = HMAC_Update (self -> ctx ,
1758- (const unsigned char * )view .buf ,
1759- (size_t )view .len );
1760- PyMutex_Unlock ( & self -> mutex );
1735+ HASHLIB_ACQUIRE_LOCK ( self );
1736+ r = HMAC_Update (self -> ctx ,
1737+ (const unsigned char * )view .buf ,
1738+ (size_t )view .len );
1739+ HASHLIB_RELEASE_LOCK ( self );
17611740 Py_END_ALLOW_THREADS
1762- } else {
1763- r = HMAC_Update (self -> ctx ,
1764- (const unsigned char * )view .buf ,
1765- (size_t )view .len );
17661741 }
1767-
17681742 PyBuffer_Release (& view );
17691743
17701744 if (r == 0 ) {
17711745 notify_ssl_error_occurred ();
1772- return 0 ;
1746+ return -1 ;
17731747 }
1774- return 1 ;
1748+ return 0 ;
17751749}
17761750
17771751/*[clinic input]
@@ -1845,7 +1819,7 @@ static PyObject *
18451819_hashlib_HMAC_update_impl (HMACobject * self , PyObject * msg )
18461820/*[clinic end generated code: output=f31f0ace8c625b00 input=1829173bb3cfd4e6]*/
18471821{
1848- if (! _hmac_update (self , msg )) {
1822+ if (_hmac_update (self , msg ) < 0 ) {
18491823 return NULL ;
18501824 }
18511825 Py_RETURN_NONE ;
@@ -2412,17 +2386,6 @@ hashlib_exception(PyObject *module)
24122386 return 0 ;
24132387}
24142388
2415- static int
2416- hashlib_constants (PyObject * module )
2417- {
2418- if (PyModule_AddIntConstant (module , "_GIL_MINSIZE" ,
2419- HASHLIB_GIL_MINSIZE ) < 0 )
2420- {
2421- return -1 ;
2422- }
2423- return 0 ;
2424- }
2425-
24262389static PyModuleDef_Slot hashlib_slots [] = {
24272390 {Py_mod_exec , hashlib_init_hashtable },
24282391 {Py_mod_exec , hashlib_init_HASH_type },
@@ -2431,7 +2394,6 @@ static PyModuleDef_Slot hashlib_slots[] = {
24312394 {Py_mod_exec , hashlib_md_meth_names },
24322395 {Py_mod_exec , hashlib_init_constructors },
24332396 {Py_mod_exec , hashlib_exception },
2434- {Py_mod_exec , hashlib_constants },
24352397 {Py_mod_multiple_interpreters , Py_MOD_PER_INTERPRETER_GIL_SUPPORTED },
24362398 {Py_mod_gil , Py_MOD_GIL_NOT_USED },
24372399 {0 , NULL }
0 commit comments