Skip to content

Commit c2dd487

Browse files
committed
PyResult<T, E>
1 parent 6343332 commit c2dd487

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

vm/src/object/ext.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55
use crate::common::lock::PyRwLockReadGuard;
66
use crate::{
77
builtins::{PyBaseExceptionRef, PyType},
8-
convert::{ToPyObject, ToPyResult, TryFromObject},
8+
convert::{ToPyException, ToPyObject, ToPyResult, TryFromObject},
99
VirtualMachine,
1010
};
1111
use std::{borrow::Borrow, fmt, ops::Deref};
@@ -27,7 +27,7 @@ Basically reference counting, but then done by rust.
2727
/// Use this type for functions which return a python object or an exception.
2828
/// Both the python object and the python exception are `PyObjectRef` types
2929
/// since exceptions are also python objects.
30-
pub type PyResult<T = PyObjectRef> = Result<T, PyBaseExceptionRef>; // A valid value, or an exception
30+
pub type PyResult<T = PyObjectRef, E = PyBaseExceptionRef> = Result<T, E>; // A valid value, or an exception
3131

3232
// TODO: remove these 2 impls
3333
impl fmt::Display for PyObjectRef {
@@ -272,12 +272,21 @@ where
272272
}
273273
}
274274

275-
impl<T> ToPyResult for PyResult<T>
275+
impl<T, E> ToPyResult for PyResult<T, E>
276276
where
277277
T: ToPyObject,
278+
E: ToPyException,
278279
{
279280
#[inline(always)]
280281
fn to_pyresult(self, vm: &VirtualMachine) -> PyResult {
281282
self.map(|res| T::to_pyobject(res, vm))
283+
.map_err(|e| E::to_pyexception(e, vm))
284+
}
285+
}
286+
287+
impl ToPyException for PyBaseExceptionRef {
288+
#[inline(always)]
289+
fn to_pyexception(self, _vm: &VirtualMachine) -> PyBaseExceptionRef {
290+
self
282291
}
283292
}

0 commit comments

Comments
 (0)