'Call up the pdb debugger if desired, always clean up the tb
reference.
Keywords:
- force(False): by default, this routine checks the instance call_pdb
flag and does not actually invoke the debugger if the flag is false.
The \'force\' option forces the debugger to activate even if the flag
is false.
If the call_pdb flag is set, the pdb interactive debugger is
invoked. In all cases, the self.tb reference to the current traceback
is deleted to prevent lingering references which hamper memory
management.
Note that each call to pdb() does an \'import readline\', so if your app
requires a special setup for the readline completers, you\'ll have to
fix that by hand after invoking the exception handler.'

def debugger(self, force=False):

    if (force or self.call_pdb):
        if (self.pdb is None):
            self.pdb = self.debugger_cls()
        display_trap = DisplayTrap(hook=sys.__displayhook__)
        with display_trap:
            self.pdb.reset()
            if (hasattr(self, 'tb') and (self.tb is not None)):
                etb = self.tb
            else:
                etb = self.tb = sys.last_traceback
            while ((self.tb is not None) and (self.tb.tb_next is not None)):
                self.tb = self.tb.tb_next
            if (etb and etb.tb_next):
                etb = etb.tb_next
            self.pdb.botframe = etb.tb_frame
            self.pdb.interaction(self.tb.tb_frame, self.tb)
    if hasattr(self, 'tb'):
        del self.tb
