Skip to content

Implemented Error trait for PyErr #126

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
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
14 changes: 14 additions & 0 deletions src/err.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use std::ptr;
use libc::c_char;
use conversion::ToPyObject;
use std::ffi::CString;
use std::error::Error;
use std::fmt;

/**
Defines a new exception type.
Expand Down Expand Up @@ -394,6 +396,18 @@ impl <'p> std::convert::From<PythonObjectDowncastError<'p>> for PyErr {
}
}

impl Error for PyErr {
fn description(&self) -> &str {
"Error originating from the rust-cpython bindings."
}
}

impl fmt::Display for PyErr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PyErr: ptype {:?} pvalue {:?}", self.ptype, self.pvalue)
Copy link
Owner

Choose a reason for hiding this comment

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

I would expect Display to result in something like str(e) in Python.
This code here is more like repr(type(e)) + repr(e), except even weirder if the exception is not yet initialized (PyErr::new_lazy_init).

}
}

/// Construct PyObject from the result of a Python FFI call that returns a new reference (owned pointer).
/// Returns `Err(PyErr)` if the pointer is `null`.
/// Unsafe because the pointer might be invalid.
Expand Down