Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1535 by avoiding the request if it's None #1561

Merged
merged 2 commits into from
Feb 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ Bug Fixes
- 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
------------

Expand Down
24 changes: 12 additions & 12 deletions pyramid/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -355,19 +355,19 @@ def __call__(self, info):
``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

Expand Down
6 changes: 6 additions & 0 deletions pyramid/tests/test_renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,12 @@ def test_render_to_json(self):
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
Expand Down