Skip to content

Commit

Permalink
Ensure closables in request.FILES get closed. (encode#5262)
Browse files Browse the repository at this point in the history
Ensure closables on `.FILES` get closed.
  • Loading branch information
tomchristie authored Jul 10, 2017
1 parent b905197 commit 6d4d4df
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions rest_framework/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,10 @@ def _load_data_and_files(self):
else:
self._full_data = self._data

# copy files refs to the underlying request so that closable
# objects are handled appropriately.
self._request._files = self._files

def _load_stream(self):
"""
Return the content body of the request, as a stream.
Expand Down
2 changes: 1 addition & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ class TestDateTimeField(FieldValues):
valid_inputs = {
'2001-01-01 13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
'2001-01-01T13:00': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc)
'2001-01-01T13:00Z': datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
datetime.datetime(2001, 1, 1, 13, 00): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc): datetime.datetime(2001, 1, 1, 13, 00, tzinfo=utc),
}
Expand Down
31 changes: 31 additions & 0 deletions tests/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"""
from __future__ import unicode_literals

import os.path
import tempfile

from django.conf.urls import url
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.models import User
Expand Down Expand Up @@ -120,11 +123,39 @@ def post(self, request):

return Response(status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class FileUploadView(APIView):
def post(self, request):
filenames = [file.temporary_file_path() for file in request.FILES.values()]

for filename in filenames:
assert os.path.exists(filename)

return Response(status=status.HTTP_200_OK, data=filenames)


urlpatterns = [
url(r'^$', MockView.as_view()),
url(r'^upload/$', FileUploadView.as_view())
]


@override_settings(
ROOT_URLCONF='tests.test_request',
FILE_UPLOAD_HANDLERS=['django.core.files.uploadhandler.TemporaryFileUploadHandler'])
class FileUploadTests(TestCase):

def test_fileuploads_closed_at_request_end(self):
with tempfile.NamedTemporaryFile() as f:
response = self.client.post('/upload/', {'file': f})

# sanity check that file was processed
assert len(response.data) == 1

for file in response.data:
assert not os.path.exists(file)


@override_settings(ROOT_URLCONF='tests.test_request')
class TestContentParsingWithAuthentication(TestCase):
def setUp(self):
Expand Down

0 comments on commit 6d4d4df

Please sign in to comment.