Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some boy scouting w.r.t. usage of our internal helper functions when handling collection ABC #3683

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/sync.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -196,7 +196,7 @@ impl GILOnceCell<Py<PyType>> {
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))
}
Expand Down
16 changes: 7 additions & 9 deletions src/types/mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
MAPPING_ABC
.get_or_try_init(py, || {
py.import("collections.abc")?.getattr("Mapping")?.extract()
})
.map(|ty| ty.as_ref(py))
static MAPPING_ABC: GILOnceCell<Py<PyType>> = GILOnceCell::new();

MAPPING_ABC.get_or_try_init_type_ref(py, "collections.abc", "Mapping")
}

impl PyTypeCheck for PyMapping {
Expand All @@ -260,8 +256,10 @@ impl PyTypeCheck for PyMapping {
PyDict::is_type_of(object)
|| get_mapping_abc(object.py())
.and_then(|abc| object.is_instance(abc))
// TODO: surface errors in this chain to the user
.unwrap_or(false)
.unwrap_or_else(|err| {
err.write_unraisable(object.py(), Some(object));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using write_unraisable here is a very good idea 👍

false
})
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/types/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,14 +523,10 @@ where
Ok(v)
}

static SEQUENCE_ABC: GILOnceCell<Py<PyType>> = 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<Py<PyType>> = GILOnceCell::new();

SEQUENCE_ABC.get_or_try_init_type_ref(py, "collections.abc", "Sequence")
}

impl PyTypeCheck for PySequence {
Expand All @@ -544,8 +540,10 @@ impl PyTypeCheck for PySequence {
|| PyTuple::is_type_of(object)
|| get_sequence_abc(object.py())
.and_then(|abc| object.is_instance(abc))
// TODO: surface errors in this chain to the user
.unwrap_or(false)
.unwrap_or_else(|err| {
err.write_unraisable(object.py(), Some(object));
false
})
}
}

Expand Down
Loading