Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

A fix for classmethod-based custom predicates with no __text__ property.

  • Loading branch information...
commit f2924f2ac6d08488ce62c1de6bdee9ba00e2cc35 1 parent cc85e7a
Michael Merickel authored

Showing 2 changed files with 23 additions and 2 deletions. Show diff stats Hide diff stats

  1. +9 1 pyramid/config.py
  2. +14 1 pyramid/tests/test_config.py
10 pyramid/config.py
@@ -2692,7 +2692,15 @@ def traverse_predicate(context, request):
2692 2692 if custom:
2693 2693 for num, predicate in enumerate(custom):
2694 2694 if getattr(predicate, '__text__', None) is None:
2695   - predicate.__text__ = "<unknown custom predicate>"
  2695 + text = '<unknown custom predicate>'
  2696 + try:
  2697 + predicate.__text__ = text
  2698 + except AttributeError:
  2699 + # if this happens the predicate is probably a classmethod
  2700 + if hasattr(predicate, '__func__'):
  2701 + predicate.__func__.__text__ = text
  2702 + else: # 2.5 doesn't have __func__
  2703 + predicate.im_func.__text__ = text
2696 2704 predicates.append(predicate)
2697 2705 # using hash() here rather than id() is intentional: we
2698 2706 # want to allow custom predicates that are part of
15 pyramid/tests/test_config.py
@@ -4626,7 +4626,9 @@ def test_predicate_text_is_correct(self):
4626 4626 accept='accept',
4627 4627 containment='containment',
4628 4628 request_type='request_type',
4629   - custom=(DummyCustomPredicate(),))
  4629 + custom=(DummyCustomPredicate(),
  4630 + DummyCustomPredicate.classmethod_predicate,
  4631 + DummyCustomPredicate.classmethod_predicate_no_text))
4630 4632 self.assertEqual(predicates[0].__text__, 'xhr = True')
4631 4633 self.assertEqual(predicates[1].__text__,
4632 4634 'request method = request_method')
@@ -4637,6 +4639,8 @@ def test_predicate_text_is_correct(self):
4637 4639 self.assertEqual(predicates[6].__text__, 'containment = containment')
4638 4640 self.assertEqual(predicates[7].__text__, 'request_type = request_type')
4639 4641 self.assertEqual(predicates[8].__text__, 'custom predicate')
  4642 + self.assertEqual(predicates[9].__text__, 'classmethod predicate')
  4643 + self.assertEqual(predicates[10].__text__, '<unknown custom predicate>')
4640 4644
4641 4645 class TestMultiView(unittest.TestCase):
4642 4646 def _getTargetClass(self):
@@ -5214,6 +5218,15 @@ class DummyCustomPredicate(object):
5214 5218 def __init__(self):
5215 5219 self.__text__ = 'custom predicate'
5216 5220
  5221 + def classmethod_predicate(*args):
  5222 + pass
  5223 + classmethod_predicate.__text__ = 'classmethod predicate'
  5224 + classmethod_predicate = classmethod(classmethod_predicate)
  5225 +
  5226 + @classmethod
  5227 + def classmethod_predicate_no_text(*args):
  5228 + pass
  5229 +
5217 5230 def dummy_view(request):
5218 5231 return 'OK'
5219 5232

0 comments on commit f2924f2

Please sign in to comment.
Something went wrong with that request. Please try again.