Skip to content

Commit

Permalink
restrict IntoPyObject::Error to convert into PyErr (#4489)
Browse files Browse the repository at this point in the history
  • Loading branch information
Icxolu committed Aug 25, 2024
1 parent 71b10b8 commit b2a2a1d
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 203 deletions.
4 changes: 1 addition & 3 deletions src/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ pub trait IntoPyObject<'py>: Sized {
/// used to minimize reference counting overhead.
type Output: BoundObject<'py, Self::Target>;
/// The type returned in the event of a conversion error.
type Error;
type Error: Into<PyErr>;

/// Performs the conversion.
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error>;
Expand All @@ -300,7 +300,6 @@ pub trait IntoPyObject<'py>: Sized {
where
I: IntoIterator<Item = Self> + AsRef<[Self]>,
I::IntoIter: ExactSizeIterator<Item = Self>,
PyErr: From<Self::Error>,
{
let mut iter = iter.into_iter().map(|e| {
e.into_pyobject(py)
Expand All @@ -324,7 +323,6 @@ pub trait IntoPyObject<'py>: Sized {
Self: private::Reference,
I: IntoIterator<Item = Self> + AsRef<[<Self as private::Reference>::BaseType]>,
I::IntoIter: ExactSizeIterator<Item = Self>,
PyErr: From<Self::Error>,
{
let mut iter = iter.into_iter().map(|e| {
e.into_pyobject(py)
Expand Down
1 change: 0 additions & 1 deletion src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,7 +1327,6 @@ mod tests {
fn new_py_datetime_ob<'py, A>(py: Python<'py>, name: &str, args: A) -> Bound<'py, PyAny>
where
A: IntoPyObject<'py, Target = PyTuple>,
A::Error: Into<PyErr>,
{
py.import("datetime")
.unwrap()
Expand Down
4 changes: 0 additions & 4 deletions src/conversions/either.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ impl<'py, L, R> IntoPyObject<'py> for Either<L, R>
where
L: IntoPyObject<'py>,
R: IntoPyObject<'py>,
L::Error: Into<PyErr>,
R::Error: Into<PyErr>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand All @@ -99,8 +97,6 @@ impl<'a, 'py, L, R> IntoPyObject<'py> for &'a Either<L, R>
where
&'a L: IntoPyObject<'py>,
&'a R: IntoPyObject<'py>,
<&'a L as IntoPyObject<'py>>::Error: Into<PyErr>,
<&'a R as IntoPyObject<'py>>::Error: Into<PyErr>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand Down
12 changes: 4 additions & 8 deletions src/conversions/hashbrown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ where
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
V: IntoPyObject<'py>,
H: hash::BuildHasher,
PyErr: From<K::Error> + From<V::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -69,8 +68,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand All @@ -82,7 +81,6 @@ where
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
&'a V: IntoPyObject<'py>,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -92,8 +90,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand Down Expand Up @@ -143,7 +141,6 @@ impl<'py, K, H> IntoPyObject<'py> for hashbrown::HashSet<K, H>
where
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
H: hash::BuildHasher,
PyErr: From<K::Error>,
{
type Target = PySet;
type Output = Bound<'py, Self::Target>;
Expand All @@ -166,7 +163,6 @@ impl<'a, 'py, K, H> IntoPyObject<'py> for &'a hashbrown::HashSet<K, H>
where
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
{
type Target = PySet;
type Output = Bound<'py, Self::Target>;
Expand Down
10 changes: 4 additions & 6 deletions src/conversions/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ where
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
V: IntoPyObject<'py>,
H: hash::BuildHasher,
PyErr: From<K::Error> + From<V::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -132,8 +131,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand All @@ -145,7 +144,6 @@ where
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
&'a V: IntoPyObject<'py>,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -155,8 +153,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand Down
2 changes: 0 additions & 2 deletions src/conversions/smallvec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ impl<'py, A> IntoPyObject<'py> for SmallVec<A>
where
A: Array,
A::Item: IntoPyObject<'py>,
PyErr: From<<A::Item as IntoPyObject<'py>>::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand All @@ -80,7 +79,6 @@ impl<'a, 'py, A> IntoPyObject<'py> for &'a SmallVec<A>
where
A: Array,
&'a A::Item: IntoPyObject<'py>,
PyErr: From<<&'a A::Item as IntoPyObject<'py>>::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand Down
2 changes: 0 additions & 2 deletions src/conversions/std/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ where
impl<'py, T, const N: usize> IntoPyObject<'py> for [T; N]
where
T: IntoPyObject<'py>,
PyErr: From<T::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand All @@ -59,7 +58,6 @@ where
impl<'a, 'py, T, const N: usize> IntoPyObject<'py> for &'a [T; N]
where
&'a T: IntoPyObject<'py>,
PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand Down
20 changes: 8 additions & 12 deletions src/conversions/std/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ where
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
V: IntoPyObject<'py>,
H: hash::BuildHasher,
PyErr: From<K::Error> + From<V::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -64,8 +63,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand All @@ -77,7 +76,6 @@ where
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
&'a V: IntoPyObject<'py>,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -87,8 +85,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand Down Expand Up @@ -117,7 +115,6 @@ impl<'py, K, V> IntoPyObject<'py> for collections::BTreeMap<K, V>
where
K: IntoPyObject<'py> + cmp::Eq,
V: IntoPyObject<'py>,
PyErr: From<K::Error> + From<V::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -127,8 +124,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand All @@ -139,7 +136,6 @@ impl<'a, 'py, K, V> IntoPyObject<'py> for &'a collections::BTreeMap<K, V>
where
&'a K: IntoPyObject<'py> + cmp::Eq,
&'a V: IntoPyObject<'py>,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
{
type Target = PyDict;
type Output = Bound<'py, Self::Target>;
Expand All @@ -149,8 +145,8 @@ where
let dict = PyDict::new(py);
for (k, v) in self {
dict.set_item(
k.into_pyobject(py)?.into_bound(),
v.into_pyobject(py)?.into_bound(),
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
)?;
}
Ok(dict)
Expand Down
4 changes: 0 additions & 4 deletions src/conversions/std/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl<'py, K, S> IntoPyObject<'py> for collections::HashSet<K, S>
where
K: IntoPyObject<'py> + Eq + hash::Hash,
S: hash::BuildHasher + Default,
PyErr: From<K::Error>,
{
type Target = PySet;
type Output = Bound<'py, Self::Target>;
Expand All @@ -81,7 +80,6 @@ impl<'a, 'py, K, H> IntoPyObject<'py> for &'a collections::HashSet<K, H>
where
&'a K: IntoPyObject<'py> + Eq + hash::Hash,
H: hash::BuildHasher,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
{
type Target = PySet;
type Output = Bound<'py, Self::Target>;
Expand Down Expand Up @@ -143,7 +141,6 @@ where
impl<'py, K> IntoPyObject<'py> for collections::BTreeSet<K>
where
K: IntoPyObject<'py> + cmp::Ord,
PyErr: From<K::Error>,
{
type Target = PySet;
type Output = Bound<'py, Self::Target>;
Expand All @@ -165,7 +162,6 @@ where
impl<'a, 'py, K> IntoPyObject<'py> for &'a collections::BTreeSet<K>
where
&'a K: IntoPyObject<'py> + cmp::Ord,
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
{
type Target = PySet;
type Output = Bound<'py, Self::Target>;
Expand Down
2 changes: 0 additions & 2 deletions src/conversions/std/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ impl<'a> IntoPy<PyObject> for &'a [u8] {
impl<'a, 'py, T> IntoPyObject<'py> for &'a [T]
where
&'a T: IntoPyObject<'py>,
PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand Down Expand Up @@ -86,7 +85,6 @@ impl<'py, T> IntoPyObject<'py> for Cow<'_, [T]>
where
T: Clone,
for<'a> &'a T: IntoPyObject<'py>,
for<'a> PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand Down
2 changes: 0 additions & 2 deletions src/conversions/std/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ where
impl<'py, T> IntoPyObject<'py> for Vec<T>
where
T: IntoPyObject<'py>,
PyErr: From<T::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand All @@ -62,7 +61,6 @@ where
impl<'a, 'py, T> IntoPyObject<'py> for &'a Vec<T>
where
&'a T: IntoPyObject<'py>,
PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
{
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
Expand Down
Loading

0 comments on commit b2a2a1d

Please sign in to comment.