#29278: FileResponse documentation should warn against using context managers ------------------------------------------+------------------------ Reporter: Mike DePalatis | Owner: nobody Type: Uncategorized | Status: new Component: Documentation | Version: 2.0 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 1 UI/UX: 0 | ------------------------------------------+------------------------ Idiomatic Python usually opens file objects using a context manager like this:
{{{ with open("filename.txt", "rb") as fo: # do stuff with fo }}} This is problematic when using a `FileResponse` which requires a file-like object rather than a path. Doing something like: {{{ with open("filename.txt", "rb") as fo: return FileResponse(fo) }}} results in an error due to trying to operate on a closed file: {{{ Exception happened during processing of request from ('127.0.0.1', 60169) Traceback (most recent call last): File "/Users/zduey/anaconda3/envs/cml/lib/python3.5/wsgiref/handlers.py", line 138, in run self.finish_response() File "/Users/zduey/anaconda3/envs/cml/lib/python3.5/wsgiref/handlers.py", line 179, in finish_response for data in self.result: File "/Users/zduey/anaconda3/envs/cml/lib/python3.5/wsgiref/util.py", line 30, in __next__ data = self.filelike.read(self.blksize) ValueError: read of closed file }}} The documentation for `FileResponse` should be updated to explicitly warn users against passing in file objects opened with a context manager and make clear that the file will be closed automatically. -- Ticket URL: <https://code.djangoproject.com/ticket/29278> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.20f06ea9bead2c4bae7eeb16391fbcd4%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.