@@ -688,31 +688,18 @@ impl Frame {
688
688
}
689
689
690
690
bytecode:: Instruction :: Raise { argc } => {
691
+ let cause = match argc {
692
+ 2 => self . get_exception ( vm, true ) ?,
693
+ _ => vm. get_none ( ) ,
694
+ } ;
691
695
let exception = match argc {
692
- 1 => self . pop_value ( ) ,
693
- 0 | 2 | 3 => panic ! ( "Not implemented!" ) ,
696
+ 1 | 2 => self . get_exception ( vm , false ) ? ,
697
+ 0 | 3 => panic ! ( "Not implemented!" ) ,
694
698
_ => panic ! ( "Invalid parameter for RAISE_VARARGS, must be between 0 to 3" ) ,
695
699
} ;
696
- if objtype:: isinstance ( & exception, & vm. ctx . exceptions . base_exception_type ) {
697
- info ! ( "Exception raised: {:?}" , exception) ;
698
- Err ( exception)
699
- } else if let Ok ( exception) = PyClassRef :: try_from_object ( vm, exception) {
700
- if objtype:: issubclass ( & exception, & vm. ctx . exceptions . base_exception_type ) {
701
- let exception = vm. new_empty_exception ( exception) ?;
702
- info ! ( "Exception raised: {:?}" , exception) ;
703
- Err ( exception)
704
- } else {
705
- let msg = format ! (
706
- "Can only raise BaseException derived types, not {}" ,
707
- exception
708
- ) ;
709
- let type_error_type = vm. ctx . exceptions . type_error . clone ( ) ;
710
- let type_error = vm. new_exception ( type_error_type, msg) ;
711
- Err ( type_error)
712
- }
713
- } else {
714
- Err ( vm. new_type_error ( "exceptions must derive from BaseException" . to_string ( ) ) )
715
- }
700
+ info ! ( "Exception raised: {:?} with cause: {:?}" , exception, cause) ;
701
+ vm. set_attr ( & exception, vm. new_str ( "__cause__" . to_string ( ) ) , cause) ?;
702
+ Err ( exception)
716
703
}
717
704
718
705
bytecode:: Instruction :: Break => {
@@ -1215,6 +1202,28 @@ impl Frame {
1215
1202
let stack = self . stack . borrow_mut ( ) ;
1216
1203
stack[ stack. len ( ) - depth - 1 ] . clone ( )
1217
1204
}
1205
+
1206
+ fn get_exception ( & self , vm : & VirtualMachine , none_allowed : bool ) -> PyResult {
1207
+ let exception = self . pop_value ( ) ;
1208
+ if none_allowed && vm. get_none ( ) . is ( & exception) {
1209
+ Ok ( exception)
1210
+ } else if objtype:: isinstance ( & exception, & vm. ctx . exceptions . base_exception_type ) {
1211
+ Ok ( exception)
1212
+ } else if let Ok ( exception) = PyClassRef :: try_from_object ( vm, exception) {
1213
+ if objtype:: issubclass ( & exception, & vm. ctx . exceptions . base_exception_type ) {
1214
+ let exception = vm. new_empty_exception ( exception) ?;
1215
+ Ok ( exception)
1216
+ } else {
1217
+ let msg = format ! (
1218
+ "Can only raise BaseException derived types, not {}" ,
1219
+ exception
1220
+ ) ;
1221
+ Err ( vm. new_type_error ( msg) )
1222
+ }
1223
+ } else {
1224
+ Err ( vm. new_type_error ( "exceptions must derive from BaseException" . to_string ( ) ) )
1225
+ }
1226
+ }
1218
1227
}
1219
1228
1220
1229
impl fmt:: Debug for Frame {
0 commit comments