Permalink
Browse files

merge exc_info feature from 1.0 branch

  • Loading branch information...
1 parent 47a6bba commit 72e780f53baf40dc4312bf46feed61b8c4be959c @rockyburt rockyburt committed Aug 12, 2011
Showing with 38 additions and 3 deletions.
  1. +11 −0 CHANGES.txt
  2. +17 −1 pyramid/router.py
  3. +10 −2 pyramid/tests/test_router.py
View
11 CHANGES.txt
@@ -1,3 +1,14 @@
+1.1.x (unreleased)
+==================
+
+Additions
+---------
+
+- Backport from 1.2.x: A new attribute is available on request objects:
+ ``exc_info``. Its value will be ``None`` until an exception is caught
+ by the Pyramid router, after which it will be the result of
+ ``sys.exc_info()``.
+
1.1 (2011-07-22)
================
View
18 pyramid/router.py
@@ -1,3 +1,4 @@
+import sys
from zope.interface import implements
from zope.interface import providedBy
@@ -169,6 +170,7 @@ def __call__(self, environ, start_response):
del attrs['response']
attrs['exception'] = why
+ attrs['exc_info'] = sys.exc_info()
for_ = (IExceptionViewClassifier,
request_iface.combined,
@@ -178,7 +180,21 @@ def __call__(self, environ, start_response):
if view_callable is None:
raise
- response = view_callable(why, request)
+ try:
+ msg = why[0]
+ except:
+ msg = ''
+
+ # repoze.bfg.message docs-deprecated in Pyramid 1.0
+ environ['repoze.bfg.message'] = msg
+
+ try:
+ response = view_callable(why, request)
+ finally:
+ if 'exc_info' in attrs:
+ del attrs['exc_info']
+ if 'exception' in attrs:
+ del attrs['exception']
has_listeners and notify(NewResponse(request, response))
View
12 pyramid/tests/test_router.py
@@ -769,7 +769,14 @@ def test_call_view_raises_exception_view(self):
exception_response = DummyResponse()
exception_response.app_iter = ["Hello, world"]
view = DummyView(response, raise_exception=RuntimeError)
- exception_view = DummyView(exception_response)
+
+ class SimpleView(DummyView):
+ def __call__(self, context, request):
+ self.last_exception = getattr(request, 'exception', None)
+ self.last_exc_info = getattr(request, 'exc_info', None)
+ return DummyView.__call__(self, context, request)
+
+ exception_view = SimpleView(exception_response)
environ = self._makeEnviron()
self._registerView(view, '', IViewClassifier, IRequest, None)
self._registerView(exception_view, '', IExceptionViewClassifier,
@@ -778,7 +785,8 @@ def test_call_view_raises_exception_view(self):
start_response = DummyStartResponse()
result = router(environ, start_response)
self.assertEqual(result, ["Hello, world"])
- self.assertEqual(view.request.exception.__class__, RuntimeError)
+ self.assertEqual(exception_view.last_exception.__class__, RuntimeError)
+ self.assertTrue(exception_view.last_exc_info is not None)
def test_call_view_raises_super_exception_sub_exception_view(self):
from pyramid.interfaces import IViewClassifier

0 comments on commit 72e780f

Please sign in to comment.