Michael Merickel
2015-02-06 9616e20bac92ba33a3d5751b3c1993c74397c7a1
backport #1535 by avoiding the request if it's None
2 files modified
28 ■■■■■ changed files
pyramid/renderers.py 22 ●●●● patch | view | raw | blame | history
pyramid/tests/test_renderers.py 6 ●●●●● patch | view | raw | blame | history
pyramid/renderers.py
@@ -356,19 +356,19 @@
        ``self.param_name`` is present in request.GET; otherwise returns
        plain-JSON encoded string with content-type ``application/json``"""
        def _render(value, system):
            request = system['request']
            request = system.get('request')
            default = self._make_default(request)
            val = self.serializer(value, default=default, **self.kw)
            callback = request.GET.get(self.param_name)
            if callback is None:
                ct = 'application/json'
                body = val
            else:
                ct = 'application/javascript'
                body = '%s(%s);' % (callback, val)
            response = request.response
            if response.content_type == response.default_content_type:
                response.content_type = ct
            ct = 'application/json'
            body = val
            if request is not None:
                callback = request.GET.get(self.param_name)
                if callback is not None:
                    ct = 'application/javascript'
                    body = '%s(%s);' % (callback, val)
                response = request.response
                if response.content_type == response.default_content_type:
                    response.content_type = ct
            return body
        return _render
pyramid/tests/test_renderers.py
@@ -580,6 +580,12 @@
        self.assertEqual(request.response.content_type,
                         'application/json')
    def test_render_without_request(self):
        renderer_factory = self._makeOne()
        renderer = renderer_factory(None)
        result = renderer({'a':'1'}, {})
        self.assertEqual(result, '{"a": "1"}')
class Dummy:
    pass