The behavior of sys.exc_info() is described in python docs and on SO and SO as:
- within an except block or methods called by an except block, a triple describing the exception
- elsewhere, a triple of None values
So why does this nosetest fail?
def test_lang_stack(self):
try:
self.assertEquals((None,None,None), sys.exc_info()) # no exception
a = 5 / 0
except:
self.assertNotEquals((None,None,None), sys.exc_info()) # got exception
else:
self.fail('should not get here')
#finally:
# self.assertEquals((None,None,None), sys.exc_info()) # already handled, right?
self.assertEquals((None,None,None), sys.exc_info()) # already handled, right?
It fails at the last line. If you uncomment the finally block, it fails there instead.
I do see that if I put all this inside one method and call from a different method, then the calling method does not see the exception. The exc_info value seems to remain set to the end of the method where an exception is thrown, even after the exception is handled.
I'm using python2.7 on OSX.