Skip to content

Commit

Permalink
reintroduce PyErr constructors and methods (#4475)
Browse files Browse the repository at this point in the history
* reintroduce `PyErr` constructors and methods

* fixup

Co-authored-by: Lily Foote <[email protected]>

---------

Co-authored-by: Lily Foote <[email protected]>
  • Loading branch information
Icxolu and LilyFoote committed Aug 23, 2024
1 parent e068a30 commit 18bca6e
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 146 deletions.
6 changes: 3 additions & 3 deletions guide/src/python-from-rust/calling-existing-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,9 @@ class House(object):
.call_method1(
"__exit__",
(
e.get_type_bound(py),
e.value_bound(py),
e.traceback_bound(py),
e.get_type(py),
e.value(py),
e.traceback(py),
),
)
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/conversions/anyhow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ mod test_anyhow {
let pyerr = py
.run(ffi::c_str!("raise err"), None, Some(&locals))
.unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
assert_eq!(pyerr.value(py).to_string(), expected_contents);
})
}

Expand All @@ -169,7 +169,7 @@ mod test_anyhow {
let pyerr = py
.run(ffi::c_str!("raise err"), None, Some(&locals))
.unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
assert_eq!(pyerr.value(py).to_string(), expected_contents);
})
}

Expand Down
18 changes: 10 additions & 8 deletions src/conversions/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ use crate::types::{
timezone_utc, PyDate, PyDateAccess, PyDateTime, PyDelta, PyDeltaAccess, PyTime, PyTimeAccess,
PyTzInfo, PyTzInfoAccess,
};
use crate::{
ffi, Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject,
};
#[cfg(Py_LIMITED_API)]
use crate::{intern, DowncastError};
use crate::{Bound, FromPyObject, IntoPy, PyAny, PyErr, PyObject, PyResult, Python, ToPyObject};
use chrono::offset::{FixedOffset, Utc};
use chrono::{
DateTime, Datelike, Duration, NaiveDate, NaiveDateTime, NaiveTime, Offset, TimeZone, Timelike,
Expand Down Expand Up @@ -701,13 +703,13 @@ fn naive_datetime_to_py_datetime(

fn warn_truncated_leap_second(obj: &Bound<'_, PyAny>) {
let py = obj.py();
if let Err(e) = PyErr::warn_bound(
if let Err(e) = PyErr::warn(
py,
&py.get_type::<PyUserWarning>(),
"ignored leap-second, `datetime` does not support leap-seconds",
ffi::c_str!("ignored leap-second, `datetime` does not support leap-seconds"),
0,
) {
e.write_unraisable_bound(py, Some(&obj.as_borrowed()))
e.write_unraisable(py, Some(&obj.as_borrowed()))
};
}

Expand Down Expand Up @@ -835,7 +837,7 @@ mod tests {
assert!(result.is_err());
let res = result.err().unwrap();
// Also check the error message is what we expect
let msg = res.value_bound(py).repr().unwrap().to_string();
let msg = res.value(py).repr().unwrap().to_string();
assert_eq!(msg, "TypeError(\"zoneinfo.ZoneInfo(key='Europe/London') is not a fixed offset timezone\")");
});
}
Expand All @@ -850,7 +852,7 @@ mod tests {
// Now test that converting a PyDateTime with tzinfo to a NaiveDateTime fails
let res: PyResult<NaiveDateTime> = py_datetime.extract();
assert_eq!(
res.unwrap_err().value_bound(py).repr().unwrap().to_string(),
res.unwrap_err().value(py).repr().unwrap().to_string(),
"TypeError('expected a datetime without tzinfo')"
);
});
Expand All @@ -865,14 +867,14 @@ mod tests {
// Now test that converting a PyDateTime with tzinfo to a NaiveDateTime fails
let res: PyResult<DateTime<Utc>> = py_datetime.extract();
assert_eq!(
res.unwrap_err().value_bound(py).repr().unwrap().to_string(),
res.unwrap_err().value(py).repr().unwrap().to_string(),
"TypeError('expected a datetime with non-None tzinfo')"
);

// Now test that converting a PyDateTime with tzinfo to a NaiveDateTime fails
let res: PyResult<DateTime<FixedOffset>> = py_datetime.extract();
assert_eq!(
res.unwrap_err().value_bound(py).repr().unwrap().to_string(),
res.unwrap_err().value(py).repr().unwrap().to_string(),
"TypeError('expected a datetime with non-None tzinfo')"
);
});
Expand Down
4 changes: 2 additions & 2 deletions src/conversions/eyre.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ mod tests {
let pyerr = py
.run(ffi::c_str!("raise err"), None, Some(&locals))
.unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
assert_eq!(pyerr.value(py).to_string(), expected_contents);
})
}

Expand All @@ -174,7 +174,7 @@ mod tests {
let pyerr = py
.run(ffi::c_str!("raise err"), None, Some(&locals))
.unwrap_err();
assert_eq!(pyerr.value_bound(py).to_string(), expected_contents);
assert_eq!(pyerr.value(py).to_string(), expected_contents);
})
}

Expand Down
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(PyErr::from_value(exc.into_bound(py)));
}
(None, _) => {}
}
Expand Down
4 changes: 2 additions & 2 deletions src/err/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ mod tests {

let py_err_recovered_from_rust_err: PyErr = rust_err_from_py_err.into();
assert!(py_err_recovered_from_rust_err
.value_bound(py)
.is(py_error_clone.value_bound(py))); // It should be the same exception
.value(py)
.is(py_error_clone.value(py))); // It should be the same exception
})
};

Expand Down
Loading

0 comments on commit 18bca6e

Please sign in to comment.