Skip to content

Commit

Permalink
A fix for classmethod-based custom predicates with no __text__ property.
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Jun 23, 2011
1 parent cc85e7a commit f2924f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
10 changes: 9 additions & 1 deletion pyramid/config.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2692,7 +2692,15 @@ def traverse_predicate(context, request):
if custom: if custom:
for num, predicate in enumerate(custom): for num, predicate in enumerate(custom):
if getattr(predicate, '__text__', None) is None: 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) predicates.append(predicate)
# using hash() here rather than id() is intentional: we # using hash() here rather than id() is intentional: we
# want to allow custom predicates that are part of # want to allow custom predicates that are part of
Expand Down
15 changes: 14 additions & 1 deletion pyramid/tests/test_config.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4626,7 +4626,9 @@ def test_predicate_text_is_correct(self):
accept='accept', accept='accept',
containment='containment', containment='containment',
request_type='request_type', 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[0].__text__, 'xhr = True')
self.assertEqual(predicates[1].__text__, self.assertEqual(predicates[1].__text__,
'request method = request_method') 'request method = request_method')
Expand All @@ -4637,6 +4639,8 @@ def test_predicate_text_is_correct(self):
self.assertEqual(predicates[6].__text__, 'containment = containment') self.assertEqual(predicates[6].__text__, 'containment = containment')
self.assertEqual(predicates[7].__text__, 'request_type = request_type') self.assertEqual(predicates[7].__text__, 'request_type = request_type')
self.assertEqual(predicates[8].__text__, 'custom predicate') 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): class TestMultiView(unittest.TestCase):
def _getTargetClass(self): def _getTargetClass(self):
Expand Down Expand Up @@ -5214,6 +5218,15 @@ class DummyCustomPredicate(object):
def __init__(self): def __init__(self):
self.__text__ = 'custom predicate' 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): def dummy_view(request):
return 'OK' return 'OK'


Expand Down

0 comments on commit f2924f2

Please sign in to comment.