add convenience method to PyErr and exception types

This commit is contained in:
Nikolay Kim 2017-08-04 11:47:35 -07:00
parent 21939c4d8b
commit a61c5e56a0
5 changed files with 38 additions and 7 deletions

View File

@ -117,13 +117,13 @@ struct BaseClass {
#[py::class(base=BaseClass)]
struct MyEventLoop {
fn method2(&self) -> PyResult<()> {
self.get_super().method()
self.get_base().method()
}
}
```
`ObjectProtocol` trait provide `get_super()` method. It returns reference to instance of
parent class.
`ObjectProtocol` trait provides `get_base()` method. It returns reference to instance of
base class.
## Object properties

View File

@ -54,10 +54,19 @@ macro_rules! py_exception {
}
}
impl<T> std::convert::Into<$crate::PyResult<T>> for $name {
fn into(self) -> $crate::PyResult<T> {
$crate::PyErr::new::<$name, _>(()).into()
}
}
impl $name {
pub fn new<T: $crate::ToPyObject + 'static>(args: T) -> $crate::PyErr {
$crate::PyErr::new::<$name, T>(args)
}
pub fn into<R, T: $crate::ToPyObject + 'static>(args: T) -> $crate::PyResult<R> {
$crate::PyErr::new::<$name, T>(args).into()
}
#[inline]
fn type_object() -> *mut $crate::ffi::PyTypeObject {
static mut TYPE_OBJECT: *mut $crate::ffi::PyTypeObject =
@ -457,6 +466,13 @@ impl std::convert::From<PyErr> for std::io::Error {
}
}
/// Convert `PyErr` to `PyResult<T>`
impl<T> std::convert::Into<PyResult<T>> for PyErr {
fn into(self) -> PyResult<T> {
Err(self)
}
}
macro_rules! impl_to_pyerr {
($err: ty, $pyexc: ty) => {
impl PyErrArguments for $err {

View File

@ -1,6 +1,4 @@
// Copyright (c) 2017-present PyO3 Project and Contributors
//
// based on Daniel Grunwald's https://github.com/dgrunwald/rust-cpython
//! This module contains the standard python exception types.
@ -25,10 +23,18 @@ macro_rules! exc_type(
PyErr::new::<$name, _>(())
}
}
impl<T> std::convert::Into<$crate::PyResult<T>> for $name {
fn into(self) -> $crate::PyResult<T> {
PyErr::new::<$name, _>(()).into()
}
}
impl $name {
pub fn new<V: ToPyObject + 'static>(args: V) -> PyErr {
PyErr::new::<$name, V>(args)
}
pub fn into<R, V: ToPyObject + 'static>(args: V) -> PyResult<R> {
PyErr::new::<$name, V>(args).into()
}
}
impl PyTypeObject for $name {
#[inline(always)]

View File

@ -39,12 +39,23 @@ macro_rules! import_exception {
}
}
impl<T> ::std::convert::Into<$crate::PyResult<T>> for $name {
fn into(self) -> $crate::PyResult<T> {
$crate::PyErr::new::<$name, _>(()).into()
}
}
impl $name {
pub fn new<T: $crate::ToPyObject + 'static>(args: T) -> $crate::PyErr
where Self: $crate::typeob::PyTypeObject + Sized
{
$crate::PyErr::new::<Self, T>(args)
}
pub fn into<R, T: $crate::ToPyObject + 'static>(args: T) -> $crate::PyResult<R>
where Self: $crate::typeob::PyTypeObject + Sized
{
$crate::PyErr::new::<Self, T>(args).into()
}
}
impl $crate::typeob::PyTypeObject for $name {

View File

@ -115,8 +115,6 @@ impl<T> PyObjectAlloc<T> for T where T : PyTypeInfo {
T::init_type();
let obj = ffi::PyType_GenericAlloc(T::type_object(), 0);
println!("ALLOC {:?} {:?}", obj, ffi::Py_REFCNT(obj));
let ptr = (obj as *mut u8).offset(T::OFFSET) as *mut T;
std::ptr::write(ptr, value);