Skip to content

Commit b2a2a1d

Browse files
authored
restrict IntoPyObject::Error to convert into PyErr (#4489)
1 parent 71b10b8 commit b2a2a1d

File tree

17 files changed

+71
-203
lines changed

17 files changed

+71
-203
lines changed

src/conversion.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ pub trait IntoPyObject<'py>: Sized {
284284
/// used to minimize reference counting overhead.
285285
type Output: BoundObject<'py, Self::Target>;
286286
/// The type returned in the event of a conversion error.
287-
type Error;
287+
type Error: Into<PyErr>;
288288

289289
/// Performs the conversion.
290290
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error>;
@@ -300,7 +300,6 @@ pub trait IntoPyObject<'py>: Sized {
300300
where
301301
I: IntoIterator<Item = Self> + AsRef<[Self]>,
302302
I::IntoIter: ExactSizeIterator<Item = Self>,
303-
PyErr: From<Self::Error>,
304303
{
305304
let mut iter = iter.into_iter().map(|e| {
306305
e.into_pyobject(py)
@@ -324,7 +323,6 @@ pub trait IntoPyObject<'py>: Sized {
324323
Self: private::Reference,
325324
I: IntoIterator<Item = Self> + AsRef<[<Self as private::Reference>::BaseType]>,
326325
I::IntoIter: ExactSizeIterator<Item = Self>,
327-
PyErr: From<Self::Error>,
328326
{
329327
let mut iter = iter.into_iter().map(|e| {
330328
e.into_pyobject(py)

src/conversions/chrono.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,6 @@ mod tests {
13271327
fn new_py_datetime_ob<'py, A>(py: Python<'py>, name: &str, args: A) -> Bound<'py, PyAny>
13281328
where
13291329
A: IntoPyObject<'py, Target = PyTuple>,
1330-
A::Error: Into<PyErr>,
13311330
{
13321331
py.import("datetime")
13331332
.unwrap()

src/conversions/either.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ impl<'py, L, R> IntoPyObject<'py> for Either<L, R>
7171
where
7272
L: IntoPyObject<'py>,
7373
R: IntoPyObject<'py>,
74-
L::Error: Into<PyErr>,
75-
R::Error: Into<PyErr>,
7674
{
7775
type Target = PyAny;
7876
type Output = Bound<'py, Self::Target>;
@@ -99,8 +97,6 @@ impl<'a, 'py, L, R> IntoPyObject<'py> for &'a Either<L, R>
9997
where
10098
&'a L: IntoPyObject<'py>,
10199
&'a R: IntoPyObject<'py>,
102-
<&'a L as IntoPyObject<'py>>::Error: Into<PyErr>,
103-
<&'a R as IntoPyObject<'py>>::Error: Into<PyErr>,
104100
{
105101
type Target = PyAny;
106102
type Output = Bound<'py, Self::Target>;

src/conversions/hashbrown.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ where
5959
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
6060
V: IntoPyObject<'py>,
6161
H: hash::BuildHasher,
62-
PyErr: From<K::Error> + From<V::Error>,
6362
{
6463
type Target = PyDict;
6564
type Output = Bound<'py, Self::Target>;
@@ -69,8 +68,8 @@ where
6968
let dict = PyDict::new(py);
7069
for (k, v) in self {
7170
dict.set_item(
72-
k.into_pyobject(py)?.into_bound(),
73-
v.into_pyobject(py)?.into_bound(),
71+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
72+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
7473
)?;
7574
}
7675
Ok(dict)
@@ -82,7 +81,6 @@ where
8281
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
8382
&'a V: IntoPyObject<'py>,
8483
H: hash::BuildHasher,
85-
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
8684
{
8785
type Target = PyDict;
8886
type Output = Bound<'py, Self::Target>;
@@ -92,8 +90,8 @@ where
9290
let dict = PyDict::new(py);
9391
for (k, v) in self {
9492
dict.set_item(
95-
k.into_pyobject(py)?.into_bound(),
96-
v.into_pyobject(py)?.into_bound(),
93+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
94+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
9795
)?;
9896
}
9997
Ok(dict)
@@ -143,7 +141,6 @@ impl<'py, K, H> IntoPyObject<'py> for hashbrown::HashSet<K, H>
143141
where
144142
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
145143
H: hash::BuildHasher,
146-
PyErr: From<K::Error>,
147144
{
148145
type Target = PySet;
149146
type Output = Bound<'py, Self::Target>;
@@ -166,7 +163,6 @@ impl<'a, 'py, K, H> IntoPyObject<'py> for &'a hashbrown::HashSet<K, H>
166163
where
167164
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
168165
H: hash::BuildHasher,
169-
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
170166
{
171167
type Target = PySet;
172168
type Output = Bound<'py, Self::Target>;

src/conversions/indexmap.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ where
122122
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
123123
V: IntoPyObject<'py>,
124124
H: hash::BuildHasher,
125-
PyErr: From<K::Error> + From<V::Error>,
126125
{
127126
type Target = PyDict;
128127
type Output = Bound<'py, Self::Target>;
@@ -132,8 +131,8 @@ where
132131
let dict = PyDict::new(py);
133132
for (k, v) in self {
134133
dict.set_item(
135-
k.into_pyobject(py)?.into_bound(),
136-
v.into_pyobject(py)?.into_bound(),
134+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
135+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
137136
)?;
138137
}
139138
Ok(dict)
@@ -145,7 +144,6 @@ where
145144
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
146145
&'a V: IntoPyObject<'py>,
147146
H: hash::BuildHasher,
148-
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
149147
{
150148
type Target = PyDict;
151149
type Output = Bound<'py, Self::Target>;
@@ -155,8 +153,8 @@ where
155153
let dict = PyDict::new(py);
156154
for (k, v) in self {
157155
dict.set_item(
158-
k.into_pyobject(py)?.into_bound(),
159-
v.into_pyobject(py)?.into_bound(),
156+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
157+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
160158
)?;
161159
}
162160
Ok(dict)

src/conversions/smallvec.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ impl<'py, A> IntoPyObject<'py> for SmallVec<A>
6060
where
6161
A: Array,
6262
A::Item: IntoPyObject<'py>,
63-
PyErr: From<<A::Item as IntoPyObject<'py>>::Error>,
6463
{
6564
type Target = PyAny;
6665
type Output = Bound<'py, Self::Target>;
@@ -80,7 +79,6 @@ impl<'a, 'py, A> IntoPyObject<'py> for &'a SmallVec<A>
8079
where
8180
A: Array,
8281
&'a A::Item: IntoPyObject<'py>,
83-
PyErr: From<<&'a A::Item as IntoPyObject<'py>>::Error>,
8482
{
8583
type Target = PyAny;
8684
type Output = Bound<'py, Self::Target>;

src/conversions/std/array.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ where
4040
impl<'py, T, const N: usize> IntoPyObject<'py> for [T; N]
4141
where
4242
T: IntoPyObject<'py>,
43-
PyErr: From<T::Error>,
4443
{
4544
type Target = PyAny;
4645
type Output = Bound<'py, Self::Target>;
@@ -59,7 +58,6 @@ where
5958
impl<'a, 'py, T, const N: usize> IntoPyObject<'py> for &'a [T; N]
6059
where
6160
&'a T: IntoPyObject<'py>,
62-
PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
6361
{
6462
type Target = PyAny;
6563
type Output = Bound<'py, Self::Target>;

src/conversions/std/map.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ where
5454
K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
5555
V: IntoPyObject<'py>,
5656
H: hash::BuildHasher,
57-
PyErr: From<K::Error> + From<V::Error>,
5857
{
5958
type Target = PyDict;
6059
type Output = Bound<'py, Self::Target>;
@@ -64,8 +63,8 @@ where
6463
let dict = PyDict::new(py);
6564
for (k, v) in self {
6665
dict.set_item(
67-
k.into_pyobject(py)?.into_bound(),
68-
v.into_pyobject(py)?.into_bound(),
66+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
67+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
6968
)?;
7069
}
7170
Ok(dict)
@@ -77,7 +76,6 @@ where
7776
&'a K: IntoPyObject<'py> + cmp::Eq + hash::Hash,
7877
&'a V: IntoPyObject<'py>,
7978
H: hash::BuildHasher,
80-
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
8179
{
8280
type Target = PyDict;
8381
type Output = Bound<'py, Self::Target>;
@@ -87,8 +85,8 @@ where
8785
let dict = PyDict::new(py);
8886
for (k, v) in self {
8987
dict.set_item(
90-
k.into_pyobject(py)?.into_bound(),
91-
v.into_pyobject(py)?.into_bound(),
88+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
89+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
9290
)?;
9391
}
9492
Ok(dict)
@@ -117,7 +115,6 @@ impl<'py, K, V> IntoPyObject<'py> for collections::BTreeMap<K, V>
117115
where
118116
K: IntoPyObject<'py> + cmp::Eq,
119117
V: IntoPyObject<'py>,
120-
PyErr: From<K::Error> + From<V::Error>,
121118
{
122119
type Target = PyDict;
123120
type Output = Bound<'py, Self::Target>;
@@ -127,8 +124,8 @@ where
127124
let dict = PyDict::new(py);
128125
for (k, v) in self {
129126
dict.set_item(
130-
k.into_pyobject(py)?.into_bound(),
131-
v.into_pyobject(py)?.into_bound(),
127+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
128+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
132129
)?;
133130
}
134131
Ok(dict)
@@ -139,7 +136,6 @@ impl<'a, 'py, K, V> IntoPyObject<'py> for &'a collections::BTreeMap<K, V>
139136
where
140137
&'a K: IntoPyObject<'py> + cmp::Eq,
141138
&'a V: IntoPyObject<'py>,
142-
PyErr: From<<&'a K as IntoPyObject<'py>>::Error> + From<<&'a V as IntoPyObject<'py>>::Error>,
143139
{
144140
type Target = PyDict;
145141
type Output = Bound<'py, Self::Target>;
@@ -149,8 +145,8 @@ where
149145
let dict = PyDict::new(py);
150146
for (k, v) in self {
151147
dict.set_item(
152-
k.into_pyobject(py)?.into_bound(),
153-
v.into_pyobject(py)?.into_bound(),
148+
k.into_pyobject(py).map_err(Into::into)?.into_bound(),
149+
v.into_pyobject(py).map_err(Into::into)?.into_bound(),
154150
)?;
155151
}
156152
Ok(dict)

src/conversions/std/set.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ impl<'py, K, S> IntoPyObject<'py> for collections::HashSet<K, S>
5858
where
5959
K: IntoPyObject<'py> + Eq + hash::Hash,
6060
S: hash::BuildHasher + Default,
61-
PyErr: From<K::Error>,
6261
{
6362
type Target = PySet;
6463
type Output = Bound<'py, Self::Target>;
@@ -81,7 +80,6 @@ impl<'a, 'py, K, H> IntoPyObject<'py> for &'a collections::HashSet<K, H>
8180
where
8281
&'a K: IntoPyObject<'py> + Eq + hash::Hash,
8382
H: hash::BuildHasher,
84-
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
8583
{
8684
type Target = PySet;
8785
type Output = Bound<'py, Self::Target>;
@@ -143,7 +141,6 @@ where
143141
impl<'py, K> IntoPyObject<'py> for collections::BTreeSet<K>
144142
where
145143
K: IntoPyObject<'py> + cmp::Ord,
146-
PyErr: From<K::Error>,
147144
{
148145
type Target = PySet;
149146
type Output = Bound<'py, Self::Target>;
@@ -165,7 +162,6 @@ where
165162
impl<'a, 'py, K> IntoPyObject<'py> for &'a collections::BTreeSet<K>
166163
where
167164
&'a K: IntoPyObject<'py> + cmp::Ord,
168-
PyErr: From<<&'a K as IntoPyObject<'py>>::Error>,
169165
{
170166
type Target = PySet;
171167
type Output = Bound<'py, Self::Target>;

src/conversions/std/slice.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ impl<'a> IntoPy<PyObject> for &'a [u8] {
2222
impl<'a, 'py, T> IntoPyObject<'py> for &'a [T]
2323
where
2424
&'a T: IntoPyObject<'py>,
25-
PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
2625
{
2726
type Target = PyAny;
2827
type Output = Bound<'py, Self::Target>;
@@ -86,7 +85,6 @@ impl<'py, T> IntoPyObject<'py> for Cow<'_, [T]>
8685
where
8786
T: Clone,
8887
for<'a> &'a T: IntoPyObject<'py>,
89-
for<'a> PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
9088
{
9189
type Target = PyAny;
9290
type Output = Bound<'py, Self::Target>;

src/conversions/std/vec.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ where
4343
impl<'py, T> IntoPyObject<'py> for Vec<T>
4444
where
4545
T: IntoPyObject<'py>,
46-
PyErr: From<T::Error>,
4746
{
4847
type Target = PyAny;
4948
type Output = Bound<'py, Self::Target>;
@@ -62,7 +61,6 @@ where
6261
impl<'a, 'py, T> IntoPyObject<'py> for &'a Vec<T>
6362
where
6463
&'a T: IntoPyObject<'py>,
65-
PyErr: From<<&'a T as IntoPyObject<'py>>::Error>,
6664
{
6765
type Target = PyAny;
6866
type Output = Bound<'py, Self::Target>;

0 commit comments

Comments
 (0)