@@ -5,7 +5,7 @@ use super::{
55use crate :: common:: lock:: PyRwLockReadGuard ;
66use crate :: {
77 builtins:: { PyBaseExceptionRef , PyType } ,
8- convert:: { ToPyObject , ToPyResult , TryFromObject } ,
8+ convert:: { ToPyException , ToPyObject , ToPyResult , TryFromObject } ,
99 VirtualMachine ,
1010} ;
1111use 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
3333impl 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 >
276276where
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