From 7cac620d6834f2997bedbb1e6bbb10637f97a9b7 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sat, 28 May 2011 19:44:41 -0400 Subject: use a generator; explain --- pyramid/exceptions.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/pyramid/exceptions.py b/pyramid/exceptions.py index 53cb0e5a8..e626efd5c 100644 --- a/pyramid/exceptions.py +++ b/pyramid/exceptions.py @@ -220,7 +220,6 @@ ${body}''') ## Set this to True for responses that should have no request body empty_body = False - _default_called = False def __init__(self, detail=None, headers=None, comment=None, body_template=None, **kw): @@ -239,14 +238,17 @@ ${body}''') del self.content_type del self.content_length elif not ('unicode_body' in kw or 'body' in kw or 'app_iter' in kw): - self.app_iter = iter(self._default_app_iter, None) + self.app_iter = self._default_app_iter() def __str__(self): return self.detail or self.explanation def _default_app_iter(self): - if self._default_called: - return None + # This is a generator which defers the creation of the response page + # body; we use a generator because we want to ensure that if + # attributes of this response are changed after it is constructed we + # use the changed values rather than the values at time of construction + # (e.g. self.content_type). html_comment = '' comment = self.comment or '' if 'html' in self.content_type or '': @@ -281,8 +283,8 @@ ${body}''') page = page_template.substitute(status=self.status, body=body) if isinstance(page, unicode): page = page.encode(self.charset) - self._default_called = True - return page + yield page + raise StopIteration def wsgi_response(self): return self -- cgit v1.2.3