added convinience method

This commit is contained in:
Nikolay Kim 2017-04-29 21:31:35 -07:00
parent 8d728663d7
commit 257be7119c
1 changed files with 16 additions and 1 deletions

View File

@ -19,7 +19,7 @@
use std;
use python::{PythonObject, ToPythonPointer, Python, PythonObjectDowncastError,
PythonObjectWithTypeObject, PyClone, PyDrop};
use objects::{PyObject, PyType, exc};
use objects::{PyObject, PyType, ToPyTuple, exc};
#[cfg(feature="python27-sys")]
use objects::oldstyle::PyClass;
use ffi;
@ -268,6 +268,21 @@ impl PyErr {
}
}
/// Construct a new error, with the usual lazy initialization of Python exceptions.
/// `exc` is the exception type; usually one of the standard exceptions like `py.get_type::<exc::RuntimeError>()`.
/// `args` is the a tuple of arguments to pass to the exception constructor.
#[inline]
pub fn new_err<A>(py: Python, exc: &PyType, args: A) -> PyErr
where A: ToPyTuple
{
let exc = exc.clone_ref(py);
PyErr {
ptype: exc.into_object(),
pvalue: Some(args.to_py_tuple(py).into_object()),
ptraceback: None
}
}
/// Print a standard traceback to sys.stderr.
pub fn print(self, py: Python) {
self.restore(py);