# # This module was written by Ka-Ping Yee, . # # $Id: cgitb.py,v 1.9 2003-12-05 03:36:34 richard Exp $ __doc__ = """ Extended CGI traceback handler by Ka-Ping Yee, . """ import sys, os, types, string, keyword, linecache, tokenize, inspect, cgi import pydoc, traceback from roundup.i18n import _ def breaker(): return ('' + ' > ' + '' * 5) def niceDict(indent, dict): l = [] for k,v in dict.items(): l.append('%s%s'%(k, cgi.escape(repr(v)))) return '\n'.join(l) def pt_html(context=5): esc = cgi.escape l = ['

Templating Error

', '

%s: %s

'%(esc(str(sys.exc_type)), esc(str(sys.exc_value))), '

Debugging information follows

', '
    ',] from roundup.cgi.PageTemplates.Expressions import TraversalError t = inspect.trace(context) t.reverse() for frame, file, lnum, func, lines, index in t: args, varargs, varkw, locals = inspect.getargvalues(frame) if locals.has_key('__traceback_info__'): ti = locals['__traceback_info__'] if isinstance(ti, TraversalError): s = [] for name, info in ti.path: s.append('
  1. "%s" (%s)
  2. '%(name, esc(repr(info)))) s = '\n'.join(s) l.append('
  3. Looking for "%s", current path:
      %s
  4. '%( ti.name, s)) else: l.append('
  5. In %s
  6. '%esc(str(ti))) if locals.has_key('__traceback_supplement__'): ts = locals['__traceback_supplement__'] if len(ts) == 2: supp, context = ts s = 'A problem occurred in your template "%s".'%str(context.id) if context._v_errors: s = s + '
    ' + '
    '.join( [esc(x) for x in context._v_errors]) l.append('
  7. %s
  8. '%s) elif len(ts) == 3: supp, context, info = ts l.append('''
  9. While evaluating the %r expression on line %d %s %s
    Current variables:
  10. '''%(info, context.position[0], niceDict(' ', context.global_vars), niceDict(' ', context.local_vars))) l.append('''
Full traceback:
%s
'''%cgi.escape(''.join(traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)))) l.append('

 

') return '\n'.join(l) def html(context=5): etype, evalue = sys.exc_type, sys.exc_value if type(etype) is types.ClassType: etype = etype.__name__ pyver = 'Python ' + string.split(sys.version)[0] + '
' + sys.executable head = pydoc.html.heading( '%s: %s'%(etype, evalue), '#ffffff', '#777777', pyver) head = head + (_('

A problem occurred while running a Python script. ' 'Here is the sequence of function calls leading up to ' 'the error, with the most recent (innermost) call first. ' 'The exception attributes are:')) indent = '%s ' % (' ' * 5) traceback = [] for frame, file, lnum, func, lines, index in inspect.trace(context): if file is None: link = '''<file is None - probably inside eval or exec>''' else: file = os.path.abspath(file) link = '%s' % (file, pydoc.html.escape(file)) args, varargs, varkw, locals = inspect.getargvalues(frame) if func == '?': call = '' else: call = 'in %s' % func + inspect.formatargvalues( args, varargs, varkw, locals, formatvalue=lambda value: '=' + pydoc.html.repr(value)) level = '''
%s %s
''' % (link, call) if index is None or file is None: traceback.append('

' + level) continue # do a file inspection names = [] def tokeneater(type, token, start, end, line, names=names): if type == tokenize.NAME and token not in keyword.kwlist: if token not in names: names.append(token) if type == tokenize.NEWLINE: raise IndexError def linereader(file=file, lnum=[lnum]): line = linecache.getline(file, lnum[0]) lnum[0] = lnum[0] + 1 return line try: tokenize.tokenize(linereader, tokeneater) except IndexError: pass lvals = [] for name in names: if name in frame.f_code.co_varnames: if locals.has_key(name): value = pydoc.html.repr(locals[name]) else: value = _('undefined') name = '%s' % name else: if frame.f_globals.has_key(name): value = pydoc.html.repr(frame.f_globals[name]) else: value = _('undefined') name = 'global %s' % name lvals.append('%s = %s'%(name, value)) if lvals: lvals = string.join(lvals, ', ') lvals = indent + '%s'\ '
'%lvals else: lvals = '' excerpt = [] i = lnum - index for line in lines: number = ' ' * (5-len(str(i))) + str(i) number = '%s' % number line = '%s %s' % (number, pydoc.html.preformat(line)) if i == lnum: line = '''
%s
''' % line excerpt.append('\n' + line) if i == lnum: excerpt.append(lvals) i = i + 1 traceback.append('

' + level + string.join(excerpt, '\n')) traceback.reverse() exception = '

%s: %s' % (str(etype), str(evalue)) attribs = [] if type(evalue) is types.InstanceType: for name in dir(evalue): value = pydoc.html.repr(getattr(evalue, name)) attribs.append('
%s%s = %s' % (indent, name, value)) return head + string.join(attribs) + string.join(traceback) + '

 

' def handler(): print breaker() print html() # vim: set filetype=python ts=4 sw=4 et si