def _format_source(self, source_map, colormap, lineno): bold_tp = self.tpl('source_bold') default_tpl = self.tpl('source_default') comment_tpl = self.tpl('source_comment') source_lines = OrderedDict() for ln in source_map: line = '' for snippet, ttype in source_map[ln]: if ttype in [sc.KEYWORD, sc.OP]: line += bold_tp % snippet elif ttype == sc.VAR: if snippet not in colormap: line += default_tpl % snippet else: hue, sat, val, bold = colormap[snippet] var_tpl = get_ansi_tpl(hue, sat, val, bold) line += var_tpl % snippet elif ttype == sc.CALL: line += bold_tp % snippet elif ttype == sc.COMMENT: line += comment_tpl % snippet else: line += default_tpl % snippet source_lines[ln] = line return source_lines
def format_exception_message(etype, evalue, tb=None, style='plaintext'): type_str = etype.__name__ val_str = str(evalue) if val_str: type_str += ": " if style == 'plaintext': return type_str + val_str else: sc = getattr(colorschemes, style) clr_head = get_ansi_tpl(*sc.colors['exception_type']) clr_msg = get_ansi_tpl(*sc.colors['exception_msg']) return clr_head % type_str + clr_msg % val_str
def format_exception_message(etype, evalue, tb=None, style='plaintext'): type_str = etype.__name__ val_str = str(evalue) if etype == SyntaxError: val_str += '\n %s\n %s^' % (evalue.text.rstrip(), ' '*evalue.offset) if val_str: type_str += ": " if style == 'plaintext': return type_str + val_str else: sc = getattr(colorschemes, style) clr_head = get_ansi_tpl(*sc.colors['exception_type']) clr_msg = get_ansi_tpl(*sc.colors['exception_msg']) return clr_head % type_str + clr_msg % val_str
def _format_assignments(self, assignments, colormap, truncation=500): msgs = [] for name, value in assignments.items(): val_str = format_value(value, indent=len(name) + self.var_indent + 3, truncation=truncation) assign_str = self.val_tpl % (name, val_str) hue, sat, val, bold = colormap.get(name, self.colors['var_invisible']) clr_str = get_ansi_tpl(hue, sat, val, bold) % assign_str msgs.append(clr_str) if len(msgs) > 0: return self.sep_vars + '\n' + ''.join(msgs) + self.sep_vars + '\n\n' else: return ''
def format_exc_info(etype, evalue, tb, style='plaintext', add_summary='auto', reverse=False, **kwargs): """ Format an exception traceback, including the exception message keyword args like stackprinter.format() """ msg = '' try: # First, recursively format any chained exceptions (exceptions during whose handling # the given one happened). # TODO: refactor this whole messy function to return a more... structured datastructure # before assembling a string, so that e.g. a summary of the whole chain can be shown at # the end. context = getattr(evalue, '__context__', None) cause = getattr(evalue, '__cause__', None) suppress_context = getattr(evalue, '__suppress_context__', False) if cause: chained_exc = cause chain_hint = ("\n\nThe above exception was the direct cause " "of the following exception:\n\n") elif context and not suppress_context: chained_exc = context chain_hint = ("\n\nWhile handling the above exception, " "another exception occurred:\n\n") else: chained_exc = None if chained_exc: msg += format_exc_info(chained_exc.__class__, chained_exc, chained_exc.__traceback__, style=style, add_summary=add_summary, reverse=reverse, **kwargs) if style == 'plaintext': msg += chain_hint else: sc = getattr(colorschemes, style) clr = get_ansi_tpl(*sc.colors['exception_type']) msg += clr % chain_hint # Now, actually do some formatting: msgs = [] if tb: frameinfos = [ex.get_info(tb_) for tb_ in _walk_traceback(tb)] stack_msg = format_stack(frameinfos, style=style, reverse=reverse, **kwargs) msgs.append(stack_msg) if add_summary == 'auto': add_summary = stack_msg.count('\n') > 50 if add_summary: summ = format_summary(frameinfos, style=style, reverse=reverse, **kwargs) summ += '\n' msgs.append('---- (full traceback below) ----\n\n' if reverse else '---- (full traceback above) ----\n') msgs.append(summ) exc = format_exception_message(etype, evalue, style=style) msgs.append('\n\n' if reverse else '') msgs.append(exc) if reverse: msgs = reversed(msgs) msg += ''.join(msgs) except Exception as exc: import os if 'PY_STACKPRINTER_DEBUG' in os.environ: raise our_tb = traceback.format_exception(exc.__class__, exc, exc.__traceback__, chain=False) where = getattr(exc, 'where', None) context = " while formatting " + str(where) if where else '' msg = 'Stackprinter failed%s:\n%s\n' % (context, ''.join(our_tb[-2:])) msg += 'So here is your original traceback at least:\n\n' msg += ''.join(traceback.format_exception(etype, evalue, tb)) return msg
def tpl(self, name): return get_ansi_tpl(*self.colors[name])