diff --git a/pecan/commands/shell.py b/pecan/commands/shell.py index 3424103..000b176 100644 --- a/pecan/commands/shell.py +++ b/pecan/commands/shell.py @@ -34,8 +34,9 @@ class IPythonShell(object): shell = InteractiveShellEmbed(banner2=banner) shell(local_ns=ns) except ImportError: + # Support for the IPython <= 0.10 shell API from IPython.Shell import IPShellEmbed - shell = IPShellEmbed(argv=self.args) + shell = IPShellEmbed(argv=[]) shell.set_banner(shell.IP.BANNER + '\n\n' + banner) shell(local_ns=ns, global_ns={}) @@ -48,7 +49,7 @@ class ShellCommand(BaseCommand): SHELLS = { 'python': NativePythonShell, 'ipython': IPythonShell - } + } arguments = BaseCommand.arguments + ({ 'command': ['--shell', '-s'], diff --git a/pecan/middleware/errordocument.py b/pecan/middleware/errordocument.py index 8961968..0c374ae 100644 --- a/pecan/middleware/errordocument.py +++ b/pecan/middleware/errordocument.py @@ -1,6 +1,40 @@ +import sys from recursive import ForwardRequestException +class StatusPersist(object): + + def __init__(self, app, status, url): + self.app = app + self.status = status + self.url = url + + def __call__(self, environ, start_response): + def keep_status_start_response(status, headers, exc_info=None): + return start_response(self.status, headers, exc_info) + parts = self.url.split('?') + environ['PATH_INFO'] = parts[0] + if len(parts) > 1: + environ['QUERY_STRING'] = parts[1] + else: + environ['QUERY_STRING'] = '' + + try: + return self.app(environ, keep_status_start_response) + except RecursionLoop, e: + environ['wsgi.errors'].write( + 'Recursion error getting error page: %s\n' % e + ) + keep_status_start_response( + '500 Server Error', + [('Content-type', 'text/plain')], + sys.exc_info() + ) + return [ + 'Error: %s. (Error page could not be fetched)' % self.status + ] + + class ErrorDocumentMiddleware(object): def __init__(self, app, error_map): @@ -12,14 +46,20 @@ class ErrorDocumentMiddleware(object): def replacement_start_response(status, headers, exc_info=None): try: status_code = status.split(' ')[0] - except ValueError, TypeError: + except (ValueError, TypeError): raise Exception(( 'ErrorDocumentMiddleware received an invalid ' 'status %s' % status )) if status_code in self.error_map: - raise ForwardRequestException(self.error_map[status_code]) + def factory(app): + return StatusPersist( + app, + status, + self.error_map[status_code] + ) + raise ForwardRequestException(factory=factory) return start_response(status, headers, exc_info) diff --git a/pecan/tests/test_base.py b/pecan/tests/test_base.py index 54ffd27..d02f57f 100644 --- a/pecan/tests/test_base.py +++ b/pecan/tests/test_base.py @@ -4,7 +4,7 @@ from pecan.middleware.logger import TransLogger from webtest import TestApp if sys.version_info < (2, 7): - import unittest2 as unittest # pragma: nocover + import unittest2 as unittest # pragma: nocover else: import unittest # pragma: nocover diff --git a/pecan/tests/test_hooks.py b/pecan/tests/test_hooks.py index a7d7025..458e40b 100644 --- a/pecan/tests/test_hooks.py +++ b/pecan/tests/test_hooks.py @@ -1,5 +1,5 @@ from cStringIO import StringIO -from pecan import make_app, expose, request, redirect, abort +from pecan import make_app, expose, redirect, abort from pecan.core import state from pecan.hooks import ( PecanHook, TransactionHook, HookController, RequestViewerHook diff --git a/pecan/util.py b/pecan/util.py index 697dec8..aa2e683 100644 --- a/pecan/util.py +++ b/pecan/util.py @@ -1,5 +1,4 @@ import sys -import os def iscontroller(obj):