Support TryFromSliceError/TryFromIntError -> PyErr conversion

This commit is contained in:
kngwyu 2019-10-12 17:25:28 +09:00
parent e6ad49b6bf
commit ee69ffd0f0
2 changed files with 7 additions and 3 deletions

View File

@ -490,8 +490,10 @@ impl<W: Send + std::fmt::Debug> PyErrArguments for std::io::IntoInnerError<W> {
} }
} }
impl_to_pyerr!(std::array::TryFromSliceError, exceptions::ValueError);
impl_to_pyerr!(std::num::ParseIntError, exceptions::ValueError); impl_to_pyerr!(std::num::ParseIntError, exceptions::ValueError);
impl_to_pyerr!(std::num::ParseFloatError, exceptions::ValueError); impl_to_pyerr!(std::num::ParseFloatError, exceptions::ValueError);
impl_to_pyerr!(std::num::TryFromIntError, exceptions::ValueError);
impl_to_pyerr!(std::string::ParseError, exceptions::ValueError); impl_to_pyerr!(std::string::ParseError, exceptions::ValueError);
impl_to_pyerr!(std::str::ParseBoolError, exceptions::ValueError); impl_to_pyerr!(std::str::ParseBoolError, exceptions::ValueError);
impl_to_pyerr!(std::ffi::IntoStringError, exceptions::UnicodeDecodeError); impl_to_pyerr!(std::ffi::IntoStringError, exceptions::UnicodeDecodeError);

View File

@ -155,11 +155,12 @@ impl PySequenceProtocol for Sequence {
Ok(5) Ok(5)
} }
fn __getitem__(&self, key: isize) -> PyResult<isize> { fn __getitem__(&self, key: isize) -> PyResult<usize> {
if key == 5 { let idx: usize = std::convert::TryFrom::try_from(key)?;
if idx == 5 {
return Err(PyErr::new::<IndexError, _>(())); return Err(PyErr::new::<IndexError, _>(()));
} }
Ok(key) Ok(idx)
} }
} }
@ -170,6 +171,7 @@ fn sequence() {
let c = Py::new(py, Sequence {}).unwrap(); let c = Py::new(py, Sequence {}).unwrap();
py_assert!(py, c, "list(c) == [0, 1, 2, 3, 4]"); py_assert!(py, c, "list(c) == [0, 1, 2, 3, 4]");
py_assert!(py, c, "c[-1] == 4");
py_expect_exception!(py, c, "c['abc']", TypeError); py_expect_exception!(py, c, "c['abc']", TypeError);
} }