Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some std error types to PyErr #22

Merged
merged 2 commits into from
Jun 8, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/err.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std;
use std::ffi::CString;
use std::os::raw::c_char;
use std::error::Error;
use libc;

use ffi;
Expand Down Expand Up @@ -369,6 +370,90 @@ impl std::convert::From<PyErr> for std::io::Error {
}
}

impl std::convert::From<std::io::Error> for PyErr {
fn from(err: std::io::Error) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::IOError, _>(py, err.description())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be more precise, check utils.rs from async-tokio

}
}

impl<W: Send + std::fmt::Debug> std::convert::From<std::io::IntoInnerError<W>> for PyErr {
fn from(err: std::io::IntoInnerError<W>) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::IOError, _>(py, err.description())
}
}

impl std::convert::From<std::num::ParseIntError> for PyErr {
fn from(err: std::num::ParseIntError) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::ValueError, _>(py, err.description())
}
}

impl std::convert::From<std::num::ParseFloatError> for PyErr {
fn from(err: std::num::ParseFloatError) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::ValueError, _>(py, err.description())
}
}

impl std::convert::From<std::string::ParseError> for PyErr {
fn from(err: std::string::ParseError) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::ValueError, _>(py, err.description())
}
}

impl std::convert::From<std::str::ParseBoolError> for PyErr {
fn from(err: std::str::ParseBoolError) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::ValueError, _>(py, err.description())
}
}

impl std::convert::From<std::ffi::IntoStringError> for PyErr {
fn from(err: std::ffi::IntoStringError) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::UnicodeDecodeError, _>(py, err.description())
}
}

impl std::convert::From<std::str::Utf8Error> for PyErr {
fn from(err: std::str::Utf8Error) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::UnicodeDecodeError, _>(py, err.description())
}
}

impl std::convert::From<std::string::FromUtf8Error> for PyErr {
fn from(err: std::string::FromUtf8Error) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::UnicodeDecodeError, _>(py, err.description())
}
}

impl std::convert::From<std::string::FromUtf16Error> for PyErr {
fn from(err: std::string::FromUtf16Error) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::UnicodeDecodeError, _>(py, err.description())
}
}

impl std::convert::From<std::char::DecodeUtf16Error> for PyErr {
fn from(err: std::char::DecodeUtf16Error) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::UnicodeDecodeError, _>(py, err.description())
}
}

impl std::convert::From<std::net::AddrParseError> for PyErr {
fn from(err: std::net::AddrParseError) -> Self {
let py = unsafe { Python::assume_gil_acquired() };
PyErr::new::<exc::ValueError, _>(py, err.description())
}
}

pub fn panic_after_error() -> ! {
unsafe { ffi::PyErr_Print(); }
panic!("Python API called failed");
Expand Down