def format_exception_only(self): """Format the exception part of the traceback. The return value is a generator of strings, each ending in a newline. Normally, the generator emits a single string; however, for SyntaxError exceptions, it emites several lines that (when printed) display detailed information about where the syntax error occurred. The message indicating which exception occurred is always the last string in the output. """ if self.exc_type is None: yield _format_final_exc_line(None, self._str) return stype = getattr(self.exc_type, '__qualname__', self.exc_type.__name__) smod = u(self.exc_type.__module__) if smod not in ("__main__", "builtins", "exceptions"): stype = smod + u('.') + stype if not issubclass(self.exc_type, SyntaxError): yield _format_final_exc_line(stype, self._str) return # It was a syntax error; show exactly where the problem was found. filename = _some_fs_str(self.filename) or u("<string>") lineno = str(self.lineno) or u('?') yield u(' File "{0}", line {1}\n').format(filename, lineno) badline = None if self.text is not None: if type(self.text) is bytes: # Not decoded - get the line via linecache which will decode # for us. if self.lineno: badline = linecache.getline(filename, int(lineno)) if not badline: # But we can't for some reason, so fallback to attempting a # u cast. badline = u(self.text) else: badline = self.text offset = self.offset if badline is not None: yield u(' {0}\n').format(badline.strip()) if offset is not None: caretspace = badline.rstrip('\n') offset = min(len(caretspace), offset) - 1 caretspace = caretspace[:offset].lstrip() # non-space whitespace (likes tabs) must be kept for alignment caretspace = ((c.isspace() and c or ' ') for c in caretspace) yield u(' {0}^\n').format(''.join(caretspace)) msg = self.msg or u("<no detail available>") yield u("{0}: {1}\n").format(stype, msg)
def line(self): if self._line is None: self._line = linecache.getline(self.filename, self.lineno).strip() return self._line