Opened 9 years ago

Last modified 8 years ago

#24727 closed Cleanup/optimization

ClearableFileInput masks useful exceptions in is_initial() — at Version 2

Reported by: Antonio García-Domínguez Owned by: nobody
Component: Forms Version: 1.8
Severity: Normal Keywords:
Cc: berker.peksag@… Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Antonio García-Domínguez)

I have a Python 2.7 site with some special requirements on how to link to uploaded files, so I defined a FileSystemStorage subclass with a custom url() method that computed a reverse URL for them.

This morning I noticed that some files would not show up in a FileField with a ClearableFileInput widget after being uploaded through the Django admin app. After searching for a while, I found the issue: the hasattr(value, "url") call in ClearableFileInput.is_initial() was simply returning False for these files, masking an exception from the reverse() call of our custom url() method.

I suggest changing ClearableFileInput.is_initial() so it will not mask useful exceptions. Instead of simply using hasattr, it should use a getattr call inside a try..except block and rethrow anything that is not an AttributeError. In fact, this is the new behavior of hasattr in Python 3.2:

https://docs.python.org/3.2/library/functions.html#hasattr

Change History (2)

comment:1 by Antonio García-Domínguez, 9 years ago

Has patch: set

Here is a pull request with the proposed fix:

https://github.com/django/django/pull/4579

comment:2 by Antonio García-Domínguez, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top