From d3ef52fbf32796ccde58dc06eac0d54e2a4716af Mon Sep 17 00:00:00 2001 From: elnuno Date: Tue, 11 Apr 2017 22:27:32 -0300 Subject: [PATCH] Close CallbackFileWrapper.__buf once it's used. --- cachecontrol/filewrapper.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/cachecontrol/filewrapper.py b/cachecontrol/filewrapper.py index dd91334c..bcb39858 100644 --- a/cachecontrol/filewrapper.py +++ b/cachecontrol/filewrapper.py @@ -62,9 +62,16 @@ def _close(self): # and allows the garbage collector to do it's thing normally. self.__callback = None + # Closing the BytesIO stream releases memory. Important when caching + # big files. + self.__buf.close() + def read(self, amt=None): data = self.__fp.read(amt) - self.__buf.write(data) + if data: + # We may be dealing with b'', a sign that things are over: + # it's passed e.g. after we've already closed self.__buf. + self.__buf.write(data) if self.__is_fp_closed(): self._close()