2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
|
|
|
|
2019-07-30 23:09:39 +00:00
|
|
|
use pyo3::prelude::*;
|
2024-02-19 22:07:05 +00:00
|
|
|
use pyo3::{exceptions, py_run};
|
2019-08-02 22:45:53 +00:00
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
2020-02-09 10:54:00 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-07-30 23:09:39 +00:00
|
|
|
use std::fs::File;
|
|
|
|
|
2023-09-24 12:34:53 +00:00
|
|
|
#[path = "../src/tests/common.rs"]
|
2019-07-30 23:09:39 +00:00
|
|
|
mod common;
|
|
|
|
|
|
|
|
#[pyfunction]
|
2020-02-09 10:54:00 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-07-30 23:09:39 +00:00
|
|
|
fn fail_to_open_file() -> PyResult<()> {
|
|
|
|
File::open("not_there.txt")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-06-08 04:59:18 +00:00
|
|
|
#[cfg_attr(target_arch = "wasm32", ignore)] // Not sure why this fails.
|
2019-08-03 20:29:49 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-07-30 23:09:39 +00:00
|
|
|
fn test_filenotfounderror() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-03-12 22:57:03 +00:00
|
|
|
let fail_to_open_file = wrap_pyfunction_bound!(fail_to_open_file)(py).unwrap();
|
2019-08-02 22:45:53 +00:00
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
fail_to_open_file,
|
|
|
|
r#"
|
2019-08-02 22:45:53 +00:00
|
|
|
try:
|
|
|
|
fail_to_open_file()
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
assert str(e) == "No such file or directory (os error 2)"
|
|
|
|
"#
|
2022-07-19 17:34:23 +00:00
|
|
|
);
|
|
|
|
});
|
2019-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CustomError;
|
|
|
|
|
|
|
|
impl Error for CustomError {}
|
|
|
|
|
|
|
|
impl fmt::Display for CustomError {
|
2022-03-23 07:07:28 +00:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-08-02 22:45:53 +00:00
|
|
|
write!(f, "Oh no!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl std::convert::From<CustomError> for PyErr {
|
|
|
|
fn from(err: CustomError) -> PyErr {
|
2020-08-25 19:33:36 +00:00
|
|
|
exceptions::PyOSError::new_err(err.to_string())
|
2019-08-02 22:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fail_with_custom_error() -> Result<(), CustomError> {
|
|
|
|
Err(CustomError)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn call_fail_with_custom_error() -> PyResult<()> {
|
|
|
|
fail_with_custom_error()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_error() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
|
|
|
let call_fail_with_custom_error =
|
2024-03-12 22:57:03 +00:00
|
|
|
wrap_pyfunction_bound!(call_fail_with_custom_error)(py).unwrap();
|
2019-08-02 22:45:53 +00:00
|
|
|
|
2022-07-19 17:34:23 +00:00
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
call_fail_with_custom_error,
|
|
|
|
r#"
|
2019-08-02 22:45:53 +00:00
|
|
|
try:
|
|
|
|
call_fail_with_custom_error()
|
|
|
|
except OSError as e:
|
|
|
|
assert str(e) == "Oh no!"
|
|
|
|
"#
|
2022-07-19 17:34:23 +00:00
|
|
|
);
|
|
|
|
});
|
2019-07-30 23:09:39 +00:00
|
|
|
}
|
2019-09-07 06:38:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_exception_nosegfault() {
|
2020-12-31 15:46:54 +00:00
|
|
|
use std::net::TcpListener;
|
2019-09-07 06:38:59 +00:00
|
|
|
fn io_err() -> PyResult<()> {
|
|
|
|
TcpListener::bind("no:address")?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
fn parse_int() -> PyResult<()> {
|
|
|
|
"@_@".parse::<i64>()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
assert!(io_err().is_err());
|
|
|
|
assert!(parse_int().is_err());
|
|
|
|
}
|
2023-01-18 18:49:39 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(Py_3_8)]
|
2023-12-28 13:41:38 +00:00
|
|
|
#[cfg_attr(not(feature = "gil-refs"), allow(deprecated))]
|
2023-01-18 18:49:39 +00:00
|
|
|
fn test_write_unraisable() {
|
2023-05-24 09:04:05 +00:00
|
|
|
use common::UnraisableCapture;
|
2023-07-30 20:58:21 +00:00
|
|
|
use pyo3::{exceptions::PyRuntimeError, ffi};
|
2023-01-18 18:49:39 +00:00
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2023-05-24 09:04:05 +00:00
|
|
|
let capture = UnraisableCapture::install(py);
|
2023-01-18 18:49:39 +00:00
|
|
|
|
|
|
|
assert!(capture.borrow(py).capture.is_none());
|
|
|
|
|
|
|
|
let err = PyRuntimeError::new_err("foo");
|
|
|
|
err.write_unraisable(py, None);
|
|
|
|
|
|
|
|
let (err, object) = capture.borrow_mut(py).capture.take().unwrap();
|
|
|
|
assert_eq!(err.to_string(), "RuntimeError: foo");
|
|
|
|
assert!(object.is_none(py));
|
|
|
|
|
|
|
|
let err = PyRuntimeError::new_err("bar");
|
2024-02-03 20:56:23 +00:00
|
|
|
err.write_unraisable(py, Some(py.NotImplemented().as_ref(py)));
|
2023-01-18 18:49:39 +00:00
|
|
|
|
|
|
|
let (err, object) = capture.borrow_mut(py).capture.take().unwrap();
|
|
|
|
assert_eq!(err.to_string(), "RuntimeError: bar");
|
|
|
|
assert!(object.as_ptr() == unsafe { ffi::Py_NotImplemented() });
|
|
|
|
|
2023-05-24 09:04:05 +00:00
|
|
|
capture.borrow_mut(py).uninstall(py);
|
2023-01-18 18:49:39 +00:00
|
|
|
});
|
|
|
|
}
|