Added file object to '_closable_objects' to close at end of request #11
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This commit fixes ResourceWarnings in Python 3 where file objects do not appear to be closed at the end of the request, and the Django server will emit ResourceWarnings about unclosed files.
The
_closable_objects
list is managed by the base FileResponse class in Django and will call.close()
on objects at the end of the request.Issue number
N/A
Expected behaviour
File object would be closed at the end of the request.
Actual behaviour
File object remains open.
Description of fix
The
FileResponse
object maintains a list of objects to be closed at the end of the request. Since this class uses a special reader class the file object is not appended to. This will add the file object when the instance is created. Checks to ensure file object has aclose()
method, and that theFileResponse
class has the_closable_object
property (for compatibility).Other info
Was causing intermittent warnings/crashes in the Django runserver when
--autoreload
used.