Skip to content

Commit

Permalink
Support returning app_iter from renderer.
Browse files Browse the repository at this point in the history
Sets `response.app_iter = result` for renderer results which are iterable and not text/bytes.
  • Loading branch information
lrowe committed Dec 6, 2014
1 parent 0117573 commit f0a9dfb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
4 changes: 4 additions & 0 deletions pyramid/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,10 @@ def _make_response(self, result, request):
if result is not None:
if isinstance(result, text_type):
response.text = result
elif isinstance(result, bytes):
response.body = result
elif hasattr(result, '__iter__'):
response.app_iter = result
else:
response.body = result

Expand Down
25 changes: 21 additions & 4 deletions pyramid/tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ def test_render_to_response(self):
helper = self._makeOne('loo.foo')
response = helper.render_to_response('values', {},
request=request)
self.assertEqual(response.body[0], 'values')
self.assertEqual(response.body[1], {})
self.assertEqual(response.app_iter[0], 'values')
self.assertEqual(response.app_iter[1], {})

def test_get_renderer(self):
factory = self._registerRendererFactory()
Expand All @@ -209,8 +209,8 @@ def test_render_view(self):
request = testing.DummyRequest()
response = 'response'
response = helper.render_view(request, response, view, context)
self.assertEqual(response.body[0], 'response')
self.assertEqual(response.body[1],
self.assertEqual(response.app_iter[0], 'response')
self.assertEqual(response.app_iter[1],
{'renderer_info': helper,
'renderer_name': 'loo.foo',
'request': request,
Expand Down Expand Up @@ -287,6 +287,23 @@ def test__make_response_result_is_str(self):
response = helper._make_response(la.encode('utf-8'), request)
self.assertEqual(response.body, la.encode('utf-8'))

def test__make_response_result_is_iterable(self):
from pyramid.response import Response
request = testing.DummyRequest()
request.response = Response()
helper = self._makeOne('loo.foo')
la = text_('/La Pe\xc3\xb1a', 'utf-8')
response = helper._make_response([la.encode('utf-8')], request)
self.assertEqual(response.body, la.encode('utf-8'))

def test__make_response_result_is_other(self):
self._registerResponseFactory()
request = None
helper = self._makeOne('loo.foo')
result = object()
response = helper._make_response(result, request)
self.assertEqual(response.body, result)

def test__make_response_result_is_None_no_body(self):
from pyramid.response import Response
request = testing.DummyRequest()
Expand Down

0 comments on commit f0a9dfb

Please sign in to comment.