@@ -60,14 +60,14 @@ use std::hash::Hash;
6060use std:: marker:: PhantomData ;
6161use std:: sync:: Mutex ;
6262
63+ use pyo3:: sync:: MutexExt ;
6364use pyo3:: { Bound , Py , Python } ;
6465use rustc_hash:: FxHashMap ;
6566
6667use crate :: dtype:: { clone_methods_impl, Element , PyArrayDescr , PyArrayDescrMethods } ;
6768use crate :: npyffi:: {
6869 PyArray_DatetimeDTypeMetaData , PyDataType_C_METADATA , NPY_DATETIMEUNIT , NPY_TYPES ,
6970} ;
70- use crate :: ThreadStateGuard ;
7171
7272/// Represents the [datetime units][datetime-units] supported by NumPy
7373///
@@ -224,13 +224,10 @@ impl TypeDescriptors {
224224
225225 #[ allow( clippy:: wrong_self_convention) ]
226226 fn from_unit < ' py > ( & self , py : Python < ' py > , unit : NPY_DATETIMEUNIT ) -> Bound < ' py , PyArrayDescr > {
227- // Detach from the runtime to avoid deadlocking on acquiring the mutex.
228- let ts_guard = ThreadStateGuard :: new ( ) ;
229-
230- let mut dtypes = self . dtypes . lock ( ) . expect ( "dtype cache poisoned" ) ;
231-
232- // Now we hold the mutex so it's safe to re-attach to the runtime.
233- drop ( ts_guard) ;
227+ let mut dtypes = self
228+ . dtypes
229+ . lock_py_attached ( py)
230+ . expect ( "dtype cache poisoned" ) ;
234231
235232 let dtype = match dtypes. get_or_insert_with ( Default :: default) . entry ( unit) {
236233 Entry :: Occupied ( entry) => entry. into_mut ( ) ,
0 commit comments