Opened 14 years ago
Closed 14 years ago
#16946 closed Bug (duplicate)
core.files.File assumes name attribute on file-like objects
| Reported by: | Paul McMillan | Owned by: | Simon Charette |
|---|---|---|---|
| Component: | Core (Other) | Version: | 1.3 |
| Severity: | Normal | Keywords: | |
| Cc: | charette.s@…, nickname123 | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | yes | Patch needs improvement: | yes |
| Easy pickings: | yes | UI/UX: | no |
Description
The django.core.files.File object is a wrapper to add some django-specific stuff around standard python file-like objects.
https://code.djangoproject.com/browser/django/trunk/django/core/files/base.py#L10
In the __init__ and elsewhere, File works around file-like objects which may not have a name attribute (optional for python file objects). However, down in the _size() getter, it slips up and makes the assumption that the file-like object has a name attribute. This means that it produces a confusing attribute error about name rather than the desired error about being unable to determine a file's size.
This should be patched to check for self.name before doing the os.path.exists, like line 113.
Attachments (2)
Change History (10)
by , 14 years ago
| Attachment: | 16946.diff added |
|---|
comment:1 by , 14 years ago
| Cc: | added |
|---|---|
| Has patch: | set |
comment:2 by , 14 years ago
| Component: | Uncategorized → Core (Other) |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
Looks good, needs a test. (I know, the devil in the details!) urllib.urlopen() is a good example of a file-like object that doesn't have a name that you could use for the test.
comment:3 by , 14 years ago
| Needs tests: | set |
|---|---|
| Patch needs improvement: | set |
comment:4 by , 14 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
I'll have a look at tests in the next few days.
by , 14 years ago
| Attachment: | 16946.with-test.diff added |
|---|
comment:5 by , 14 years ago
Added a new patch with test but it triggers an error in modeltests.files.tests.FileTests.tearDown:
ERROR: test_file_with_no_name_size (modeltests.files.tests.FileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/simon/.virtualenvs/trunk/src/django/tests/modeltests/files/tests.py", line 21, in tearDown
shutil.rmtree(temp_storage_location)
File "/usr/lib/python2.7/shutil.py", line 236, in rmtree
onerror(os.listdir, path, sys.exc_info())
File "/usr/lib/python2.7/shutil.py", line 234, in rmtree
names = os.listdir(path)
OSError: [Errno 2] No such file or directory: '/tmp/tmplXh8th'
Actually adding a new test_ method that doesn't create a file using temp_storage.save to modeltests.files.tests.FileTests.tearDown triggers this error in the tearDown and adding temp_storage.save('tests/default.txt', ContentFile('default content')) in the newly added test_file_with_no_name_size method prevents the tearDown from spitting this error...
Should the shutil.rmtree(temp_storage_location) be wrapped in a try statement to catch OSError?
comment:7 by , 14 years ago
| Cc: | added |
|---|
comment:8 by , 14 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | assigned → closed |
This is indeed a duplicate of #15644
Make sure django File wrapper size getter raises the correct exception when called on a an instance with no name