diff options
author | Gevorg Davoian <gdavoian@mirantis.com> | 2016-05-20 15:00:12 +0300 |
---|---|---|
committer | Gevorg Davoian <gdavoian@mirantis.com> | 2016-06-08 08:41:36 +0000 |
commit | 4fdaeff758c3a95cdbf99976b87cad42215307b6 (patch) | |
tree | ceb908472e15f5e22dd769cc858f5f68f8f9394f | |
parent | 8a4cac92bcb70a79de156b2aa07a0b0b9a8b6c78 (diff) | |
download | oslo-serialization-2.8.0.tar.gz |
Replace TypeError by ValueError in msgpackutils2.8.0
There are some differences in the behaviour of json and msgpack
serializers. Msgpack throws TypeError when it doesn't know
how to handle an object. Json throws ValueError instead. Regarding
the fact that serialization should be configurable (in particular,
serializers should have similar behaviour; right now transition from
json to msgpack leads to errors and fails) this patch proposes to
raise ValueError instead of TypeError on failures so that libraries
already using jsonutils would be able to also work with msgpackutils.
Change-Id: I3d21b7d136e5a426a3c4a70a953c82ddcd6ef5af
-rw-r--r-- | oslo_serialization/jsonutils.py | 2 | ||||
-rw-r--r-- | oslo_serialization/msgpackutils.py | 6 | ||||
-rw-r--r-- | oslo_serialization/tests/test_jsonutils.py | 6 | ||||
-rw-r--r-- | oslo_serialization/tests/test_msgpackutils.py | 3 |
4 files changed, 10 insertions, 7 deletions
diff --git a/oslo_serialization/jsonutils.py b/oslo_serialization/jsonutils.py index 0e758c3..cd09c2e 100644 --- a/oslo_serialization/jsonutils.py +++ b/oslo_serialization/jsonutils.py @@ -126,7 +126,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True, return 'mock' if level > max_depth: - return '?' + return None # The try block may not be necessary after the class check above, # but just in case ... diff --git a/oslo_serialization/msgpackutils.py b/oslo_serialization/msgpackutils.py index c891a36..af76c4c 100644 --- a/oslo_serialization/msgpackutils.py +++ b/oslo_serialization/msgpackutils.py @@ -13,7 +13,7 @@ # under the License. ''' -Msgpack related utilities. +MessagePack related utilities. This module provides a few things: @@ -388,8 +388,8 @@ class DateHandler(object): def _serializer(registry, obj): handler = registry.match(obj) if handler is None: - raise TypeError("No serialization handler registered" - " for type '%s'" % (type(obj).__name__)) + raise ValueError("No serialization handler registered" + " for type '%s'" % (type(obj).__name__)) return msgpack.ExtType(handler.identity, handler.serialize(obj)) diff --git a/oslo_serialization/tests/test_jsonutils.py b/oslo_serialization/tests/test_jsonutils.py index 6e0fd3f..be9f10e 100644 --- a/oslo_serialization/tests/test_jsonutils.py +++ b/oslo_serialization/tests/test_jsonutils.py @@ -288,9 +288,9 @@ class ToPrimitiveTestCase(test_base.BaseTestCase): l4_obj = LevelsGenerator(4) - json_l2 = {0: {0: '?'}} - json_l3 = {0: {0: {0: '?'}}} - json_l4 = {0: {0: {0: {0: '?'}}}} + json_l2 = {0: {0: None}} + json_l3 = {0: {0: {0: None}}} + json_l4 = {0: {0: {0: {0: None}}}} ret = jsonutils.to_primitive(l4_obj, max_depth=2) self.assertEqual(ret, json_l2) diff --git a/oslo_serialization/tests/test_msgpackutils.py b/oslo_serialization/tests/test_msgpackutils.py index 2374793..0446477 100644 --- a/oslo_serialization/tests/test_msgpackutils.py +++ b/oslo_serialization/tests/test_msgpackutils.py @@ -209,3 +209,6 @@ class MsgPackUtilsTest(test_base.BaseTestCase): self.assertEqual(255, c.r) self.assertEqual(254, c.g) self.assertEqual(253, c.b) + + def test_object(self): + self.assertRaises(ValueError, msgpackutils.dumps, object()) |