def wsgi(env, start_response): # Load the environment. self._load(env) # Handle the request & Recieve status + output if self.debug: try: code, output = self._handle() except: code = "500 Internal Error" output = debug.debugerror() else: code, output = self._handle() # Get headers. headers = webapi.getheaders() # Send Complete WSGI request start_response(str(code), headers) return [str(output)]
def debugerror(): 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 # hack to get correct line number for templates lineno += tback.tb_frame.f_locals.get("__lineoffset__", 0) pre_context_lineno, pre_context, context_line, post_context = \ _get_lines_from_file(filename, lineno, 7) if '__hidetraceback__' not in tback.tb_frame.f_locals: frames.append({ '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() out = """<div id="summary"> <h1>%(exception_type)s at %(context_path)s</h1> <h2>%(exception_value)s</h2> <table><tr> <th>Python</th> <td>%(frame_file)s in %(frame_func)s line %(frame_line)s</td> </tr><tr> <th>Web</th> <td>%(context_method)s %(context_home)s%(context_path)s</td> </tr></table> </div> <div id="traceback"> <h2>Traceback <span>(innermost first)</span></h2> <ul class="traceback"> """ % { "exception_type": escape(exception_type.__name__), "exception_value": exception_value, "context_home": escape(webapi.context["home"]), "context_path": escape(webapi.context["path"]), "context_method": escape(webapi.context["method"]), "frame_file": escape(frames[0]["filename"]), "frame_func": escape(frames[0]["function"]), "frame_line": frames[0]["lineno"] } for frame in frames: out += """<li class="frame"><code>%s</code> in <code>%s</code>""" % \ (escape(frame["filename"]), escape(frame["function"])) if frame["context_line"] is not None: out += '<div class="context" id="c%s">' % frame["id"] if frame["pre_context"]: out += """<ol start="%s" class="pre-context" id="pre%s">""" % \ (frame["pre_context_lineno"], frame["id"]) for line in frame["pre_context"]: out += """<li onclick="toggle('pre%(frameid)s', 'post%(frameid)s')">%(line)s</li> """ % {"frameid": frame["id"], "line": escape(line)} out += "</ol>" out += """<ol start="%s" class="context-line"> <li onclick="toggle('pre%s', 'post%s')">%s </li></ol> """ % (frame["lineno"], frame["id"], frame["id"], frame["context_line"]) if frame["post_context"]: out += """<ol start="%s" class="post-context" id="post%s">""" % (frame["lineno"] + 1, frame["id"]) for line in frame["post_context"]: out += """<li onclick="toggle('pre%s', 'post%s')">%s</li> """ % (frame["id"], frame["id"], escape(frame["context_line"])) out += "</ol>" out += "</div>" if frame["vars"]: out += """<div class="commands"><a href='#' onclick="return varToggle(this, '%s')"><span>▶</span> Local vars</a></div>""" % frame["id"] out += dicttable(frame["vars"], kls='vars') out += "</li>" out += '</ul></div><div id="requestinfo">' if webapi.context["output"] or webapi.context["headers"]: out += """<h2>Response so far</h2> <h3>HEADERS</h3>""" out += dicttable(dict(webapi.getheaders())) out += '<h3>BODY</h3><p class="req" style="padding-bottom: 2em"><code>' out += str(escape(webapi.context["output"])) + "</code></p>" out += '<h2>Request information</h2>' out += '<h3>INPUT</h3>' out += dicttable(webapi.getall()) out += '<h3 id="cookie-info">COOKIES</h3>' out += dicttable(webapi.cookies()) out += '<h3 id="meta-info">META</h3>' newctx = [(k, v) for (k, v) in webapi.context.iteritems() if not k.startswith('_') and not isinstance(v, dict)] out += dicttable(dict(newctx)) out += '<h3 id="meta-info">ENV</h3>' out += dicttable(webapi.context["environ"]) + '</div>' return error_template_start + out + error_template_end
def debugerror(): 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 # hack to get correct line number for templates lineno += tback.tb_frame.f_locals.get("__lineoffset__", 0) pre_context_lineno, pre_context, context_line, post_context = \ _get_lines_from_file(filename, lineno, 7) if '__hidetraceback__' not in tback.tb_frame.f_locals: frames.append({ '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() out = """<div id="summary"> <h1>%(exception_type)s at %(context_path)s</h1> <h2>%(exception_value)s</h2> <table><tr> <th>Python</th> <td>%(frame_file)s in %(frame_func)s line %(frame_line)s</td> </tr><tr> <th>Web</th> <td>%(context_method)s %(context_home)s%(context_path)s</td> </tr></table> </div> <div id="traceback"> <h2>Traceback <span>(innermost first)</span></h2> <ul class="traceback"> """ % { "exception_type": escape(exception_type.__name__), "exception_value": exception_value, "context_home": escape(webapi.context["home"]), "context_path": escape(webapi.context["path"]), "context_method": escape(webapi.context["method"]), "frame_file": escape(frames[0]["filename"]), "frame_func": escape(frames[0]["function"]), "frame_line": frames[0]["lineno"] } for frame in frames: out += """<li class="frame"><code>%s</code> in <code>%s</code>""" % \ (escape(frame["filename"]), escape(frame["function"])) if frame["context_line"] is not None: out += '<div class="context" id="c%s">' % frame["id"] if frame["pre_context"]: out += """<ol start="%s" class="pre-context" id="pre%s">""" % \ (frame["pre_context_lineno"], frame["id"]) for line in frame["pre_context"]: out += """<li onclick="toggle('pre%(frameid)s', 'post%(frameid)s')">%(line)s</li> """ % { "frameid": frame["id"], "line": escape(line) } out += "</ol>" out += """<ol start="%s" class="context-line"> <li onclick="toggle('pre%s', 'post%s')">%s </li></ol> """ % (frame["lineno"], frame["id"], frame["id"], frame["context_line"]) if frame["post_context"]: out += """<ol start="%s" class="post-context" id="post%s">""" % (frame["lineno"] + 1, frame["id"]) for line in frame["post_context"]: out += """<li onclick="toggle('pre%s', 'post%s')">%s</li> """ % (frame["id"], frame["id"], escape(frame["context_line"])) out += "</ol>" out += "</div>" if frame["vars"]: out += """<div class="commands"><a href='#' onclick="return varToggle(this, '%s')"><span>▶</span> Local vars</a></div>""" % frame["id"] out += dicttable(frame["vars"], kls='vars') out += "</li>" out += '</ul></div><div id="requestinfo">' if webapi.context["output"] or webapi.context["headers"]: out += """<h2>Response so far</h2> <h3>HEADERS</h3>""" out += dicttable(dict(webapi.getheaders())) out += '<h3>BODY</h3><p class="req" style="padding-bottom: 2em"><code>' out += str(escape(webapi.context["output"])) + "</code></p>" out += '<h2>Request information</h2>' out += '<h3>INPUT</h3>' out += dicttable(webapi.getall()) out += '<h3 id="cookie-info">COOKIES</h3>' out += dicttable(webapi.cookies()) out += '<h3 id="meta-info">META</h3>' newctx = [(k, v) for (k, v) in webapi.context.iteritems() if not k.startswith('_') and not isinstance(v, dict)] out += dicttable(dict(newctx)) out += '<h3 id="meta-info">ENV</h3>' out += dicttable(webapi.context["environ"]) + '</div>' return error_template_start + out + error_template_end