[Django] #18283: FileField should not reuse FieldFiles

23 views
Skip to first unread message

Django

unread,
May 7, 2012, 1:17:13 PM5/7/12
to django-...@googlegroups.com
#18283: FileField should not reuse FieldFiles
----------------------------------------------+--------------------
Reporter: sayane | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.4
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
A simple example:

{{{
old_image = mymodel.image
old_image.open()
try:
mymodel.image = do_something_with_image(old_image)
finally:
old_image.close()

mymodel.save() # transaction manager is in auto commit mode
old_image.delete()
}}}

'''Expected behavior:'''

New file is commited to disk and database, old file is removed.

'''Actual behavior:'''

New file is commited to disk and database, then this new file is removed
from disk.


That's because FileField (or to be more precise: FileDescriptor class) is
re-using FieldFile objects instead of creating new FieldFile for every new
instance of a file. This leads to unexpected results and data loss (like
in example).

--
Ticket URL: <https://code.djangoproject.com/ticket/18283>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
May 20, 2012, 1:05:28 PM5/20/12
to django-...@googlegroups.com
#18283: FileField should not reuse FieldFiles
-------------------------------------+-------------------------------------
Reporter: sayane | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.4
(models, ORM) | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by jezdez):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/18283#comment:1>

Django

unread,
Nov 7, 2015, 6:47:13 AM11/7/15
to django-...@googlegroups.com
#18283: FileField should not reuse FieldFiles
-------------------------------------+-------------------------------------
Reporter: sayane | Owner: szopu
Type: Bug | Status: assigned

Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by szopu):

* owner: nobody => szopu
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/18283#comment:2>

Django

unread,
Nov 15, 2015, 3:29:29 PM11/15/15
to django-...@googlegroups.com
#18283: FileField should not reuse FieldFiles
-------------------------------------+-------------------------------------
Reporter: sayane | Owner: trg

Type: Bug | Status: assigned
Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by trg):

* owner: szopu => trg


--
Ticket URL: <https://code.djangoproject.com/ticket/18283#comment:3>

Django

unread,
Nov 15, 2015, 5:14:06 PM11/15/15
to django-...@googlegroups.com
#18283: FileField should not reuse FieldFiles
-------------------------------------+-------------------------------------
Reporter: sayane | Owner: trg
Type: Bug | Status: assigned
Component: Database layer | Version: 1.4
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by trg):

I have a real trouble with reproducing this bug. The description of it is
very enigmatic - the submitter didn't mention what
`do_something_with_image()` actually does (for instance, what type of
object it returns).
This bug seems to happen only when one takes the `FieldFile` object from
specified model field and modifies it in place. This is illustrated by
`test_unchanged_fieldfile` in the diff below:

https://github.com/django/django/compare/master...szopu:ticket_18283?expand=1


The only way I see this problem can be fixed, is defensive shallow copying
of the `FieldFile` object in `__get__` and `__set__` (I think that doing
it for `__set__` only is not enough). But this solution has a downside,
for instance, this would generate following semantics:

{{{#!python
model.myfile.name = 'somefile.txt'
print(model.myfile.name) # returns the previous filename instead of
'somefile.txt'
}}}

So my question is: Is it the right way to go?

--
Ticket URL: <https://code.djangoproject.com/ticket/18283#comment:4>

Reply all
Reply to author
Forward
0 new messages