Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:Pylons/pyramid

  • Loading branch information...
commit 1bbe668972a6ac3b116f44e50825c66ae5813326 2 parents b4f1932 + 92b4566
Chris McDonough mcdonc authored
4 CHANGES.txt
View
@@ -54,3 +54,7 @@ Features
result for the view being called. The uri format using an asset spec is
package:path/to/template#defname.mako. The old way of returning a tuple
from the view is supported for backward compatibility, ('defname', {}).
+
+- When there is a predicate mismatch exception (seen when no view matches for
+ a given request due to predicates not working), the exception now contains
+ a textual description of the predicate which didn't match.
7 pyramid/config/util.py
View
@@ -113,6 +113,12 @@ def make_predicates(xhr=None, request_method=None, path_info=None,
# any predicates get an order of MAX_ORDER, meaning that they will
# be tried very last.
+ # NB: each predicate callable constructed by this function (or examined
+ # by this function, in the case of custom predicates) must leave this
+ # function with a ``__text__`` attribute. The subsystem which reports
+ # errors when no predicates match depends upon the existence of this
+ # attribute on each predicate callable.
+
predicates = []
weights = []
h = md5()
@@ -273,6 +279,7 @@ def traverse_predicate(context, request):
tvalue = tgenerate(m) # tvalue will be urlquoted string
m['traverse'] = traversal_path(tvalue) # will be seq of unicode
return True
+ traverse_predicate.__text__ = 'traverse matchdict pseudo-predicate'
# This isn't actually a predicate, it's just a infodict
# modifier that injects ``traverse`` into the matchdict. As a
# result, the ``traverse_predicate`` function above always
12 pyramid/config/views.py
View
@@ -276,11 +276,13 @@ def predicated_view(self, view):
if not predicates:
return view
def predicate_wrapper(context, request):
- if all((predicate(context, request) for predicate in predicates)):
- return view(context, request)
- view_name = getattr(view, '__name__', view)
- raise PredicateMismatch(
- 'predicate mismatch for view %s' % view_name)
+ for predicate in predicates:
+ if not predicate(context, request):
+ view_name = getattr(view, '__name__', view)
+ raise PredicateMismatch(
+ 'predicate mismatch for view %s (%s)' % (
+ view_name, predicate.__text__))
+ return view(context, request)
def checker(context, request):
return all((predicate(context, request) for predicate in
predicates))
31 pyramid/tests/test_config/test_views.py
View
@@ -2905,6 +2905,7 @@ def test_predicate_mismatch_view_has_no_name(self):
view = lambda *arg: response
def predicate1(context, request):
return False
+ predicate1.__text__ = 'text'
deriver = self._makeOne(predicates=[predicate1])
result = deriver(view)
request = self._makeRequest()
@@ -2912,7 +2913,8 @@ def predicate1(context, request):
try:
result(None, None)
except PredicateMismatch as e:
- self.assertEqual(e.detail, 'predicate mismatch for view <lambda>')
+ self.assertEqual(e.detail,
+ 'predicate mismatch for view <lambda> (text)')
else: # pragma: no cover
raise AssertionError
@@ -2921,6 +2923,7 @@ def test_predicate_mismatch_view_has_name(self):
def myview(request): pass
def predicate1(context, request):
return False
+ predicate1.__text__ = 'text'
deriver = self._makeOne(predicates=[predicate1])
result = deriver(myview)
request = self._makeRequest()
@@ -2928,7 +2931,29 @@ def predicate1(context, request):
try:
result(None, None)
except PredicateMismatch as e:
- self.assertEqual(e.detail, 'predicate mismatch for view myview')
+ self.assertEqual(e.detail,
+ 'predicate mismatch for view myview (text)')
+ else: # pragma: no cover
+ raise AssertionError
+
+ def test_predicate_mismatch_exception_has_text_in_detail(self):
+ from pyramid.exceptions import PredicateMismatch
+ def myview(request): pass
+ def predicate1(context, request):
+ return True
+ predicate1.__text__ = 'pred1'
+ def predicate2(context, request):
+ return False
+ predicate2.__text__ = 'pred2'
+ deriver = self._makeOne(predicates=[predicate1, predicate2])
+ result = deriver(myview)
+ request = self._makeRequest()
+ request.method = 'POST'
+ try:
+ result(None, None)
+ except PredicateMismatch as e:
+ self.assertEqual(e.detail,
+ 'predicate mismatch for view myview (pred2)')
else: # pragma: no cover
raise AssertionError
@@ -2974,9 +2999,11 @@ def test_with_predicates_notall(self):
def predicate1(context, request):
predicates.append(True)
return True
+ predicate1.__text__ = 'text'
def predicate2(context, request):
predicates.append(True)
return False
+ predicate2.__text__ = 'text'
deriver = self._makeOne(predicates=[predicate1, predicate2])
result = deriver(view)
request = self._makeRequest()
Please sign in to comment.
Something went wrong with that request. Please try again.