2 years ago

#65367

test-img

Frank Blabu

Debugging python code: Determine if an 'except' is waiting for an exception in some later frame

When executing a python script while having installed a trace callback via sys.set_trace (), I would like to be able to distinguish between exceptions which will later be excepted in the python code explicitly and exceptions which would 'fall through' and terminate the script. Only the second type must be handled in my case (by triggering a debugger).

How can I distinguish these types ? Is going up the frame structure and looking for some frame properties an option ?

A tracer function code sample looks like this:

class Tracer:
  
  def __enter__ (self):
    sys.settrace (Tracer.func)
    
  def __exit__ (self, ext_type, exc_value, traceback):
    sys.settrace (None)
    
  @staticmethod
  def func (frame, event, arg):
    if event == 'exception':
      exception, value, tb = arg

      if not will_be_excepted (exception):
        notify_debugger (exception, value, tb)
        
    return Tracer.func

with Tracer () as tracer:
  # Code to trace

So I'm looking for some idea about how to write the will_be_excepted () function in the above sample. Letting the exception pass and triggering the debugger after script termination is not an option, because I would like to break right at the frame the exception is thrown.

python

debugging

exception

trace

0 Answers

Your Answer

Accepted video resources