#29510: QueryDict.copy() returns closed files when the type of file is
TemporaryUploadedFile
-------------------------------------+-------------------------------------
     Reporter:  Liquid Scorpio       |                    Owner:  Dan
                                     |  Madere
         Type:  Bug                  |                   Status:  assigned
    Component:  File                 |                  Version:  1.11
  uploads/storage                    |
     Severity:  Normal               |               Resolution:
     Keywords:  QueryDict, upload,   |             Triage Stage:  Accepted
  file                               |
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Dan Madere):

 Replying to [comment:10 Paulo]:
 > Replying to [comment:9 Claude Paroz]:
 > > Did you explore possible handling of such file objects in
 `QueryDict.__deepcopy__`?
 >
 > To keep the file on copy?
 > If so then yes, we can check value for UploadedFile and create a new
 instance that points to the same underlying file (although I guess that's
 more shallow than deep copy).
 > To have fully deep copy we'd have to create new tmp if is on disk or
 somehow copy the file stream.

 After trying it, I don't think either of these suggestions is workable,
 considering the nature of `TemporaryUploadedFile`. It has logic where the
 file is deleted as soon as it's closed. When copying/streaming the old
 file's content to the new one, we have a problem where the new file
 deletes itself immediately.

 I stepped back, and pondered why are people copying a `QueryDict` anyway,
 and what do they expect to happen to a `TemporaryUploadedFile` inside?
 [[https://docs.djangoproject.com/en/dev/ref/request-response/#querydict-
 objects]]:

 {{{
 class QueryDict¶

 In an HttpRequest object, the GET and POST attributes are instances of
 django.http.QueryDict, a dictionary-like class customized to deal with
 multiple values for the same key. This is necessary because some HTML form
 elements, notably <select multiple>, pass multiple values for the same
 key.

 The QueryDicts at request.POST and request.GET will be immutable when
 accessed in a normal request/response cycle. To get a mutable version you
 need to use QueryDict.copy().
 }}}

 That leads me to think that people are copying a `QueryDict` because it's
 a nice starting point to have all the query params organized, but they
 want to modify it, and pass to the template. Doing so would result in a
 `This QueryDict instance is immutable` exception, and the advice above, so
 they try to copy the `QueryDict` before modifying it.

 I propose we omit `TemporaryUploadedFile` values on deep copy of a
 `QueryDict`, and will open a PR for feedback.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070183f8ee9acb-0fc3225e-0eed-453a-9c99-270340a745f1-000000%40eu-central-1.amazonses.com.

Reply via email to