Skip to content

Commit

Permalink
Include cgitb.Hook in paste.util (#95)
Browse files Browse the repository at this point in the history
* Include cgitb.Hook in paste.util

* Add PSF copyright notice
  • Loading branch information
Cito authored Apr 25, 2024
1 parent cf49c16 commit 0742dc4
Show file tree
Hide file tree
Showing 5 changed files with 442 additions and 22 deletions.
4 changes: 1 addition & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ Debugging Filters
* Catch (optionally email) errors with extended tracebacks (using
Zope/ZPT conventions) in ``paste.exceptions``

* Catch errors presenting a `cgitb
<http://docs.python.org/2/library/cgitb.html>`_-based
output, in ``paste.cgitb_catcher``.
* Catch errors presenting traceback in ``paste.cgitb_catcher``.

* Profile each request and append profiling information to the HTML,
in ``paste.debug.profile``
Expand Down
4 changes: 1 addition & 3 deletions docs/developer-features.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ Debugging Filters
frame, including an interactive prompt that runs in the individual
stack frames, in :mod:`paste.evalexception`.

* Catch errors presenting a `cgitb
<http://python.org/doc/current/lib/module-cgitb.html>`_-based
output, in :mod:`paste.cgitb_catcher`.
* Catch errors presenting traceback in ``paste.cgitb_catcher``.

* Profile each request and append profiling information to the HTML,
in :mod:`paste.debug.profile`
Expand Down
34 changes: 18 additions & 16 deletions paste/cgitb_catcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
"""
WSGI middleware
Captures any exceptions and prints a pretty report. See the `cgitb
documentation <http://python.org/doc/current/lib/module-cgitb.html>`_
for more.
Captures any exceptions and prints a pretty report.
"""

import cgitb
from io import StringIO
import sys

from paste.util import converters
from paste.util.cgitb_hook import Hook

class NoDefault(object):
pass

class CgitbMiddleware(object):
class NoDefault:
...


class CgitbMiddleware:

def __init__(self, app,
global_conf=None,
Expand All @@ -42,7 +42,7 @@ def __call__(self, environ, start_response):
try:
app_iter = self.app(environ, start_response)
return self.catching_iter(app_iter, environ)
except:
except Exception:
exc_info = sys.exc_info()
start_response('500 Internal Server Error',
[('content-type', 'text/html')],
Expand All @@ -61,7 +61,7 @@ def catching_iter(self, app_iter, environ):
if hasattr(app_iter, 'close'):
error_on_close = True
app_iter.close()
except:
except Exception:
response = self.exception_handler(sys.exc_info(), environ)
if not error_on_close and hasattr(app_iter, 'close'):
try:
Expand All @@ -77,22 +77,24 @@ def catching_iter(self, app_iter, environ):

def exception_handler(self, exc_info, environ):
dummy_file = StringIO()
hook = cgitb.Hook(file=dummy_file,
display=self.display,
logdir=self.logdir,
context=self.context,
format=self.format)
hook = Hook(
file=dummy_file,
display=self.display,
logdir=self.logdir,
context=self.context,
format=self.format)
hook(*exc_info)
return dummy_file.getvalue()


def make_cgitb_middleware(app, global_conf,
display=NoDefault,
logdir=None,
context=5,
format='html'):
"""
Wraps the application in the ``cgitb`` (standard library)
error catcher.
Wraps the application in an error catcher based on the
former ``cgitb`` module in the standard library.
display:
If true (or debug is set in the global configuration)
Expand Down
Loading

0 comments on commit 0742dc4

Please sign in to comment.