#29510: QueryDict.copy() returns closed files when the type of file is
TemporaryUploadedFile
-------------------------------------+-------------------------------------
     Reporter:  Liquid Scorpio       |                    Owner:  Dan
                                     |  Madere
         Type:  Bug                  |                   Status:  closed
    Component:  File                 |                  Version:  1.11
  uploads/storage                    |
     Severity:  Normal               |               Resolution:  invalid
     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
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

 * status:  assigned => closed
 * resolution:   => invalid


Comment:

 So (ruling out the test client) I've experimented with a real-view,
 uploading a test file with some POST data as well:

 {{{
 from django.urls import path
 from django.http import HttpResponse

 import pprint

 # With settings to ensure TemporaryUploadedFile:
 #   FILE_UPLOAD_MAX_MEMORY_SIZE=10
 def file_upload_view(request):
     pprint.pprint(request)
     print("\nPOST:")
     pprint.pprint(request.POST)
     print("\nFILES:")
     pprint.pprint(request.FILES)

     f = request.FILES.copy()
     assert f["file1"].seekable()

     return HttpResponse("Got it!")


 urlpatterns = [
     path("upload", file_upload_view),
 ]
 }}}

 This passes without issue:

 {{{
 <WSGIRequest: POST '/upload'>

 POST:
 <QueryDict: {'key1': ['value1'], 'key2': ['value2']}>

 FILES:
 {'file1': <TemporaryUploadedFile: Large Test File.txt (text/plain)>}
 }}}

 Note the...

 {{{
     f = request.FILES.copy()
     assert f["file1"].seekable()
 }}}

 I've tested back to Python 3.9 and Django 3.2.

 The reason this works is that `FILES` is a `MultiValueDict` not a
 `QueryDict`:


 {{{
 >>> request.FILES.copy()
 <MultiValueDict: {'file_field1': [<TemporaryUploadedFile:
 tmpx56phiic.file1 (application/octet-stream)>], 'file_field2':
 [<TemporaryUploadedFile: tmp7ipmyal8.file2 (application/octet-stream)>]}>
 }}}

 ...and so `copy()` uses `copy.copy()` rather than `copy.deepcopy()`.

 If you try and `deepcopy()`  the `TemporaryUploadedFile` you indeed get
 the error, but that's not what Django does.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29510#comment:20>
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/01070185ca608483-35dc232c-bdf0-46a1-a37c-63fa920d0316-000000%40eu-central-1.amazonses.com.

Reply via email to