diff --git a/src/sync.rs b/src/sync.rs index 91a83106..62843113 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -1,5 +1,5 @@ //! Synchronization mechanisms based on the Python GIL. -use crate::{types::PyString, types::PyType, Py, PyErr, PyVisit, Python}; +use crate::{types::PyString, types::PyType, Py, PyResult, PyVisit, Python}; use std::cell::UnsafeCell; /// Value with concurrent access protected by the GIL. @@ -196,7 +196,7 @@ impl GILOnceCell> { py: Python<'py>, module_name: &str, attr_name: &str, - ) -> Result<&'py PyType, PyErr> { + ) -> PyResult<&'py PyType> { self.get_or_try_init(py, || py.import(module_name)?.getattr(attr_name)?.extract()) .map(|ty| ty.as_ref(py)) } diff --git a/src/types/mapping.rs b/src/types/mapping.rs index bf6a6d49..583fc468 100644 --- a/src/types/mapping.rs +++ b/src/types/mapping.rs @@ -240,14 +240,10 @@ impl<'py> PyMappingMethods<'py> for Py2<'py, PyMapping> { } } -static MAPPING_ABC: GILOnceCell> = GILOnceCell::new(); - fn get_mapping_abc(py: Python<'_>) -> PyResult<&PyType> { - MAPPING_ABC - .get_or_try_init(py, || { - py.import("collections.abc")?.getattr("Mapping")?.extract() - }) - .map(|ty| ty.as_ref(py)) + static MAPPING_ABC: GILOnceCell> = GILOnceCell::new(); + + MAPPING_ABC.get_or_try_init_type_ref(py, "collections.abc", "Mapping") } impl PyTypeCheck for PyMapping { diff --git a/src/types/sequence.rs b/src/types/sequence.rs index c4c2b21b..beded132 100644 --- a/src/types/sequence.rs +++ b/src/types/sequence.rs @@ -523,14 +523,10 @@ where Ok(v) } -static SEQUENCE_ABC: GILOnceCell> = GILOnceCell::new(); - fn get_sequence_abc(py: Python<'_>) -> PyResult<&PyType> { - SEQUENCE_ABC - .get_or_try_init(py, || { - py.import("collections.abc")?.getattr("Sequence")?.extract() - }) - .map(|ty| ty.as_ref(py)) + static SEQUENCE_ABC: GILOnceCell> = GILOnceCell::new(); + + SEQUENCE_ABC.get_or_try_init_type_ref(py, "collections.abc", "Sequence") } impl PyTypeCheck for PySequence {