Merge "Fix json to_primitive when using IO OBjects"
This commit is contained in:
commit
8445e61e93
@ -33,6 +33,7 @@ import codecs
|
|||||||
import datetime
|
import datetime
|
||||||
import functools
|
import functools
|
||||||
import inspect
|
import inspect
|
||||||
|
import io
|
||||||
import itertools
|
import itertools
|
||||||
import json
|
import json
|
||||||
import uuid
|
import uuid
|
||||||
@ -161,7 +162,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
|
|||||||
# Python 3 does not have iteritems
|
# Python 3 does not have iteritems
|
||||||
elif hasattr(value, 'items'):
|
elif hasattr(value, 'items'):
|
||||||
return recursive(dict(value.items()), level=level + 1)
|
return recursive(dict(value.items()), level=level + 1)
|
||||||
elif hasattr(value, '__iter__'):
|
elif hasattr(value, '__iter__') and not isinstance(value, io.IOBase):
|
||||||
return list(map(recursive, value))
|
return list(map(recursive, value))
|
||||||
elif convert_instances and hasattr(value, '__dict__'):
|
elif convert_instances and hasattr(value, '__dict__'):
|
||||||
# Likely an instance of something. Watch for cycles.
|
# Likely an instance of something. Watch for cycles.
|
||||||
|
@ -401,6 +401,16 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
|
|||||||
ret = jsonutils.to_primitive(obj, fallback=lambda _: 'fallback')
|
ret = jsonutils.to_primitive(obj, fallback=lambda _: 'fallback')
|
||||||
self.assertEqual('fallback', ret)
|
self.assertEqual('fallback', ret)
|
||||||
|
|
||||||
|
def test_fallback_typeerror_IO_object(self):
|
||||||
|
# IO Objects are not callable, cause a TypeError in to_primitive()
|
||||||
|
obj = io.IOBase
|
||||||
|
|
||||||
|
ret = jsonutils.to_primitive(obj)
|
||||||
|
self.assertEqual(str(obj), ret)
|
||||||
|
|
||||||
|
ret = jsonutils.to_primitive(obj, fallback=lambda _: 'fallback')
|
||||||
|
self.assertEqual('fallback', ret)
|
||||||
|
|
||||||
def test_exception(self):
|
def test_exception(self):
|
||||||
self.assertIn(jsonutils.to_primitive(ValueError("an exception")),
|
self.assertIn(jsonutils.to_primitive(ValueError("an exception")),
|
||||||
["ValueError('an exception',)",
|
["ValueError('an exception',)",
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
fixes:
|
||||||
|
- |
|
||||||
|
`Bug #1908607 <https://bugs.launchpad.net/cinder/+bug/1908607>`_: Fix
|
||||||
|
json to_primitive when using IO OBjects.
|
Loading…
x
Reference in New Issue
Block a user