[python2.6] prefer python2.6's builtin json module over simplejson stable
authorAlexandre Fayolle <alexandre.fayolle@logilab.fr>
Thu, 22 Apr 2010 19:48:04 +0000
branchstable
changeset 5377 84d14ddfae13
parent 5376 2c3f14bc2590
child 5383 fbe7416104c6
[python2.6] prefer python2.6's builtin json module over simplejson
devtools/testlib.py
goa/goactl.py
test/unittest_utils.py
utils.py
view.py
web/__init__.py
web/_exceptions.py
web/application.py
web/component.py
web/formwidgets.py
web/request.py
web/test/unittest_views_basecontrollers.py
web/test/unittest_views_baseviews.py
web/views/autoform.py
web/views/basecontrollers.py
web/views/editforms.py
web/views/facets.py
web/views/formrenderers.py
web/views/igeocodable.py
web/views/plots.py
web/views/tableview.py
web/views/timeline.py
web/views/treeview.py
--- a/devtools/testlib.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/devtools/testlib.py	Thu Apr 22 19:48:04 2010 +0000
@@ -14,7 +14,10 @@
 from math import log
 from contextlib import contextmanager
 
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 import yams.schema
 
@@ -483,7 +486,7 @@
 
     def remote_call(self, fname, *args):
         """remote json call simulation"""
-        dump = simplejson.dumps
+        dump = json.dumps
         args = [dump(arg) for arg in args]
         req = self.request(fname=fname, pageid='123', arg=args)
         ctrl = self.vreg['controllers'].select('json', req)
--- a/goa/goactl.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/goa/goactl.py	Thu Apr 22 19:48:04 2010 +0000
@@ -17,7 +17,11 @@
 
 
 def slink_directories():
-    import rql, yams, yapps, simplejson, docutils, roman
+    import rql, yams, yapps, docutils, roman
+    try:
+        import json as simplejson
+    except ImportError:
+        import simplejson
     from logilab import common as lgc
     from logilab import constraint as lgcstr
     from logilab import mtconverter as lgmtc
--- a/test/unittest_utils.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/test/unittest_utils.py	Thu Apr 22 19:48:04 2010 +0000
@@ -14,10 +14,13 @@
 from cubicweb.utils import make_uid, UStringIO, SizeConstrainedList, RepeatList
 
 try:
-    import simplejson
+    try:
+        import json 
+    except ImportError:
+        import simplejson as json
     from cubicweb.utils import CubicWebJsonEncoder
 except ImportError:
-    simplejson = None
+    json = None
 
 class MakeUidTC(TestCase):
     def test_1(self):
@@ -107,11 +110,11 @@
 
 class JSONEncoderTC(TestCase):
     def setUp(self):
-        if simplejson is None:
-            self.skip('simplejson not available')
+        if json is None:
+            self.skip('json not available')
 
     def encode(self, value):
-        return simplejson.dumps(value, cls=CubicWebJsonEncoder)
+        return json.dumps(value, cls=CubicWebJsonEncoder)
 
     def test_encoding_dates(self):
         self.assertEquals(self.encode(datetime.datetime(2009, 9, 9, 20, 30)),
--- a/utils.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/utils.py	Thu Apr 22 19:48:04 2010 +0000
@@ -339,14 +339,17 @@
     return __answer_cache[0]
 
 try:
-    # may not be there if cubicweb-web not installed
-    from simplejson import dumps, JSONEncoder
+    try:
+        # may not be there if cubicweb-web not installed
+        from json import dumps, JSONEncoder
+    except ImportError:
+        from simplejson import dumps, JSONEncoder
 except ImportError:
     pass
 else:
 
     class CubicWebJsonEncoder(JSONEncoder):
-        """define a simplejson encoder to be able to encode yams std types"""
+        """define a json encoder to be able to encode yams std types"""
 
         # _iterencode is the only entry point I've found to use a custom encode
         # hook early enough: .default() is called if nothing else matched before,
--- a/view.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/view.py	Thu Apr 22 19:48:04 2010 +0000
@@ -12,7 +12,10 @@
 from cStringIO import StringIO
 from warnings import warn
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from logilab.common.deprecation import deprecated
 from logilab.mtconverter import xml_escape
--- a/web/__init__.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/__init__.py	Thu Apr 22 19:48:04 2010 +0000
@@ -10,7 +10,10 @@
 __docformat__ = "restructuredtext en"
 _ = unicode
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 from urllib import quote as urlquote
 
 from logilab.common.deprecation import deprecated
--- a/web/_exceptions.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/_exceptions.py	Thu Apr 22 19:48:04 2010 +0000
@@ -57,5 +57,8 @@
         self.reason = reason
 
     def dumps(self):
-        import simplejson
-        return simplejson.dumps({'reason': self.reason})
+        try:
+            from json import dumps
+        except ImportError:
+            from simplejson import dumps
+        return dumps({'reason': self.reason})
--- a/web/application.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/application.py	Thu Apr 22 19:48:04 2010 +0000
@@ -385,6 +385,9 @@
                 self.error_handler(req, ex, tb=False)
             except Exception, ex:
                 self.error_handler(req, ex, tb=True)
+            except:
+                self.critical('Catch all triggered!!!')
+                self.exception('this is what happened')
         finally:
             if req.cnx is not None:
                 try:
--- a/web/component.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/component.py	Thu Apr 22 19:48:04 2010 +0000
@@ -8,7 +8,10 @@
 __docformat__ = "restructuredtext en"
 _ = unicode
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from logilab.common.deprecation import class_renamed
 from logilab.mtconverter import xml_escape
--- a/web/formwidgets.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/formwidgets.py	Thu Apr 22 19:48:04 2010 +0000
@@ -515,7 +515,6 @@
     @classmethod
     def add_localized_infos(cls, req):
         """inserts JS variables defining localized months and days"""
-        # import here to avoid dependancy from cubicweb to simplejson
         _ = req._
         monthnames = [_(mname) for mname in cls.monthnames]
         daynames = [_(dname) for dname in cls.daynames]
--- a/web/request.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/request.py	Thu Apr 22 19:48:04 2010 +0000
@@ -16,7 +16,10 @@
 from urlparse import urlsplit
 from itertools import count
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from rql.utils import rqlvar_maker
 
--- a/web/test/unittest_views_basecontrollers.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/test/unittest_views_basecontrollers.py	Thu Apr 22 19:48:04 2010 +0000
@@ -5,7 +5,10 @@
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
 """
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 from logilab.common.testlib import unittest_main, mock_object
 
@@ -550,7 +553,7 @@
 #         rql = 'Any T,N WHERE T is Tag, T name N'
 #         ctrl = self.ctrl(self.request(mode='json', rql=rql, pageid='123'))
 #         self.assertEquals(ctrl.publish(),
-#                           simplejson.dumps(self.execute(rql).rows))
+#                           json.dumps(self.execute(rql).rows))
 
     def test_remote_add_existing_tag(self):
         self.remote_call('tag_entity', self.john.eid, ['python'])
@@ -627,14 +630,14 @@
     # silly tests
     def test_external_resource(self):
         self.assertEquals(self.remote_call('external_resource', 'RSS_LOGO')[0],
-                          simplejson.dumps(self.request().external_resource('RSS_LOGO')))
+                          json.dumps(self.request().external_resource('RSS_LOGO')))
     def test_i18n(self):
         self.assertEquals(self.remote_call('i18n', ['bimboom'])[0],
-                          simplejson.dumps(['bimboom']))
+                          json.dumps(['bimboom']))
 
     def test_format_date(self):
         self.assertEquals(self.remote_call('format_date', '2007-01-01 12:00:00')[0],
-                          simplejson.dumps('2007/01/01'))
+                          json.dumps('2007/01/01'))
 
 
 
--- a/web/test/unittest_views_baseviews.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/test/unittest_views_baseviews.py	Thu Apr 22 19:48:04 2010 +0000
@@ -5,7 +5,10 @@
 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
 """
-from simplejson import loads
+try:
+    from json import loads
+except ImportError:
+    from simplejson import loads
 
 from logilab.common.testlib import unittest_main
 from logilab.mtconverter import html_unescape
--- a/web/views/autoform.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/autoform.py	Thu Apr 22 19:48:04 2010 +0000
@@ -109,7 +109,10 @@
 
 from warnings import warn
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from logilab.mtconverter import xml_escape
 from logilab.common.decorators import iclassmethod, cached
--- a/web/views/basecontrollers.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/basecontrollers.py	Thu Apr 22 19:48:04 2010 +0000
@@ -12,7 +12,10 @@
 
 from smtplib import SMTP
 
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 from logilab.common.decorators import cached
 from logilab.common.date import strptime
@@ -34,7 +37,7 @@
     HAS_SEARCH_RESTRICTION = False
 
 def jsonize(func):
-    """decorator to sets correct content_type and calls `simplejson.dumps` on
+    """decorator to sets correct content_type and calls `json.dumps` on
     results
     """
     def wrapper(self, *args, **kwargs):
@@ -236,7 +239,7 @@
         errback = str(self._cw.form.get('__onfailure', 'null'))
         cbargs = str(self._cw.form.get('__cbargs', 'null'))
         self._cw.set_content_type('text/html')
-        jsargs = simplejson.dumps((status, args, entity), cls=CubicWebJsonEncoder)
+        jsargs = json.dumps((status, args, entity), cls=CubicWebJsonEncoder)
         return """<script type="text/javascript">
  wp = window.parent;
  window.parent.handleFormValidationResponse('%s', %s, %s, %s, %s);
@@ -277,7 +280,7 @@
         if not isinstance(args, (list, tuple)):
             args = (args,)
         try:
-            args = [simplejson.loads(arg) for arg in args]
+            args = [json.loads(arg) for arg in args]
         except ValueError, exc:
             self.exception('error while decoding json arguments for js_%s: %s', fname, args, exc)
             raise RemoteCallFailed(repr(exc))
@@ -441,7 +444,7 @@
         entity = self._cw.entity_from_eid(int(self._cw.form['eid']))
         # note: default is reserved in js land
         args['default'] = self._cw.form['default_value']
-        args['reload'] = simplejson.loads(args['reload'])
+        args['reload'] = json.loads(args['reload'])
         rset = req.eid_rset(int(self._cw.form['eid']))
         view = req.vreg['views'].select('doreledit', req, rset=rset, rtype=args['rtype'])
         stream = view.set_stream()
--- a/web/views/editforms.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/editforms.py	Thu Apr 22 19:48:04 2010 +0000
@@ -11,7 +11,10 @@
 
 from copy import copy
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from logilab.mtconverter import xml_escape
 from logilab.common.decorators import cached
--- a/web/views/facets.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/facets.py	Thu Apr 22 19:48:04 2010 +0000
@@ -7,7 +7,10 @@
 """
 __docformat__ = "restructuredtext en"
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from logilab.mtconverter import xml_escape
 
--- a/web/views/formrenderers.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/formrenderers.py	Thu Apr 22 19:48:04 2010 +0000
@@ -25,7 +25,10 @@
 from logilab.common import dictattr
 from logilab.mtconverter import xml_escape
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from cubicweb import tags
 from cubicweb.appobject import AppObject
--- a/web/views/igeocodable.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/igeocodable.py	Thu Apr 22 19:48:04 2010 +0000
@@ -7,7 +7,10 @@
 """
 __docformat__ = "restructuredtext en"
 
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 from cubicweb.interfaces import IGeocodable
 from cubicweb.view import EntityView
@@ -39,7 +42,7 @@
             'center': center,
             'markers': markers,
             }
-        self.w(simplejson.dumps(geodata))
+        self.w(json.dumps(geodata))
 
     def build_marker_data(self, row, extraparams):
         entity = self.cw_rset.get_entity(row, 0)
--- a/web/views/plots.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/plots.py	Thu Apr 22 19:48:04 2010 +0000
@@ -7,7 +7,10 @@
 """
 __docformat__ = "restructuredtext en"
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from logilab.common.date import datetime2ticks
 from logilab.mtconverter import xml_escape
--- a/web/views/tableview.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/tableview.py	Thu Apr 22 19:48:04 2010 +0000
@@ -8,7 +8,10 @@
 """
 __docformat__ = "restructuredtext en"
 
-from simplejson import dumps
+try:
+    from json import dumps
+except ImportError:
+    from simplejson import dumps
 
 from logilab.mtconverter import xml_escape
 
--- a/web/views/timeline.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/timeline.py	Thu Apr 22 19:48:04 2010 +0000
@@ -9,7 +9,10 @@
 """
 __docformat__ = "restructuredtext en"
 
-import simplejson
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 from logilab.mtconverter import xml_escape
 
@@ -40,7 +43,7 @@
                 events.append(event)
         timeline_data = {'dateTimeFormat': self.date_fmt,
                          'events': events}
-        self.w(simplejson.dumps(timeline_data))
+        self.w(json.dumps(timeline_data))
 
     # FIXME: those properties should be defined by the entity class
     def onclick_url(self, entity):
--- a/web/views/treeview.py	Thu Apr 22 19:37:56 2010 +0000
+++ b/web/views/treeview.py	Thu Apr 22 19:48:04 2010 +0000
@@ -7,7 +7,10 @@
 """
 __docformat__ = "restructuredtext en"
 
-import simplejson as json
+try:
+    import json
+except ImportError:
+    import simplejson as json
 
 from logilab.mtconverter import xml_escape
 from cubicweb.utils import make_uid