Merge pull request #3683 from PyO3/use-type-ref-helper

Some boy scouting w.r.t. usage of our internal helper functions when handling collection ABC
This commit is contained in:
Adam Reichold 2023-12-21 10:46:56 +00:00 committed by GitHub
commit 5b12cf1b8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 20 deletions

View File

@ -1,5 +1,5 @@
//! Synchronization mechanisms based on the Python GIL. //! 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; use std::cell::UnsafeCell;
/// Value with concurrent access protected by the GIL. /// Value with concurrent access protected by the GIL.
@ -196,7 +196,7 @@ impl GILOnceCell<Py<PyType>> {
py: Python<'py>, py: Python<'py>,
module_name: &str, module_name: &str,
attr_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()) self.get_or_try_init(py, || py.import(module_name)?.getattr(attr_name)?.extract())
.map(|ty| ty.as_ref(py)) .map(|ty| ty.as_ref(py))
} }

View File

@ -240,14 +240,10 @@ impl<'py> PyMappingMethods<'py> for Py2<'py, PyMapping> {
} }
} }
static MAPPING_ABC: GILOnceCell<Py<PyType>> = GILOnceCell::new();
fn get_mapping_abc(py: Python<'_>) -> PyResult<&PyType> { fn get_mapping_abc(py: Python<'_>) -> PyResult<&PyType> {
MAPPING_ABC static MAPPING_ABC: GILOnceCell<Py<PyType>> = GILOnceCell::new();
.get_or_try_init(py, || {
py.import("collections.abc")?.getattr("Mapping")?.extract() MAPPING_ABC.get_or_try_init_type_ref(py, "collections.abc", "Mapping")
})
.map(|ty| ty.as_ref(py))
} }
impl PyTypeCheck for PyMapping { impl PyTypeCheck for PyMapping {
@ -260,8 +256,10 @@ impl PyTypeCheck for PyMapping {
PyDict::is_type_of(object) PyDict::is_type_of(object)
|| get_mapping_abc(object.py()) || get_mapping_abc(object.py())
.and_then(|abc| object.is_instance(abc)) .and_then(|abc| object.is_instance(abc))
// TODO: surface errors in this chain to the user .unwrap_or_else(|err| {
.unwrap_or(false) err.write_unraisable(object.py(), Some(object));
false
})
} }
} }

View File

@ -523,14 +523,10 @@ where
Ok(v) Ok(v)
} }
static SEQUENCE_ABC: GILOnceCell<Py<PyType>> = GILOnceCell::new();
fn get_sequence_abc(py: Python<'_>) -> PyResult<&PyType> { fn get_sequence_abc(py: Python<'_>) -> PyResult<&PyType> {
SEQUENCE_ABC static SEQUENCE_ABC: GILOnceCell<Py<PyType>> = GILOnceCell::new();
.get_or_try_init(py, || {
py.import("collections.abc")?.getattr("Sequence")?.extract() SEQUENCE_ABC.get_or_try_init_type_ref(py, "collections.abc", "Sequence")
})
.map(|ty| ty.as_ref(py))
} }
impl PyTypeCheck for PySequence { impl PyTypeCheck for PySequence {
@ -544,8 +540,10 @@ impl PyTypeCheck for PySequence {
|| PyTuple::is_type_of(object) || PyTuple::is_type_of(object)
|| get_sequence_abc(object.py()) || get_sequence_abc(object.py())
.and_then(|abc| object.is_instance(abc)) .and_then(|abc| object.is_instance(abc))
// TODO: surface errors in this chain to the user .unwrap_or_else(|err| {
.unwrap_or(false) err.write_unraisable(object.py(), Some(object));
false
})
} }
} }