def format_exc(info=None, context=5): if info: exc_type, exc_value, tb = info else: exc_type, exc_value, tb = sys.exc_info() try: exc_msg = restore_escapes(exc_value) while tb.tb_next is not None: module = tb.tb_frame.f_globals.get('__name__') or '?' if module == 'pony' or module.startswith('pony.'): tb = tb.tb_next else: break records = [] if issubclass(exc_type, SyntaxError) and exc_value.filename and exc_value.filename != '<?>': lines, index = getlines2(exc_value.filename, exc_value.lineno, context=5) source_encoding = detect_source_encoding(exc_value.filename) formatted_lines = [] for i, line in enumerate(lines): syntax_error_offset = None if i == index: syntax_error_offset = exc_value.offset - 1 formatted_lines.append(format_line(None, line.decode(source_encoding, 'replace'), syntax_error_offset)) record = Record(module='<?>', filename=exc_value.filename, lineno=exc_value.lineno, lines=formatted_lines, index=index) records = [ record ] else: frames = inspect.getinnerframes(tb, context) prev_frame = None for frame, filename, lineno, func, lines, index in frames: if index is None: continue module = frame.f_globals.get('__name__') or '?' source_encoding = detect_source_encoding(filename) formatted_lines = [ format_line(frame, line.decode(source_encoding, 'replace')) for line in lines ] record = Record(module=module, filename=filename, lineno=lineno, lines=formatted_lines, index=index, func=func) records.append(record) if module != 'pony.templating': pass elif func in ('_eval', '_compile'): element = prev_frame.f_locals['elem'] # instance of SyntaxElement subclass text, offsets, filename = (element.source + (None,))[:3] lineno, offset = pos2lineno(element.start, offsets) lines, index = getlines(text, offsets, lineno, context=5) record = Record(module='<template>', filename=filename, lineno=lineno, lines=lines, index=index) records.append(record) prev_frame = frame if issubclass(exc_type, ParseError): text, offsets = exc_value.source[:2] lines, index = getlines(text, offsets, exc_value.lineno, context=5) record = Record(module='<template>', filename='<?>', lineno=exc_value.lineno, lines=lines, index=index) records.append(record) return html() finally: del tb
def from_frame(frame, context=5): module = frame.f_globals.get('__name__') or '?' filename, lineno, func, lines, index = inspect.getframeinfo(frame, context) if lines is None: lines = [] # if lines is None then index also is None source_encoding = detect_source_encoding(filename) formatted_lines = [ format_line(frame, line.decode(source_encoding, 'replace')) for line in lines ] return Record(module, filename, lineno, formatted_lines, index, func)
def format_exc(info=None, context=5): if info: exc_type, exc_value, tb = info else: exc_type, exc_value, tb = sys.exc_info() try: exc_msg = restore_escapes(exc_value) while tb.tb_next is not None: module = tb.tb_frame.f_globals.get('__name__') or '?' if module == 'pony' or module.startswith('pony.'): tb = tb.tb_next else: break records = [] if issubclass(exc_type, SyntaxError ) and exc_value.filename and exc_value.filename != '<?>': lines, index = getlines2(exc_value.filename, exc_value.lineno, context=5) source_encoding = detect_source_encoding(exc_value.filename) formatted_lines = [] for i, line in enumerate(lines): syntax_error_offset = None if i == index: syntax_error_offset = exc_value.offset - 1 formatted_lines.append( format_line(None, line.decode(source_encoding, 'replace'), syntax_error_offset)) record = Record(module='<?>', filename=exc_value.filename, lineno=exc_value.lineno, lines=formatted_lines, index=index) records = [record] else: frames = inspect.getinnerframes(tb, context) prev_frame = None for frame, filename, lineno, func, lines, index in frames: if index is None: continue module = frame.f_globals.get('__name__') or '?' source_encoding = detect_source_encoding(filename) formatted_lines = [ format_line(frame, line.decode(source_encoding, 'replace')) for line in lines ] record = Record(module=module, filename=filename, lineno=lineno, lines=formatted_lines, index=index, func=func) records.append(record) if module != 'pony.templating': pass elif func in ('_eval', '_compile'): element = prev_frame.f_locals[ 'elem'] # instance of SyntaxElement subclass text, offsets, filename = (element.source + (None, ))[:3] lineno, offset = pos2lineno(element.start, offsets) lines, index = getlines(text, offsets, lineno, context=5) record = Record(module='<template>', filename=filename, lineno=lineno, lines=lines, index=index) records.append(record) prev_frame = frame if issubclass(exc_type, ParseError): text, offsets = exc_value.source[:2] lines, index = getlines(text, offsets, exc_value.lineno, context=5) record = Record(module='<template>', filename='<?>', lineno=exc_value.lineno, lines=lines, index=index) records.append(record) return html() finally: del tb