summaryrefslogtreecommitdiff
path: root/cherrypy/lib/encoding.py
diff options
context:
space:
mode:
authorAllan Crooks <allan@amcone.net>2014-05-04 14:39:10 +0100
committerAllan Crooks <allan@amcone.net>2014-05-04 14:39:10 +0100
commita400b3aa0ecd1dfcd909038b4c4e18a7441784df (patch)
treeef9958887ca1b1948bc2b3dafd2df22e220b4192 /cherrypy/lib/encoding.py
parent975a1d68020320938b1dcc0efec529b69b9a2733 (diff)
downloadcherrypy-git-a400b3aa0ecd1dfcd909038b4c4e18a7441784df.tar.gz
Explicitly close response iterators when finished with them; this should help us clean up memory sooner when the iterators aren't fully consumed. Fixes #1314.
Diffstat (limited to 'cherrypy/lib/encoding.py')
-rw-r--r--cherrypy/lib/encoding.py15
1 files changed, 10 insertions, 5 deletions
diff --git a/cherrypy/lib/encoding.py b/cherrypy/lib/encoding.py
index 6d54ac8c..fa8387b6 100644
--- a/cherrypy/lib/encoding.py
+++ b/cherrypy/lib/encoding.py
@@ -4,6 +4,7 @@ import time
import cherrypy
from cherrypy._cpcompat import basestring, BytesIO, ntob, set, unicodestr
from cherrypy.lib import file_generator
+from cherrypy.lib import is_closable_iterator
from cherrypy.lib import set_vary_header
@@ -32,23 +33,27 @@ def decode(encoding=None, default_encoding='utf-8'):
if not isinstance(default_encoding, list):
default_encoding = [default_encoding]
body.attempt_charsets = body.attempt_charsets + default_encoding
-
+
class UTF8StreamEncoder:
def __init__(self, iterator):
self._iterator = iterator
-
+
def __iter__(self):
return self
-
+
def next(self):
return self.__next__()
-
+
def __next__(self):
res = next(self._iterator)
if isinstance(res, unicodestr):
res = res.encode('utf-8')
return res
-
+
+ def close(self):
+ if is_closable_iterator(self._iterator)
+ self._iterator.close()
+
def __getattr__(self, attr):
if attr.startswith('__'):
raise AttributeError(self, attr)