2019-07-30 23:09:39 +00:00
|
|
|
use pyo3::prelude::*;
|
2019-08-02 22:45:53 +00:00
|
|
|
use pyo3::{exceptions, py_run, wrap_pyfunction, PyErr, PyResult};
|
|
|
|
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;
|
|
|
|
|
|
|
|
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]
|
2019-08-03 20:29:49 +00:00
|
|
|
#[cfg(not(target_os = "windows"))]
|
2019-07-30 23:09:39 +00:00
|
|
|
fn test_filenotfounderror() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2020-09-03 13:48:32 +00:00
|
|
|
let fail_to_open_file = wrap_pyfunction!(fail_to_open_file)(py).unwrap();
|
2019-08-02 22:45:53 +00:00
|
|
|
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
fail_to_open_file,
|
|
|
|
r#"
|
|
|
|
try:
|
|
|
|
fail_to_open_file()
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
assert str(e) == "No such file or directory (os error 2)"
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct CustomError;
|
|
|
|
|
|
|
|
impl Error for CustomError {}
|
|
|
|
|
|
|
|
impl fmt::Display for CustomError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
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() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
2020-09-03 13:48:32 +00:00
|
|
|
let call_fail_with_custom_error = wrap_pyfunction!(call_fail_with_custom_error)(py).unwrap();
|
2019-08-02 22:45:53 +00:00
|
|
|
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
call_fail_with_custom_error,
|
|
|
|
r#"
|
|
|
|
try:
|
|
|
|
call_fail_with_custom_error()
|
|
|
|
except OSError as e:
|
|
|
|
assert str(e) == "Oh no!"
|
|
|
|
"#
|
|
|
|
);
|
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());
|
|
|
|
}
|