Explicitly close a file in the static serve view. Thanks to Benjamin Peterson for the report.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16282 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Alex Gaynor 2011-05-27 04:06:23 +00:00
parent c3db0b058c
commit 20fbfbadcd
1 changed files with 3 additions and 1 deletions

View File

@ -2,6 +2,7 @@
Views and functions for serving static files. These are only to be used Views and functions for serving static files. These are only to be used
during development, and SHOULD NOT be used in a production setting. during development, and SHOULD NOT be used in a production setting.
""" """
from __future__ import with_statement
import mimetypes import mimetypes
import os import os
@ -57,7 +58,8 @@ def serve(request, path, document_root=None, show_indexes=False):
if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'), if not was_modified_since(request.META.get('HTTP_IF_MODIFIED_SINCE'),
statobj.st_mtime, statobj.st_size): statobj.st_mtime, statobj.st_size):
return HttpResponseNotModified(mimetype=mimetype) return HttpResponseNotModified(mimetype=mimetype)
response = HttpResponse(open(fullpath, 'rb').read(), mimetype=mimetype) with open(fullpath, 'rb') as f:
response = HttpResponse(f.read(), mimetype=mimetype)
response["Last-Modified"] = http_date(statobj.st_mtime) response["Last-Modified"] = http_date(statobj.st_mtime)
if stat.S_ISREG(statobj.st_mode): if stat.S_ISREG(statobj.st_mode):
response["Content-Length"] = statobj.st_size response["Content-Length"] = statobj.st_size