# HG changeset patch # User RĂ©mi Cardona # Date 1442421799 -7200 # Node ID a6d9d27f4253e34c568afc493b7a1604f5f1ea58 # Parent 9bad9e06193219c080c81ab22e8d57f270e53f2a [web/views] port JSON views to py3k diff -r 9bad9e061932 -r a6d9d27f4253 devtools/testlib.py --- a/devtools/testlib.py Wed Sep 16 18:43:02 2015 +0200 +++ b/devtools/testlib.py Wed Sep 16 18:43:19 2015 +0200 @@ -88,7 +88,7 @@ class JsonValidator(object): def parse_string(self, data): - return json.loads(data) + return json.loads(data.decode('ascii')) @contextmanager def real_error_handling(app): diff -r 9bad9e061932 -r a6d9d27f4253 web/test/unittest_views_json.py --- a/web/test/unittest_views_json.py Wed Sep 16 18:43:02 2015 +0200 +++ b/web/test/unittest_views_json.py Wed Sep 16 18:43:19 2015 +0200 @@ -16,12 +16,14 @@ # # You should have received a copy of the GNU Lesser General Public License along # with CubicWeb. If not, see . +from six import binary_type + from cubicweb.devtools.testlib import CubicWebTC class JsonViewsTC(CubicWebTC): anonymize = True - res_jsonp_data = '[["guests", 1]]' + res_jsonp_data = b'[["guests", 1]]' def setUp(self): super(JsonViewsTC, self).setUp() @@ -47,10 +49,10 @@ 'rql': u'Any GN,COUNT(X) GROUPBY GN ORDERBY GN ' 'WHERE X in_group G, G name GN'}) data = self.ctrl_publish(req, ctrl='jsonp') - self.assertIsInstance(data, str) + self.assertIsInstance(data, binary_type) self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript']) # because jsonp anonymizes data, only 'guests' group should be found - self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data) + self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')') def test_json_rsetexport_with_jsonp_and_bad_vid(self): with self.admin_access.web_request() as req: @@ -61,7 +63,7 @@ data = self.ctrl_publish(req, ctrl='jsonp') self.assertEqual(req.headers_out.getRawHeaders('content-type'), ['application/javascript']) # result should be plain json, not the table view - self.assertEqual(data, 'foo(%s)' % self.res_jsonp_data) + self.assertEqual(data, b'foo(' + self.res_jsonp_data + b')') def test_json_ersetexport(self): with self.admin_access.web_request() as req: @@ -79,7 +81,7 @@ class NotAnonymousJsonViewsTC(JsonViewsTC): anonymize = False - res_jsonp_data = '[["guests", 1], ["managers", 1]]' + res_jsonp_data = b'[["guests", 1], ["managers", 1]]' if __name__ == '__main__': from logilab.common.testlib import unittest_main diff -r 9bad9e061932 -r a6d9d27f4253 web/views/json.py --- a/web/views/json.py Wed Sep 16 18:43:02 2015 +0200 +++ b/web/views/json.py Wed Sep 16 18:43:19 2015 +0200 @@ -64,7 +64,7 @@ # use ``application/javascript`` if ``callback`` parameter is # provided, keep ``application/json`` otherwise self._cw.set_content_type('application/javascript') - json_data = b'%s(%s)' % (json_padding, json_data) + json_data = json_padding + b'(' + json_data + b')' return json_data @@ -85,7 +85,8 @@ indent = int(self._cw.form['_indent']) else: indent = None - self.w(json_dumps(data, indent=indent)) + # python's json.dumps escapes non-ascii characters + self.w(json_dumps(data, indent=indent).encode('ascii')) class JsonRsetView(JsonMixIn, AnyRsetView):