Merge pull request #561 from Alexander-N/exceptions

Use to_string() instead of description() for exception messages
This commit is contained in:
Yuji Kanagawa 2019-08-01 12:35:58 +09:00 committed by GitHub
commit c6de61f7e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 5 deletions

View File

@ -11,7 +11,6 @@ use crate::IntoPyPointer;
use crate::Python;
use crate::{IntoPyObject, ToBorrowedObject, ToPyObject};
use libc::c_int;
use std::error::Error;
use std::ffi::CString;
use std::io;
use std::os::raw::c_char;
@ -412,7 +411,7 @@ macro_rules! impl_to_pyerr {
($err: ty, $pyexc: ty) => {
impl PyErrArguments for $err {
fn arguments(&self, py: Python) -> PyObject {
self.description().to_object(py)
self.to_string().to_object(py)
}
}
@ -459,10 +458,9 @@ impl std::convert::From<io::Error> for PyErr {
}
}
/// Extract `errno` and `errdesc` from from `io::Error`
impl PyErrArguments for io::Error {
fn arguments(&self, py: Python) -> PyObject {
(self.raw_os_error().unwrap_or(0), self.description()).to_object(py)
self.to_string().to_object(py)
}
}
@ -474,7 +472,7 @@ impl<W: 'static + Send + std::fmt::Debug> std::convert::From<std::io::IntoInnerE
impl<W: Send + std::fmt::Debug> PyErrArguments for std::io::IntoInnerError<W> {
fn arguments(&self, py: Python) -> PyObject {
self.description().to_object(py)
self.to_string().to_object(py)
}
}

28
tests/test_exceptions.rs Normal file
View File

@ -0,0 +1,28 @@
use pyo3::prelude::*;
use pyo3::types::IntoPyDict;
use pyo3::wrap_pyfunction;
use std::fs::File;
mod common;
#[pyfunction]
fn fail_to_open_file() -> PyResult<()> {
File::open("not_there.txt")?;
Ok(())
}
#[test]
fn test_filenotfounderror() {
let gil = Python::acquire_gil();
let py = gil.python();
let fail_to_open_file = wrap_pyfunction!(fail_to_open_file)(py);
let d = [("fail_to_open_file", fail_to_open_file)].into_py_dict(py);
match py.run("fail_to_open_file()", None, Some(d)) {
Ok(()) => panic!("Call should raise a FileNotFoundError"),
Err(e) => {
py_assert!(py, e, "isinstance(e, FileNotFoundError)");
py_assert!(py, e, "'No such file or directory (os error 2)' == str(e)");
}
};
}