From 9a91cb5695e1ca184efae7998a5deb92bf9c2e15 Mon Sep 17 00:00:00 2001 From: Jonathan LaCour Date: Tue, 20 Mar 2012 09:47:57 -0700 Subject: [PATCH] Adding the ability to re-process a failed request with a pdb breakpoint in the debug middleware. --- pecan/middleware/debug.py | 76 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/pecan/middleware/debug.py b/pecan/middleware/debug.py index 01e4fd0..61e2783 100644 --- a/pecan/middleware/debug.py +++ b/pecan/middleware/debug.py @@ -6,6 +6,8 @@ from mako.template import Template from webob import Response +import pdb + debug_template_raw = ''' Pecan - Application Error @@ -119,6 +121,12 @@ debug_template_raw = ''' color: #C70 !important; } + #debug { + background: #FDF6E3; + padding: 10px !important; + margin-top: 10px; + font-family: monospace; + } + + @@ -151,6 +191,15 @@ debug_template_raw = '''
${traceback}
+ % if not debugging: + Want to debug this request? +
+ You can with a Python debugger breakpoint. +
+ % endif +

WSGI Environment

${environment}
@@ -161,6 +210,18 @@ debug_template_raw = ''' ''' debug_template = Template(debug_template_raw) +__debug_environ__ = None + + +class PdbMiddleware(object): + def __init__(self, app): + self.app = app + + def __call__(self, environ, start_response): + try: + return self.app(environ, start_response) + except: + pdb.post_mortem() class DebugMiddleware(object): @@ -171,9 +232,21 @@ class DebugMiddleware(object): assert not environ['wsgi.multiprocess'], ( "The DebugMiddleware middleware is not usable in a " "multi-process environment") + + # initiate a PDB session if requested + global __debug_environ__ + debugging = environ['PATH_INFO'] == '/__pecan_initiate_pdb__' + if debugging: + PdbMiddleware(self.app)(__debug_environ__, start_response) + environ = __debug_environ__ + try: return self.app(environ, start_response) except: + # save the environ for debugging + if not debugging: + __debug_environ__ = environ + # get a formatted exception out = StringIO() print_exc(file=out) @@ -189,7 +262,8 @@ class DebugMiddleware(object): shcore=shcore, shbrushpython=shbrushpython, shcorecss=shcorecss, - shthemedefault=shthemedefault + shthemedefault=shthemedefault, + debugging=debugging ) # construct and return our response