From f2924f2ac6d08488ce62c1de6bdee9ba00e2cc35 Mon Sep 17 00:00:00 2001 From: Michael Merickel Date: Thu, 23 Jun 2011 12:41:29 -0500 Subject: A fix for classmethod-based custom predicates with no __text__ property. --- pyramid/config.py | 10 +++++++++- pyramid/tests/test_config.py | 15 ++++++++++++++- 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__ = "" + text = '' + 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__, '') 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' -- cgit v1.2.3