Michael Merickel
2015-02-06 4b13e0dfcdf47b3ef9befd529c0d78af8a2712d5
Merge pull request #1561 from Pylons/fix.jsonp-without-request

fix #1535 by avoiding the request if it's None
3 files modified
35 ■■■■■ changed files
CHANGES.txt 5 ●●●●● patch | view | raw | blame | history
pyramid/renderers.py 24 ●●●● patch | view | raw | blame | history
pyramid/tests/test_renderers.py 6 ●●●●● patch | view | raw | blame | history
CHANGES.txt
@@ -120,6 +120,11 @@
- Fix route generation for static view asset specifications having no path.
  See https://github.com/Pylons/pyramid/pull/1377
- Allow the ``pyramid.renderers.JSONP`` renderer to work even if there is no
  valid request object. In this case it will not wrap the object in a
  callback and thus behave just like the ``pyramid.renderers.JSON` renderer.
  See https://github.com/Pylons/pyramid/pull/1561
Deprecations
------------
pyramid/renderers.py
@@ -24,7 +24,7 @@
from pyramid.path import caller_package
from pyramid.response import Response, _get_response_factory
from pyramid.response import _get_response_factory
from pyramid.threadlocal import get_current_registry
# API
@@ -355,19 +355,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
@@ -602,6 +602,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