Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenStack
oslo
python-oslo.serialization
Commits
8445e61e
Commit
8445e61e
authored
Jan 19, 2021
by
Zuul
Committed by
Gerrit Code Review
Jan 19, 2021
Browse files
Merge "Fix json to_primitive when using IO OBjects"
parents
54e079c8
02037330
Changes
3
Hide whitespace changes
Inline
Side-by-side
oslo_serialization/jsonutils.py
View file @
8445e61e
...
...
@@ -33,6 +33,7 @@ import codecs
import
datetime
import
functools
import
inspect
import
io
import
itertools
import
json
import
uuid
...
...
@@ -161,7 +162,7 @@ def to_primitive(value, convert_instances=False, convert_datetime=True,
# Python 3 does not have iteritems
elif
hasattr
(
value
,
'items'
):
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
))
elif
convert_instances
and
hasattr
(
value
,
'__dict__'
):
# Likely an instance of something. Watch for cycles.
...
...
oslo_serialization/tests/test_jsonutils.py
View file @
8445e61e
...
...
@@ -401,6 +401,16 @@ class ToPrimitiveTestCase(test_base.BaseTestCase):
ret
=
jsonutils
.
to_primitive
(
obj
,
fallback
=
lambda
_
:
'fallback'
)
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
):
self
.
assertIn
(
jsonutils
.
to_primitive
(
ValueError
(
"an exception"
)),
[
"ValueError('an exception',)"
,
...
...
releasenotes/notes/bug-1908607-fix-json-to_primitive-IO-OBjects-04faff4a1b5cf48f.yaml
0 → 100644
View file @
8445e61e
---
fixes
:
-
|
`Bug #1908607 <https://bugs.launchpad.net/cinder/+bug/1908607>`_: Fix
json to_primitive when using IO OBjects.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment