jsonutils: Raise ValueError in case the input can't be converted
According to the warning it was planned that this change was made in 3.0. Change-Id: I0c1011b935f52f5ea1329e0316f935a034fdfd0e
This commit is contained in:
parent
27a8fd66c7
commit
c83a4c9968
@ -37,7 +37,6 @@ import io
|
|||||||
import itertools
|
import itertools
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
import warnings
|
|
||||||
from xmlrpc import client as xmlrpclib
|
from xmlrpc import client as xmlrpclib
|
||||||
|
|
||||||
from oslo_utils import encodeutils
|
from oslo_utils import encodeutils
|
||||||
@ -173,13 +172,10 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
|||||||
# __iter__ defined but it isn't callable as list().
|
# __iter__ defined but it isn't callable as list().
|
||||||
return fallback(value)
|
return fallback(value)
|
||||||
|
|
||||||
if orig_fallback is not None:
|
if orig_fallback is None:
|
||||||
return orig_fallback(value)
|
raise ValueError("Cannot convert %r to primitive" % (value,))
|
||||||
|
|
||||||
# TODO(gcb) raise ValueError in version 3.0
|
return orig_fallback(value)
|
||||||
warnings.warn("Cannot convert %r to primitive, will raise ValueError "
|
|
||||||
"instead of warning in version 3.0" % (value,))
|
|
||||||
return value
|
|
||||||
|
|
||||||
|
|
||||||
JSONEncoder = json.JSONEncoder
|
JSONEncoder = json.JSONEncoder
|
||||||
|
@ -261,8 +261,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
|||||||
p = jsonutils.to_primitive(x)
|
p = jsonutils.to_primitive(x)
|
||||||
self.assertEqual({'a': 1, 'b': 2, 'c': 3}, p)
|
self.assertEqual({'a': 1, 'b': 2, 'c': 3}, p)
|
||||||
|
|
||||||
@mock.patch('warnings.warn')
|
def test_instance(self):
|
||||||
def test_instance(self, warn_mock):
|
|
||||||
class MysteryClass(object):
|
class MysteryClass(object):
|
||||||
a = 10
|
a = 10
|
||||||
|
|
||||||
@ -273,8 +272,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
|||||||
self.assertEqual(dict(b=1),
|
self.assertEqual(dict(b=1),
|
||||||
jsonutils.to_primitive(x, convert_instances=True))
|
jsonutils.to_primitive(x, convert_instances=True))
|
||||||
|
|
||||||
self.assertEqual(x, jsonutils.to_primitive(x))
|
self.assertRaises(ValueError, jsonutils.to_primitive, x)
|
||||||
warn_mock.assert_called_once()
|
|
||||||
|
|
||||||
def test_typeerror(self):
|
def test_typeerror(self):
|
||||||
x = bytearray # Class, not instance
|
x = bytearray # Class, not instance
|
||||||
@ -354,8 +352,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
|||||||
def test_fallback(self):
|
def test_fallback(self):
|
||||||
obj = ReprObject()
|
obj = ReprObject()
|
||||||
|
|
||||||
ret = jsonutils.to_primitive(obj)
|
self.assertRaises(ValueError, jsonutils.to_primitive, obj)
|
||||||
self.assertIs(obj, ret)
|
|
||||||
|
|
||||||
ret = jsonutils.to_primitive(obj, fallback=repr)
|
ret = jsonutils.to_primitive(obj, fallback=repr)
|
||||||
self.assertEqual('repr', ret)
|
self.assertEqual('repr', ret)
|
||||||
@ -364,8 +361,7 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
|||||||
obj = ReprObject()
|
obj = ReprObject()
|
||||||
obj_list = [obj]
|
obj_list = [obj]
|
||||||
|
|
||||||
ret = jsonutils.to_primitive(obj_list)
|
self.assertRaises(ValueError, jsonutils.to_primitive, obj_list)
|
||||||
self.assertEqual([obj], ret)
|
|
||||||
|
|
||||||
ret = jsonutils.to_primitive(obj_list, fallback=repr)
|
ret = jsonutils.to_primitive(obj_list, fallback=repr)
|
||||||
self.assertEqual(['repr'], ret)
|
self.assertEqual(['repr'], ret)
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
upgrade:
|
||||||
|
- |
|
||||||
|
The ``oslo_utils.jsonutils.to_primitive`` function now raises ValueError
|
||||||
|
when the input value can't be converted.
|
Loading…
x
Reference in New Issue
Block a user