Michael Merickel
2011-06-23 f2924f2ac6d08488ce62c1de6bdee9ba00e2cc35
A fix for classmethod-based custom predicates with no __text__ property.
2 files modified
25 ■■■■■ changed files
pyramid/config.py 10 ●●●●● patch | view | raw | blame | history
pyramid/tests/test_config.py 15 ●●●●● patch | view | raw | blame | history
pyramid/config.py
@@ -2692,7 +2692,15 @@
    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
pyramid/tests/test_config.py
@@ -4626,7 +4626,9 @@
            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 @@
        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 @@
    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'