'Handles the current exception, printing a message to stderr as appropriate.
It will reraise the exception if necessary (eg if bail is true)'

def handle_exception(self):

    (eclass, eval, etb) = sys.exc_info()
    if isinstance(eval, SystemExit):
        eval._handle_exception_saw_this = True
        raise
    self._out_colour()
    self.write(self.stderr, self.colour.error)
    if isinstance(eval, KeyboardInterrupt):
        self.handle_interrupt()
        text = 'Interrupted'
    else:
        text = str(eval)
    if (not text.endswith('\n')):
        text = (text + '\n')
    if len(self._input_descriptions):
        for i in range(len(self._input_descriptions)):
            if (i == 0):
                pref = 'At    '
            else:
                pref = (('    ' * i) + 'From    ')
            self.write(self.stderr, ((pref + self._input_descriptions[i]) + '\n'))
    self.write(self.stderr, text)
    if self.exceptions:
        stack = []
        while etb:
            stack.append(etb.tb_frame)
            etb = etb.tb_next
        for frame in stack:
            self.write(self.stderr, ('\nFrame    %s    in    %s    at    line    %d\n' % (frame.f_code.co_name, frame.f_code.co_filename, frame.f_lineno)))
            vars = list(frame.f_locals.items())
            vars.sort()
            for (k, v) in vars:
                try:
                    v = repr(v)[:80]
                except:
                    v = '<Unable    to    convert    to    string>'
                self.write(self.stderr, ('%10s    =    %s\n' % (k, v)))
        self.write(self.stderr, ('\n%s:    %s\n' % (eclass, repr(eval))))
    self.write(self.stderr, self.colour.error_)
    eval._handle_exception_saw_this = True
    if self.bail:
        raise
