Skip to content

Commit

Permalink
Merge pull request #3060 from Pylons/push_threadlocals_exception_view
Browse files Browse the repository at this point in the history
When invoking an exception view, push the new threadlocals
  • Loading branch information
mmerickel authored Jun 5, 2017
2 parents 9ef3311 + 4f66355 commit 22215df
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
19 changes: 19 additions & 0 deletions pyramid/tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,8 @@ def exc_view(exc, request):
def test_it_supports_alternate_requests(self):
def exc_view(exc, request):
self.assertTrue(request is other_req)
from pyramid.threadlocal import get_current_request
self.assertTrue(get_current_request() is other_req)
return DummyResponse(b'foo')
self.config.add_view(exc_view, context=RuntimeError)
request = self._makeOne()
Expand All @@ -816,6 +818,23 @@ def exc_view(exc, request):
else: # pragma: no cover
self.fail()

def test_it_raises_if_no_registry(self):
request = self._makeOne()
del request.registry
from pyramid.threadlocal import manager
manager.push({'registry': None, 'request': request})
try:
raise RuntimeError
except RuntimeError:
try:
request.invoke_exception_view()
except RuntimeError as e:
self.assertEqual(e.args[0], "Unable to retrieve registry")
else: # pragma: no cover
self.fail()
finally:
manager.pop()

def test_it_supports_alternate_exc_info(self):
def exc_view(exc, request):
self.assertTrue(request.exc_info is exc_info)
Expand Down
2 changes: 1 addition & 1 deletion pyramid/threadlocal.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def clear(self):
self.stack[:] = []

def defaults():
return {'request':None, 'registry':global_registry}
return {'request': None, 'registry': global_registry}

manager = ThreadLocalManager(default=defaults)

Expand Down
39 changes: 27 additions & 12 deletions pyramid/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
default_exceptionresponse_view,
)

from pyramid.threadlocal import get_current_registry
from pyramid.threadlocal import (
get_current_registry,
manager,
)

from pyramid.util import hide_attrs

_marker = object()
Expand Down Expand Up @@ -675,8 +679,13 @@ def invoke_exception_view(
registry = getattr(request, 'registry', None)
if registry is None:
registry = get_current_registry()

if registry is None:
raise RuntimeError("Unable to retrieve registry")

if exc_info is None:
exc_info = sys.exc_info()

exc = exc_info[1]
attrs = request.__dict__
context_iface = providedBy(exc)
Expand All @@ -690,17 +699,23 @@ def invoke_exception_view(
# we use .get instead of .__getitem__ below due to
# https://github.com/Pylons/pyramid/issues/700
request_iface = attrs.get('request_iface', IRequest)
response = _call_view(
registry,
request,
exc,
context_iface,
'',
view_types=None,
view_classifier=IExceptionViewClassifier,
secure=secure,
request_iface=request_iface.combined,
)

manager.push({'request': request, 'registry': registry})

try:
response = _call_view(
registry,
request,
exc,
context_iface,
'',
view_types=None,
view_classifier=IExceptionViewClassifier,
secure=secure,
request_iface=request_iface.combined,
)
finally:
manager.pop()

if response is None:
raise HTTPNotFound
Expand Down

0 comments on commit 22215df

Please sign in to comment.