#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: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by anton-kazlouski):
This is exactly what happened in my case. I have a code that modifies some
attributes of `request.data` and then passes it to a serializer. At some
point we started to notice `This QueryDict instance is immutable`
exceptions in our logs. Thus, decided to use built-in method which you are
referenced to. Once, replaced direct modifications `request.data` with
`request.data.copy()`, `TypeError: cannot pickle '_io.BufferedRandom'
object on...` started to araise.
Replying to [comment:12 Dan Madere]:
>
> 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:17>
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/01070185340151cb-c0673a4f-428b-43c4-83e5-48f42cc974da-000000%40eu-central-1.amazonses.com.