Opened 10 years ago
Closed 9 years ago
#24727 closed Cleanup/optimization (fixed)
ClearableFileInput masks useful exceptions in is_initial()
Reported by: | Antonio García-Domínguez | Owned by: | Berker Peksag |
---|---|---|---|
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 )
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:
Change History (11)
comment:1 by , 10 years ago
Has patch: | set |
---|
comment:2 by , 10 years ago
Description: | modified (diff) |
---|
comment:3 by , 10 years ago
Needs tests: | set |
---|---|
Patch needs improvement: | set |
Triage Stage: | Unreviewed → Accepted |
comment:4 by , 10 years ago
Needs tests: | unset |
---|
comment:5 by , 10 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
comment:8 by , 9 years ago
Easy pickings: | unset |
---|---|
Has patch: | unset |
Resolution: | fixed |
Status: | closed → new |
Triage Stage: | Ready for checkin → Accepted |
Type: | Bug → Cleanup/optimization |
Sorry, we had to revert this as noted above because it caused a regression.
It looks like we might have to address #13327 before we can add this back (or find a patch that works differently).
comment:9 by , 9 years ago
Cc: | added |
---|---|
Has patch: | set |
Owner: | changed from | to
Status: | new → assigned |
Pull request: https://github.com/django/django/pull/6143
Actually, there is no reason to use hasattr()
at all. hasattr()
also calls PyObject_GetAttr
(like getattr()
) in its implementation: https://github.com/python/cpython/blob/d69ba698ea79bab7e6d14732d8bf6215b7b93f8b/Python/bltinmodule.c#L1061 and there is no significant speed difference between hasattr()
and getattr()
.
I've also added a test for defining url as property. Previously, url was defined as a class attribute in FakeFieldFile()
.
comment:10 by , 9 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
Here is a pull request with the proposed fix:
https://github.com/django/django/pull/4579