From 549445519ce90cc5c1e3f981853cc0c67725f3ed Mon Sep 17 00:00:00 2001 From: Florian Apolloner Date: Fri, 7 Feb 2020 11:30:26 +0100 Subject: [PATCH] Reverted "Fixed #30565 -- Closed HttpResponse when wsgi.file_wrapper closes file-like object." This reverts commit cce47ff65a4dd3786c049ec14ee889e128ca7de9. --- django/http/response.py | 28 ---------------- docs/ref/request-response.txt | 3 +- tests/responses/test_fileresponse.py | 49 ---------------------------- 3 files changed, 1 insertion(+), 79 deletions(-) diff --git a/django/http/response.py b/django/http/response.py index 596732a5b8..a7d98117a6 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -242,9 +242,6 @@ class HttpResponseBase: # The WSGI server must call this method upon completion of the request. # See http://blog.dscpl.com.au/2012/10/obligations-for-calling-close-on.html - # When wsgi.file_wrapper is used, the WSGI server instead calls close() - # on the file-like object. Django ensures this method is called in this - # case by replacing self.file_to_stream.close() with a wrapped version. def close(self): for closable in self._closable_objects: try: @@ -401,39 +398,14 @@ class FileResponse(StreamingHttpResponse): self.filename = filename super().__init__(*args, **kwargs) - def _wrap_file_to_stream_close(self, filelike): - """ - Wrap the file-like close() with a version that calls - FileResponse.close(). - """ - closing = False - filelike_close = getattr(filelike, 'close', lambda: None) - - def file_wrapper_close(): - nonlocal closing - # Prevent an infinite loop since FileResponse.close() tries to - # close the objects in self._closable_objects. - if closing: - return - closing = True - try: - filelike_close() - finally: - self.close() - - filelike.close = file_wrapper_close - def _set_streaming_content(self, value): if not hasattr(value, 'read'): self.file_to_stream = None return super()._set_streaming_content(value) self.file_to_stream = filelike = value - # Add to closable objects before wrapping close(), since the filelike - # might not have close(). if hasattr(filelike, 'close'): self._closable_objects.append(filelike) - self._wrap_file_to_stream_close(filelike) value = iter(lambda: filelike.read(self.block_size), b'') self.set_headers(filelike) super()._set_streaming_content(value) diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt index 95994ee41e..fbe7a26a05 100644 --- a/docs/ref/request-response.txt +++ b/docs/ref/request-response.txt @@ -911,8 +911,7 @@ Methods .. method:: HttpResponse.close() This method is called at the end of the request directly by the WSGI - server, or when the WSGI server closes the file-like object, if - `wsgi.file_wrapper`_ is used for the request. + server. .. method:: HttpResponse.write(content) diff --git a/tests/responses/test_fileresponse.py b/tests/responses/test_fileresponse.py index a6bcb58466..e77df4513a 100644 --- a/tests/responses/test_fileresponse.py +++ b/tests/responses/test_fileresponse.py @@ -80,52 +80,3 @@ class FileResponseTests(SimpleTestCase): response['Content-Disposition'], "attachment; filename*=utf-8''%E7%A5%9D%E6%82%A8%E5%B9%B3%E5%AE%89.odt" ) - - def test_file_to_stream_closes_response(self): - # Closing file_to_stream calls FileResponse.close(), even when - # file-like object doesn't have a close() method. - class FileLike: - def read(self): - pass - - class FileLikeWithClose(FileLike): - def __init__(self): - self.closed = False - - def close(self): - self.closed = True - - for filelike_cls in (FileLike, FileLikeWithClose): - with self.subTest(filelike_cls=filelike_cls.__name__): - filelike = filelike_cls() - response = FileResponse(filelike) - self.assertFalse(response.closed) - # Object with close() is added to the list of closable. - if hasattr(filelike, 'closed'): - self.assertEqual(response._closable_objects, [filelike]) - else: - self.assertEqual(response._closable_objects, []) - file_to_stream = response.file_to_stream - file_to_stream.close() - if hasattr(filelike, 'closed'): - self.assertTrue(filelike.closed) - self.assertTrue(response.closed) - - def test_file_to_stream_closes_response_on_error(self): - # Closing file_to_stream calls FileResponse.close(), even when - # closing file-like raises exceptions. - class FileLikeWithRaisingClose: - def read(self): - pass - - def close(self): - raise RuntimeError() - - filelike = FileLikeWithRaisingClose() - response = FileResponse(filelike) - self.assertFalse(response.closed) - self.assertEqual(response._closable_objects, [filelike]) - file_to_stream = response.file_to_stream - with self.assertRaises(RuntimeError): - file_to_stream.close() - self.assertTrue(response.closed)