Skip to content

Commit

Permalink
Replace PyErr::from_value_bound calls with .into
Browse files Browse the repository at this point in the history
  • Loading branch information
LilyFoote committed Feb 23, 2024
1 parent 3fd8f6b commit 481fcaf
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/coroutine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Coroutine {
(Some(exc), Some(cb)) => cb.throw(exc),
(Some(exc), None) => {
self.close();
return Err(PyErr::from_value_bound(exc.into_bound(py)));
return Err(exc.into_bound(py).into());
}
(None, _) => {}
}
Expand Down
2 changes: 1 addition & 1 deletion src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ mod tests {
);

// Restoring should preserve the same error
let e = PyErr::from_value_bound(decode_err.into_any());
let e: PyErr = decode_err.into();
e.restore(py);

assert_eq!(
Expand Down
4 changes: 2 additions & 2 deletions src/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ mod inner {
#[pymethods(crate = "pyo3")]
impl UnraisableCapture {
pub fn hook(&mut self, unraisable: Bound<'_, PyAny>) {
let err = PyErr::from_value_bound(unraisable.getattr("exc_value").unwrap());
let err = unraisable.getattr("exc_value").unwrap();
let instance = unraisable.getattr("object").unwrap();
self.capture = Some((err, instance.into()));
self.capture = Some((err.into(), instance.into()));
}
}

Expand Down
40 changes: 17 additions & 23 deletions src/types/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,34 @@ impl<'a> PyStringData<'a> {
match self {
Self::Ucs1(data) => match str::from_utf8(data) {
Ok(s) => Ok(Cow::Borrowed(s)),
Err(e) => Err(crate::PyErr::from_value_bound(
PyUnicodeDecodeError::new_utf8_bound(py, data, e)?.into_any(),
)),
Err(e) => Err(PyUnicodeDecodeError::new_utf8_bound(py, data, e)?.into()),
},
Self::Ucs2(data) => match String::from_utf16(data) {
Ok(s) => Ok(Cow::Owned(s)),
Err(e) => {
let mut message = e.to_string().as_bytes().to_vec();
message.push(0);

Err(crate::PyErr::from_value_bound(
PyUnicodeDecodeError::new_bound(
py,
CStr::from_bytes_with_nul(b"utf-16\0").unwrap(),
self.as_bytes(),
0..self.as_bytes().len(),
CStr::from_bytes_with_nul(&message).unwrap(),
)?
.into_any(),
))
}
},
Self::Ucs4(data) => match data.iter().map(|&c| std::char::from_u32(c)).collect() {
Some(s) => Ok(Cow::Owned(s)),
None => Err(crate::PyErr::from_value_bound(
PyUnicodeDecodeError::new_bound(
Err(PyUnicodeDecodeError::new_bound(
py,
CStr::from_bytes_with_nul(b"utf-32\0").unwrap(),
CStr::from_bytes_with_nul(b"utf-16\0").unwrap(),
self.as_bytes(),
0..self.as_bytes().len(),
CStr::from_bytes_with_nul(b"error converting utf-32\0").unwrap(),
CStr::from_bytes_with_nul(&message).unwrap(),
)?
.into_any(),
)),
.into())
}
},
Self::Ucs4(data) => match data.iter().map(|&c| std::char::from_u32(c)).collect() {
Some(s) => Ok(Cow::Owned(s)),
None => Err(PyUnicodeDecodeError::new_bound(
py,
CStr::from_bytes_with_nul(b"utf-32\0").unwrap(),
self.as_bytes(),
0..self.as_bytes().len(),
CStr::from_bytes_with_nul(b"error converting utf-32\0").unwrap(),
)?
.into()),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/traceback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ except Exception as e:
Some(&locals),
)
.unwrap();
let err = PyErr::from_value_bound(locals.get_item("err").unwrap().unwrap());
let err: PyErr = locals.get_item("err").unwrap().unwrap().into();
let traceback = err.value_bound(py).getattr("__traceback__").unwrap();
assert!(err.traceback_bound(py).unwrap().is(&traceback));
})
Expand Down

0 comments on commit 481fcaf

Please sign in to comment.