From cdeda3b6f852ddf3610e3b356eaaed9cc71ae1f9 Mon Sep 17 00:00:00 2001 From: Michael Hanselmann <hansmi@google.com> Date: Mon, 26 Sep 2011 11:19:16 +0200 Subject: [PATCH] =?UTF-8?q?Revert=20"Fail=20if=20dictionary=20uses=20inval?= =?UTF-8?q?id=20keys"=20and=20"Support=20newer=20=E2=80=9Cjson=E2=80=9D=20?= =?UTF-8?q?module"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit fd0351aef246f5d36e641209429e2ec093d325f8 and 9869e771704ada62bab001e729c52a36525ef081. The built-in module is a lot slower in Python 2.6. Signed-off-by: Michael Hanselmann <hansmi@google.com> Reviewed-by: Iustin Pop <iustin@google.com> --- lib/serializer.py | 32 +++--------------------------- test/ganeti.serializer_unittest.py | 21 -------------------- 2 files changed, 3 insertions(+), 50 deletions(-) diff --git a/lib/serializer.py b/lib/serializer.py index 090ae00b6..ff27261aa 100644 --- a/lib/serializer.py +++ b/lib/serializer.py @@ -29,18 +29,8 @@ backend (currently json). # C0103: Invalid name, since pylint doesn't see that Dump points to a # function and not a constant -_OLD_SIMPLEJSON = False - -try: - import json -except ImportError: - # The "json" module was only added in Python 2.6. Earlier versions must use - # the separate "simplejson" module. - import simplejson as json - _OLD_SIMPLEJSON = True - +import simplejson import re -import logging from ganeti import errors from ganeti import utils @@ -51,23 +41,7 @@ _JSON_INDENT = 2 _RE_EOLSP = re.compile("[ \t]+$", re.MULTILINE) -class _CustomJsonEncoder(json.JSONEncoder): - if __debug__ and not _OLD_SIMPLEJSON: - try: - _orig_fn = json.JSONEncoder._iterencode_dict - except AttributeError: - raise Exception("Can't override JSONEncoder's '_iterencode_dict'") - else: - def _iterencode_dict(self, data, *args, **kwargs): - for key in data.keys(): - if not (key is None or isinstance(key, (basestring, bool))): - raise ValueError("Key '%s' is of disallowed type '%s'" % - (key, type(key))) - - return self._orig_fn(data, *args, **kwargs) - - -def _GetJsonDumpers(_encoder_class=_CustomJsonEncoder): +def _GetJsonDumpers(_encoder_class=simplejson.JSONEncoder): """Returns two JSON functions to serialize data. @rtype: (callable, callable) @@ -119,7 +93,7 @@ def LoadJson(txt): @return: the original data """ - return json.loads(txt) + return simplejson.loads(txt) def DumpSignedJson(data, key, salt=None, key_selector=None): diff --git a/test/ganeti.serializer_unittest.py b/test/ganeti.serializer_unittest.py index 3e68efaa6..9d8d65680 100755 --- a/test/ganeti.serializer_unittest.py +++ b/test/ganeti.serializer_unittest.py @@ -23,11 +23,9 @@ import unittest -import warnings from ganeti import serializer from ganeti import errors -from ganeti import compat import testutils @@ -109,24 +107,5 @@ class TestSerializer(testutils.GanetiTestCase): serializer.DumpJson(tdata), "mykey") -class TestInvalidDictionaryKey(unittest.TestCase): - def _Test(self, data): - if serializer._OLD_SIMPLEJSON: - # Using old "simplejson", can't really test - warnings.warn("This test requires Python 2.6 or above to function" - " correctly") - self.assertTrue(serializer.DumpJson(data)) - else: - self.assertRaises(ValueError, serializer.DumpJson, data) - - def test(self): - for value in [123, 1.1, -1, -9492.1123, -3234e-4]: - self._Test({value: ""}) - - def testAllowed(self): - for value in ["", "Hello World", None, True, False]: - self.assertTrue(serializer.DumpJson({value: ""})) - - if __name__ == '__main__': testutils.GanetiTestProgram() -- GitLab