@@ -660,6 +660,8 @@ func do_END_FINALLY(vm *Vm, arg int32) {
660660 switch vm .exit {
661661 case exitYield :
662662 panic ("Unexpected exitYield in END_FINALLY" )
663+ case exitException :
664+ panic ("Unexpected exitException in END_FINALLY" )
663665 case exitReturn , exitContinue :
664666 vm .result = vm .POP ()
665667 case exitSilenced :
@@ -689,7 +691,7 @@ func do_END_FINALLY(vm *Vm, arg int32) {
689691 } else {
690692 vm .ClearException ()
691693 }
692-
694+ debugf ( "END_FINALLY: vm.exit = %v \n " , vm . exit )
693695}
694696
695697// Loads the __build_class__ helper function to the stack which
@@ -1201,7 +1203,7 @@ func do_DELETE_DEREF(vm *Vm, i int32) {
12011203func (vm * Vm ) raise (exc , cause py.Object ) {
12021204 if exc == nil {
12031205 // raise (with no parameters == re-raise)
1204- if vm .exc .Value == nil {
1206+ if ! vm .exc .IsSet () {
12051207 vm .SetException (py .ExceptionNewf (py .RuntimeError , "No active exception to reraise" ))
12061208 } else {
12071209 // Signal the existing exception again
@@ -1211,6 +1213,7 @@ func (vm *Vm) raise(exc, cause py.Object) {
12111213 // raise <instance>
12121214 // raise <type>
12131215 excException := py .MakeException (exc )
1216+ debugf ("raise: excException = %v\n " , excException )
12141217 vm .SetException (excException )
12151218 if cause != nil {
12161219 excException .Cause = py .MakeException (cause )
@@ -1431,24 +1434,17 @@ func (vm *Vm) UnwindBlock(frame *py.Frame, block *py.TryBlock) {
14311434
14321435// Unwinds the stack in the presence of an exception
14331436func (vm * Vm ) UnwindExceptHandler (frame * py.Frame , block * py.TryBlock ) {
1437+ debugf ("** UnwindExceptHandler stack depth %v\n " , vm .STACK_LEVEL ())
14341438 if vm .STACK_LEVEL () < block .Level + 3 {
14351439 panic ("Couldn't find traceback on stack" )
14361440 } else {
14371441 frame .Stack = frame .Stack [:block .Level + 3 ]
14381442 }
1439- // If have just raised an exception, don't overwrite it
1440- //
1441- // FIXME if have two exceptions python shows both tracebacks
1442- //
1443- // FIXME this is a departure from python's way not sure it is
1444- // correct
1445- if vm .exc .Value != nil {
1446- vm .DROPN (3 )
1447- } else {
1448- vm .exc .Type = vm .POP ().(* py.Type )
1449- vm .exc .Value = vm .POP ()
1450- vm .exc .Traceback = vm .POP ().(* py.Traceback )
1451- }
1443+ debugf ("** UnwindExceptHandler stack depth now %v\n " , vm .STACK_LEVEL ())
1444+ vm .exc .Type = vm .POP ().(* py.Type )
1445+ vm .exc .Value = vm .POP ()
1446+ vm .exc .Traceback = vm .POP ().(* py.Traceback )
1447+ debugf ("** UnwindExceptHandler exc = (type: %v, value: %v, traceback: %v)\n " , vm .exc .Type , vm .exc .Value , vm .exc .Traceback )
14521448}
14531449
14541450// Run the virtual machine on a Frame object
@@ -1534,13 +1530,13 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
15341530 frame .Lasti = b .Handler
15351531 break
15361532 }
1537- if vm .exit & ( exitException | exitReraise ) != 0 && (b .Type == SETUP_EXCEPT || b .Type == SETUP_FINALLY ) {
1533+ if ( vm .exit == exitException || vm . exit == exitReraise ) && (b .Type == SETUP_EXCEPT || b .Type == SETUP_FINALLY ) {
15381534 debugf ("*** Exception\n " )
15391535 handler := b .Handler
15401536 // This invalidates b
15411537 frame .PushBlock (EXCEPT_HANDLER , - 1 , vm .STACK_LEVEL ())
1542- vm .PUSH (vm .old_exc .Traceback )
1543- vm .PUSH (vm .old_exc .Value )
1538+ vm .PUSH (vm .exc .Traceback )
1539+ vm .PUSH (vm .exc .Value )
15441540 vm .PUSH (vm .exc .Type ) // can be nil
15451541 // FIXME PyErr_Fetch(&exc, &val, &tb)
15461542 exc := vm .exc .Type
@@ -1563,7 +1559,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
15631559 break
15641560 }
15651561 if b .Type == SETUP_FINALLY {
1566- if vm .exit & ( exitReturn | exitContinue ) != 0 {
1562+ if vm .exit == exitReturn || vm . exit == exitContinue {
15671563 vm .PUSH (vm .result )
15681564 }
15691565 vm .PUSH (py .Int (vm .exit ))
@@ -1573,7 +1569,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
15731569 }
15741570 }
15751571 }
1576- if vm .exc .Value != nil {
1572+ if vm .exc .IsSet () {
15771573 return vm .result , vm .exc
15781574 }
15791575 return vm .result , nil
0 commit comments