summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2011-06-23 12:41:29 -0500
committerMichael Merickel <michael@merickel.org>2011-06-23 12:41:29 -0500
commitf2924f2ac6d08488ce62c1de6bdee9ba00e2cc35 (patch)
treef9ccac4c1d05fe8612aea2db352d4419f61cde20
parentcc85e7a96ccbb1671514adb1a1b1992fd1f02461 (diff)
downloadpyramid-f2924f2ac6d08488ce62c1de6bdee9ba00e2cc35.tar.gz
pyramid-f2924f2ac6d08488ce62c1de6bdee9ba00e2cc35.tar.bz2
pyramid-f2924f2ac6d08488ce62c1de6bdee9ba00e2cc35.zip
A fix for classmethod-based custom predicates with no __text__ property.
-rw-r--r--pyramid/config.py10
-rw-r--r--pyramid/tests/test_config.py15
2 files changed, 23 insertions, 2 deletions
diff --git a/pyramid/config.py b/pyramid/config.py
index 77f5b5b34..418e393f4 100644
--- a/pyramid/config.py
+++ b/pyramid/config.py
@@ -2692,7 +2692,15 @@ def _make_predicates(xhr=None, request_method=None, path_info=None,
if custom:
for num, predicate in enumerate(custom):
if getattr(predicate, '__text__', None) is None:
- predicate.__text__ = "<unknown custom predicate>"
+ text = '<unknown custom predicate>'
+ try:
+ predicate.__text__ = text
+ except AttributeError:
+ # if this happens the predicate is probably a classmethod
+ if hasattr(predicate, '__func__'):
+ predicate.__func__.__text__ = text
+ else: # 2.5 doesn't have __func__
+ predicate.im_func.__text__ = text
predicates.append(predicate)
# using hash() here rather than id() is intentional: we
# want to allow custom predicates that are part of
diff --git a/pyramid/tests/test_config.py b/pyramid/tests/test_config.py
index dd8bf109f..5c102132c 100644
--- a/pyramid/tests/test_config.py
+++ b/pyramid/tests/test_config.py
@@ -4626,7 +4626,9 @@ class Test__make_predicates(unittest.TestCase):
accept='accept',
containment='containment',
request_type='request_type',
- custom=(DummyCustomPredicate(),))
+ custom=(DummyCustomPredicate(),
+ DummyCustomPredicate.classmethod_predicate,
+ DummyCustomPredicate.classmethod_predicate_no_text))
self.assertEqual(predicates[0].__text__, 'xhr = True')
self.assertEqual(predicates[1].__text__,
'request method = request_method')
@@ -4637,6 +4639,8 @@ class Test__make_predicates(unittest.TestCase):
self.assertEqual(predicates[6].__text__, 'containment = containment')
self.assertEqual(predicates[7].__text__, 'request_type = request_type')
self.assertEqual(predicates[8].__text__, 'custom predicate')
+ self.assertEqual(predicates[9].__text__, 'classmethod predicate')
+ self.assertEqual(predicates[10].__text__, '<unknown custom predicate>')
class TestMultiView(unittest.TestCase):
def _getTargetClass(self):
@@ -5214,6 +5218,15 @@ class DummyCustomPredicate(object):
def __init__(self):
self.__text__ = 'custom predicate'
+ def classmethod_predicate(*args):
+ pass
+ classmethod_predicate.__text__ = 'classmethod predicate'
+ classmethod_predicate = classmethod(classmethod_predicate)
+
+ @classmethod
+ def classmethod_predicate_no_text(*args):
+ pass
+
def dummy_view(request):
return 'OK'