From 6a5a12ea3e3193b3658b9237b86d98bc37d668d7 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 7 Sep 2012 14:37:21 -0400 Subject: [PATCH] Fixed #17888 -- no longer silence exceptions inside of check_test. Thanks to brutasse for the patch. --- django/forms/widgets.py | 8 ++------ docs/ref/forms/widgets.txt | 4 ++++ tests/regressiontests/forms/tests/widgets.py | 10 +++------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/django/forms/widgets.py b/django/forms/widgets.py index fdb9f50688..7651efccd0 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -507,11 +507,7 @@ class CheckboxInput(Widget): def render(self, name, value, attrs=None): final_attrs = self.build_attrs(attrs, type='checkbox', name=name) - try: - result = self.check_test(value) - except: # Silently catch exceptions - result = False - if result: + if self.check_test(value): final_attrs['checked'] = 'checked' if not (value is True or value is False or value is None or value == ''): # Only add the 'value' attribute if a value is non-empty. @@ -525,7 +521,7 @@ class CheckboxInput(Widget): return False value = data.get(name) # Translate true and false strings to boolean values. - values = {'true': True, 'false': False} + values = {'true': True, 'false': False} if isinstance(value, six.string_types): value = values.get(value.lower(), value) return value diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index fb7657349a..eab314a4cd 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -310,6 +310,10 @@ commonly used groups of widgets: A callable that takes the value of the CheckBoxInput and returns ``True`` if the checkbox should be checked for that value. + .. versionchanged:: 1.5 + Exceptions from ``check_test`` used to be silenced by its caller, + this is no longer the case, they will propagate upwards. + ``Select`` ~~~~~~~~~~ diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py index c950aef719..544ca41642 100644 --- a/tests/regressiontests/forms/tests/widgets.py +++ b/tests/regressiontests/forms/tests/widgets.py @@ -216,13 +216,9 @@ class FormsWidgetTestCase(TestCase): self.assertHTMLEqual(w.render('greeting', 'hello there'), '') self.assertHTMLEqual(w.render('greeting', 'hello & goodbye'), '') - # A subtlety: If the 'check_test' argument cannot handle a value and raises any - # exception during its __call__, then the exception will be swallowed and the box - # will not be checked. In this example, the 'check_test' assumes the value has a - # startswith() method, which fails for the values True, False and None. - self.assertHTMLEqual(w.render('greeting', True), '') - self.assertHTMLEqual(w.render('greeting', False), '') - self.assertHTMLEqual(w.render('greeting', None), '') + # Ticket #17888: calling check_test shouldn't swallow exceptions + with self.assertRaises(AttributeError): + w.render('greeting', True) # The CheckboxInput widget will return False if the key is not found in the data # dictionary (because HTML form submission doesn't send any result for unchecked