Repository: Pylons/pyramid

Issue:
1.7.1 changed the json renderer to return bytes instead of unicode
```
result = pyramid.renderers.render('json', {}, request=request)
```

`result` is now `bytes` instead of `unicode`. This is a regression in 1.6.2 and 1.7.1.

This is not a problem with `render_to_response` or `view_config(..., renderer=json)` because they are using the response object.



Discussion:
related: https://github.com/Pylons/pyramid/pull/2706


Resolving patch:
diff --git a/pyramid/renderers.py b/pyramid/renderers.py
--- a/pyramid/renderers.py
+++ b/pyramid/renderers.py
@@ -1,7 +1,6 @@
 import json
 import os
 import re
-import warnings
 
 from zope.interface import (
     implementer,
@@ -273,7 +272,7 @@ def _render(value, system):
                 if ct == response.default_content_type:
                     response.content_type = 'application/json'
             default = self._make_default(request)
-            return self.serializer(value, default=default, **self.kw).encode('UTF-8')
+            return self.serializer(value, default=default, **self.kw)
 
         return _render
 
@@ -380,7 +379,7 @@ def _render(value, system):
                         raise HTTPBadRequest('Invalid JSONP callback function name.')
 
                     ct = 'application/javascript'
-                    body = '/**/{0}({1});'.format(callback, val).encode('UTF-8')
+                    body = '/**/{0}({1});'.format(callback, val)
                 response = request.response
                 if response.content_type == response.default_content_type:
                     response.content_type = ct
@@ -468,17 +467,7 @@ def _make_response(self, result, request):
 
         if result is not None:
             if isinstance(result, text_type):
-                if response.charset is None:
-                    warnings.warn(
-                        "Renderer returned a result of type {0}, "
-                        "however the response Content-Type <{1}> does not "
-                        "have a charset. Implicitly encoding the result as "
-                        "UTF-8.".format(type(result), response.content_type),
-                        RuntimeWarning
-                    )
-                    response.body = result.encode('UTF-8')
-                else:
-                    response.text = result
+                response.text = result
             elif isinstance(result, bytes):
                 response.body = result
             elif hasattr(result, '__iter__'):


Test patch:
diff --git a/pyramid/tests/test_config/test_views.py b/pyramid/tests/test_config/test_views.py
--- a/pyramid/tests/test_config/test_views.py
+++ b/pyramid/tests/test_config/test_views.py
@@ -2168,7 +2168,7 @@ def test_add_notfound_view_with_renderer(self):
                                      ctx_iface=implementedBy(HTTPNotFound),
                                      request_iface=IRequest)
         result = view(None, request)
-        self._assertBody(result, b'{}')
+        self._assertBody(result, '{}')
 
     def test_add_forbidden_view_with_renderer(self):
         from zope.interface import implementedBy
@@ -2185,7 +2185,7 @@ def test_add_forbidden_view_with_renderer(self):
                                      ctx_iface=implementedBy(HTTPForbidden),
                                      request_iface=IRequest)
         result = view(None, request)
-        self._assertBody(result, b'{}')
+        self._assertBody(result, '{}')
 
     def test_set_view_mapper(self):
         from pyramid.interfaces import IViewMapperFactory
diff --git a/pyramid/tests/test_renderers.py b/pyramid/tests/test_renderers.py
--- a/pyramid/tests/test_renderers.py
+++ b/pyramid/tests/test_renderers.py
@@ -18,7 +18,7 @@ def _makeOne(self, **kw):
     def test_it(self):
         renderer = self._makeOne()(None)
         result = renderer({'a':1}, {})
-        self.assertEqual(result, b'{"a": 1}')
+        self.assertEqual(result, '{"a": 1}')
 
     def test_with_request_content_type_notset(self):
         request = testing.DummyRequest()
@@ -43,7 +43,7 @@ def adapter(obj, req):
         renderer = self._makeOne()
         renderer.add_adapter(datetime, adapter)
         result = renderer(None)({'a':now}, {'request':request})
-        self.assertEqual(result, '{{"a": "{0}"}}'.format(now.isoformat()).encode('UTF-8'))
+        self.assertEqual(result, '{"a": "%s"}' % now.isoformat())
 
     def test_with_custom_adapter2(self):
         request = testing.DummyRequest()
@@ -54,7 +54,7 @@ def adapter(obj, req):
         now = datetime.utcnow()
         renderer = self._makeOne(adapters=((datetime, adapter),))
         result = renderer(None)({'a':now}, {'request':request})
-        self.assertEqual(result, '{{"a": "{0}"}}'.format(now.isoformat()).encode('UTF-8'))
+        self.assertEqual(result, '{"a": "%s"}' % now.isoformat())
 
     def test_with_custom_serializer(self):
         class Serializer(object):
@@ -66,7 +66,7 @@ def __call__(self, obj, **kw):
         renderer = self._makeOne(serializer=serializer, baz=5)
         obj = {'a':'b'}
         result = renderer(None)(obj, {})
-        self.assertEqual(result, b'foo')
+        self.assertEqual(result, 'foo')
         self.assertEqual(serializer.obj, obj)
         self.assertEqual(serializer.kw['baz'], 5)
         self.assertTrue('default' in serializer.kw)
@@ -84,7 +84,7 @@ def __json__(self, req):
         objects = [MyObject(1), MyObject(2)]
         renderer = self._makeOne()(None)
         result = renderer(objects, {'request':request})
-        self.assertEqual(result, b'[{"x": 1}, {"x": 2}]')
+        self.assertEqual(result, '[{"x": 1}, {"x": 2}]')
 
     def test_with_object_adapter_no___json__(self):
         class MyObject(object):
@@ -290,19 +290,6 @@ 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_str_no_charset(self):
-        from pyramid.response import Response
-        request = testing.DummyRequest()
-        request.response = Response(content_type='application/json', charset=None)
-
-        self.assertIsNone(request.response.charset)
-
-        helper = self._makeOne('loo.foo')
-        la = text_('/La Pe\xc3\xb1a', 'utf-8')
-        response = helper._make_response(la, request)
-        self.assertIsNone(response.charset)
-        self.assertEqual(response.body, la.encode('utf-8'))
-
     def test__make_response_result_is_iterable(self):
         from pyramid.response import Response
         request = testing.DummyRequest()
@@ -505,7 +492,7 @@ def test_response_preserved(self):
         request.response = response
         # use a json renderer, which will mutate the response
         result = self._callFUT('json', dict(a=1), request=request)
-        self.assertEqual(result, b'{"a": 1}')
+        self.assertEqual(result, '{"a": 1}')
         self.assertEqual(request.response, response)
 
     def test_no_response_to_preserve(self):
@@ -520,7 +507,7 @@ def response(self):
         request = DummyRequestWithClassResponse()
         # use a json renderer, which will mutate the response
         result = self._callFUT('json', dict(a=1), request=request)
-        self.assertEqual(result, b'{"a": 1}')
+        self.assertEqual(result, '{"a": 1}')
         self.assertFalse('response' in request.__dict__)
 
 class Test_render_to_response(unittest.TestCase):
@@ -640,7 +627,7 @@ def test_render_to_jsonp(self):
         request = testing.DummyRequest()
         request.GET['callback'] = 'callback'
         result = renderer({'a':'1'}, {'request':request})
-        self.assertEqual(result, b'/**/callback({"a": "1"});')
+        self.assertEqual(result, '/**/callback({"a": "1"});')
         self.assertEqual(request.response.content_type,
                          'application/javascript')
 
@@ -650,7 +637,7 @@ def test_render_to_jsonp_with_dot(self):
         request = testing.DummyRequest()
         request.GET['callback'] = 'angular.callbacks._0'
         result = renderer({'a':'1'}, {'request':request})
-        self.assertEqual(result, b'/**/angular.callbacks._0({"a": "1"});')
+        self.assertEqual(result, '/**/angular.callbacks._0({"a": "1"});')
         self.assertEqual(request.response.content_type,
                          'application/javascript')