From f0a9dfba27012bcf84fccd445b0fcaa1bca32382 Mon Sep 17 00:00:00 2001 From: Laurence Rowe Date: Fri, 5 Dec 2014 17:37:57 -0800 Subject: [PATCH] Support returning app_iter from renderer. Sets `response.app_iter = result` for renderer results which are iterable and not text/bytes. --- pyramid/renderers.py | 4 ++++ pyramid/tests/test_renderers.py | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/pyramid/renderers.py b/pyramid/renderers.py index 108255ee47..2addaa5feb 100644 --- a/pyramid/renderers.py +++ b/pyramid/renderers.py @@ -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 diff --git a/pyramid/tests/test_renderers.py b/pyramid/tests/test_renderers.py index 2bddd23188..bb5b0455a4 100644 --- a/pyramid/tests/test_renderers.py +++ b/pyramid/tests/test_renderers.py @@ -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() @@ -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, @@ -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()