I would like to write a function in Python (2.6) that can determine if it is called from exception handling code somewhere on the stack.
This is for specialized use of magazines. In the python module, the loggingcaller must explicitly indicate that exception information should be logged (either by calling logger.exception()or using the keyword exc_info). I would like my registrar to do this automatically based on whether it is called from the exception handling code.
I thought that checking sys.exc_info () might be the answer, but it also returns information about an exception from an already handled exception. (From docs : "This function returns a tuple of three values that provide information about the exception that is currently being processed ... If the current stack stack does not handle the exception, information is taken from the frame of the calling stack or its caller, and so on, until no stack frame will be found that handles the exception. Here, "exception handling" is defined as 'executing or executing an except clause.' ")
In addition, since I want this to be transparent to the caller, I do not want to use exc_clear()or anything else in the sentence except.
What is the right way to do this?
Uncorroborated
source
share