Always use jsonutils.to_primitive 'fallback' parameter

This was optionally being enabled to handle the case where we did not
have oslo.serialization 2.21.1. Our minimum version is now 2.25.0 so
this will never be the case. Remove this optionality logic.

Change-Id: Ib61e1fd02a5ec98ddcaed2ce42f049636598870f
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2019-12-10 10:55:14 +00:00
parent b52c8cd247
commit f8283aa222
2 changed files with 7 additions and 23 deletions

View File

@ -34,15 +34,6 @@ if six.PY3:
from functools import reduce
try:
# Test if to_primitive() has the fallback parameter added
# in oslo.serialization 2.21.1
jsonutils.to_primitive(1, fallback=repr)
_HAVE_JSONUTILS_FALLBACK = True
except TypeError:
_HAVE_JSONUTILS_FALLBACK = False
def _dictify_context(context):
if getattr(context, 'get_logging_values', None):
return context.get_logging_values()
@ -185,16 +176,13 @@ _MSG_KEY_REGEX = re.compile(r'(%+)\((\w+)\)')
def _json_dumps_with_fallback(obj):
if _HAVE_JSONUTILS_FALLBACK:
# Bug #1593641: If an object cannot be serialized to JSON, convert
# it using repr() to prevent serialization errors. Using repr() is
# not ideal, but serialization errors are unexpected on logs,
# especially when the code using logs is not aware that the
# JSONFormatter will be used.
convert = functools.partial(jsonutils.to_primitive, fallback=repr)
return jsonutils.dumps(obj, default=convert)
else:
return jsonutils.dumps(obj)
# Bug #1593641: If an object cannot be serialized to JSON, convert
# it using repr() to prevent serialization errors. Using repr() is
# not ideal, but serialization errors are unexpected on logs,
# especially when the code using logs is not aware that the
# JSONFormatter will be used.
convert = functools.partial(jsonutils.to_primitive, fallback=repr)
return jsonutils.dumps(obj, default=convert)
class JSONFormatter(logging.Formatter):

View File

@ -661,10 +661,6 @@ class JSONFormatterTestCase(LogTestBase):
data['error_summary'])
def test_fallback(self):
if not formatters._HAVE_JSONUTILS_FALLBACK:
self.skipTest("need the fallback parameter of "
"jsonutils.to_primitive() added in "
"oslo_serialization 2.21.1")
class MyObject(object):
def __str__(self):