diff options
Diffstat (limited to 'django/http')
| -rw-r--r-- | django/http/multipartparser.py | 10 | ||||
| -rw-r--r-- | django/http/request.py | 6 |
2 files changed, 16 insertions, 0 deletions
diff --git a/django/http/multipartparser.py b/django/http/multipartparser.py index 20232cb7c6..1bcace94cd 100644 --- a/django/http/multipartparser.py +++ b/django/http/multipartparser.py @@ -228,6 +228,7 @@ class MultiPartParser(object): break except SkipFile: + self._close_files() # Just use up the rest of this file... exhaust(field_stream) else: @@ -237,6 +238,7 @@ class MultiPartParser(object): # If this is neither a FIELD or a FILE, just exhaust the stream. exhaust(stream) except StopUpload as e: + self._close_files() if not e.connection_reset: exhaust(self._input_data) else: @@ -268,6 +270,14 @@ class MultiPartParser(object): """Cleanup filename from Internet Explorer full paths.""" return filename and filename[filename.rfind("\\") + 1:].strip() + def _close_files(self): + # Free up all file handles. + # FIXME: this currently assumes that upload handlers store the file as 'file' + # We should document that... (Maybe add handler.free_file to complement new_file) + for handler in self._upload_handlers: + if hasattr(handler, 'file'): + handler.file.close() + class LazyStream(six.Iterator): """ diff --git a/django/http/request.py b/django/http/request.py index ded4744a87..ecad639c82 100644 --- a/django/http/request.py +++ b/django/http/request.py @@ -5,6 +5,7 @@ import os import re import sys from io import BytesIO +from itertools import chain from pprint import pformat from django.conf import settings @@ -245,6 +246,11 @@ class HttpRequest(object): else: self._post, self._files = QueryDict('', encoding=self._encoding), MultiValueDict() + def close(self): + if hasattr(self, '_files'): + for f in chain.from_iterable(l[1] for l in self._files.lists()): + f.close() + # File-like and iterator interface. # # Expects self._stream to be set to an appropriate source of bytes by |
