1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
#![cfg(feature = "macros")]
use pyo3::prelude::*;
use pyo3::{exceptions, py_run};
use std::error::Error;
use std::fmt;
#[cfg(not(target_os = "windows"))]
use std::fs::File;
mod test_utils;
#[pyfunction]
#[cfg(not(target_os = "windows"))]
fn fail_to_open_file() -> PyResult<()> {
File::open("not_there.txt")?;
Ok(())
}
#[test]
#[cfg_attr(target_arch = "wasm32", ignore)] // Not sure why this fails.
#[cfg(not(target_os = "windows"))]
fn test_filenotfounderror() {
Python::attach(|py| {
let fail_to_open_file = wrap_pyfunction!(fail_to_open_file)(py).unwrap();
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 {
exceptions::PyOSError::new_err(err.to_string())
}
}
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() {
Python::attach(|py| {
let call_fail_with_custom_error =
wrap_pyfunction!(call_fail_with_custom_error)(py).unwrap();
py_run!(
py,
call_fail_with_custom_error,
r#"
try:
call_fail_with_custom_error()
except OSError as e:
assert str(e) == "Oh no!"
"#
);
});
}
#[test]
fn test_exception_nosegfault() {
use std::net::TcpListener;
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());
}
#[test]
#[cfg(Py_3_8)]
fn test_write_unraisable() {
use pyo3::{exceptions::PyRuntimeError, types::PyNotImplemented};
use test_utils::UnraisableCapture;
Python::attach(|py| {
UnraisableCapture::enter(py, |capture| {
let err = PyRuntimeError::new_err("foo");
err.write_unraisable(py, None);
let (err, object) = capture.take_capture().unwrap();
assert_eq!(err.to_string(), "RuntimeError: foo");
assert!(object.is_none());
let err = PyRuntimeError::new_err("bar");
err.write_unraisable(py, Some(&PyNotImplemented::get(py)));
let (err, object) = capture.take_capture().unwrap();
assert_eq!(err.to_string(), "RuntimeError: bar");
assert!(object.is(PyNotImplemented::get(py)));
});
});
}
|