Kaydet (Commit) 5c412dd8 authored tarafından Antonio Garcia-Dominguez's avatar Antonio Garcia-Dominguez Kaydeden (comit) Tim Graham

Fixes #24727 -- Prevented ClearableFileInput from masking exceptions on Python 2

üst 81f76517
......@@ -377,6 +377,14 @@ class ClearableFileInput(FileInput):
"""
Return whether value is considered to be initial value.
"""
# hasattr() masks exceptions on Python 2.
if six.PY2:
try:
getattr(value, 'url')
except AttributeError:
return False
else:
return bool(value)
return bool(value and hasattr(value, 'url'))
def get_template_substitution_values(self, value):
......
......@@ -1345,6 +1345,25 @@ class ClearableFileInputTests(TestCase):
self.assertIn('my<div>file', output)
self.assertNotIn('my<div>file', output)
def test_html_does_not_mask_exceptions(self):
"""
A ClearableFileInput should not mask exceptions produced while
checking that it has a value.
"""
@python_2_unicode_compatible
class FailingURLFieldFile(object):
@property
def url(self):
raise RuntimeError('Canary')
def __str__(self):
return 'value'
widget = ClearableFileInput()
field = FailingURLFieldFile()
with self.assertRaisesMessage(RuntimeError, 'Canary'):
widget.render('myfile', field)
def test_clear_input_renders_only_if_not_required(self):
"""
A ClearableFileInput with is_required=False does not render a clear
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment