Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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

Merged
merged 1 commit into from

2 participants

@mmerickel
Owner

Basically Pyramid would raise an AttributeError in the Configurator when attempting to set __text__ on a custom predicate that was actually a classmethod.

@mcdonc mcdonc merged commit eacb40c into Pylons:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 2 deletions.
  1. +9 −1 pyramid/config.py
  2. +14 −1 pyramid/tests/test_config.py
View
10 pyramid/config.py
@@ -2692,7 +2692,15 @@ def traverse_predicate(context, request):
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
View
15 pyramid/tests/test_config.py
@@ -4626,7 +4626,9 @@ def test_predicate_text_is_correct(self):
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 @@ def test_predicate_text_is_correct(self):
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'
Something went wrong with that request. Please try again.