""" pretty debug errors (part of web.py) adapted from Django Copyright (c) 2005, the Lawrence Journal-World Used under the modified BSD license: http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5 """ __all__ = ["debugerror", "djangoerror"] import sys, urlparse, pprint from net import websafe from template import Template import webapi as web import os, os.path whereami = os.path.join(os.getcwd(), __file__) whereami = os.path.sep.join(whereami.split(os.path.sep)[:-1]) djangoerror_t = """\ $def with (exception_type, exception_value, frames) $exception_type at $ctx.path

$exception_type at $ctx.path

$exception_value

Python $frames[0].filename in $frames[0].function, line $frames[0].lineno
Web $ctx.method $ctx.home$ctx.path

Traceback (innermost first)

$if ctx.output or ctx.headers:

Response so far

HEADERS

$for kv in ctx.headers: $kv[0]: $kv[1]
$else: [no headers]

BODY

$ctx.output

Request information

INPUT

$:dicttable(web.input()) $:dicttable(web.cookies())

META

$ newctx = [] $# ) and (k not in ['env', 'output', 'headers', 'environ', 'status', 'db_execute']): $for k, v in ctx.iteritems(): $if not k.startswith('_') and (k in x): $newctx.append(kv) $:dicttable(dict(newctx))

ENVIRONMENT

$:dicttable(ctx.env)

You're seeing this error because you have web.internalerror set to web.debugerror. Change that if you want a different one.

""" dicttable_t = r"""$def with (d, kls='req', id=None) $if d: $ temp = d.items() $temp.sort() $for kv in temp:
VariableValue
$kv[0]
$prettify(kv[1])
$else:

No data.

""" dicttable_r = Template(dicttable_t, filter=websafe) djangoerror_r = Template(djangoerror_t, filter=websafe) def djangoerror(): def _get_lines_from_file(filename, lineno, context_lines): """ Returns context_lines before and after lineno from file. Returns (pre_context_lineno, pre_context, context_line, post_context). """ try: source = open(filename).readlines() lower_bound = max(0, lineno - context_lines) upper_bound = lineno + context_lines pre_context = \ [line.strip('\n') for line in source[lower_bound:lineno]] context_line = source[lineno].strip('\n') post_context = \ [line.strip('\n') for line in source[lineno + 1:upper_bound]] return lower_bound, pre_context, context_line, post_context except (OSError, IOError): return None, [], None, [] exception_type, exception_value, tback = sys.exc_info() frames = [] while tback is not None: filename = tback.tb_frame.f_code.co_filename function = tback.tb_frame.f_code.co_name lineno = tback.tb_lineno - 1 pre_context_lineno, pre_context, context_line, post_context = \ _get_lines_from_file(filename, lineno, 7) frames.append(web.storage({ 'tback': tback, 'filename': filename, 'function': function, 'lineno': lineno, 'vars': tback.tb_frame.f_locals, 'id': id(tback), 'pre_context': pre_context, 'context_line': context_line, 'post_context': post_context, 'pre_context_lineno': pre_context_lineno, })) tback = tback.tb_next frames.reverse() urljoin = urlparse.urljoin def prettify(x): try: out = pprint.pformat(x) except Exception, e: out = '[could not display: <' + e.__class__.__name__ + \ ': '+str(e)+'>]' return out dt = dicttable_r dt.globals = {'prettify': prettify} t = djangoerror_r t.globals = {'ctx': web.ctx, 'web':web, 'dicttable':dt, 'dict':dict, 'str':str} return t(exception_type, exception_value, frames) def debugerror(): """ A replacement for `internalerror` that presents a nice page with lots of debug information for the programmer. (Based on the beautiful 500 page from [Django](http://djangoproject.com/), designed by [Wilson Miner](http://wilsonminer.com/).) """ web.ctx.headers = [('Content-Type', 'text/html')] web.ctx.output = djangoerror() if __name__ == "__main__": urls = ( '/', 'index' ) class index: def GET(self): thisdoesnotexist web.internalerror = web.debugerror web.run(urls)