From 267dbd261862c3d1e06ec49aad36e4bbae384eca Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 11 Nov 2012 12:35:16 -0500 Subject: - Be more tolerant of potential error conditions in ``match_param`` and ``physical_path`` predicate implementations; instead of raising an exception, return False. --- CHANGES.txt | 4 ++++ pyramid/config/predicates.py | 9 ++++++++- pyramid/tests/test_config/test_predicates.py | 12 ++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index 5175baa5a..b0bbd32a2 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -52,6 +52,10 @@ Bug Fixes attribute of the request. It no longer fails in this case. See https://github.com/Pylons/pyramid/issues/700 +- Be more tolerant of potential error conditions in ``match_param`` and + ``physical_path`` predicate implementations; instead of raising an exception, + return False. + Deprecations ------------ diff --git a/pyramid/config/predicates.py b/pyramid/config/predicates.py index e31425899..ded8fbfbf 100644 --- a/pyramid/config/predicates.py +++ b/pyramid/config/predicates.py @@ -17,6 +17,8 @@ from pyramid.security import effective_principals from .util import as_sorted_tuple +_marker = object() + class XHRPredicate(object): def __init__(self, val, config): self.val = bool(val) @@ -174,6 +176,9 @@ class MatchParamPredicate(object): phash = text def __call__(self, context, request): + if not request.matchdict: + # might be None + return False for k, v in self.reqs: if request.matchdict.get(k) != v: return False @@ -266,7 +271,9 @@ class PhysicalPathPredicate(object): phash = text def __call__(self, context, request): - return resource_path_tuple(context) == self.val + if getattr(context, '__name__', _marker) is not _marker: + return resource_path_tuple(context) == self.val + return False class EffectivePrincipalsPredicate(object): def __init__(self, val, config): diff --git a/pyramid/tests/test_config/test_predicates.py b/pyramid/tests/test_config/test_predicates.py index 91dfb0fb6..1cd6050bf 100644 --- a/pyramid/tests/test_config/test_predicates.py +++ b/pyramid/tests/test_config/test_predicates.py @@ -187,6 +187,13 @@ class TestMatchParamPredicate(unittest.TestCase): result = inst(None, request) self.assertFalse(result) + def test___call___matchdict_is_None(self): + inst = self._makeOne('abc=1') + request = Dummy() + request.matchdict = None + result = inst(None, request) + self.assertFalse(result) + def test_text(self): inst = self._makeOne(('def= 1', 'abc =2')) self.assertEqual(inst.text(), 'match_param abc=2,def=1') @@ -436,6 +443,11 @@ class Test_PhysicalPathPredicate(unittest.TestCase): context.__parent__ = root self.assertFalse(inst(context, None)) + def test_it_call_context_has_no_name(self): + inst = self._makeOne('/', None) + context = Dummy() + self.assertFalse(inst(context, None)) + class Test_EffectivePrincipalsPredicate(unittest.TestCase): def setUp(self): self.config = testing.setUp() -- cgit v1.2.3