Ensure that we don't feed jinja2.Markup() a str type

If the dumped contents contain non-ascii unicode, this will cause a
UnicodeDecodeError.
This commit is contained in:
Erik Johnson 2018-08-14 15:51:41 -05:00
parent b412bff534
commit f5f16cf483
No known key found for this signature in database
GPG Key ID: 5E5583C437808F3F

View File

@ -817,14 +817,21 @@ class SerializerExtension(Extension, object):
return explore(data)
def format_json(self, value, sort_keys=True, indent=None):
return Markup(salt.utils.json.dumps(value, sort_keys=sort_keys, indent=indent).strip())
json_txt = salt.utils.json.dumps(value, sort_keys=sort_keys, indent=indent).strip()
try:
return Markup(json_txt)
except UnicodeDecodeError:
return Markup(salt.utils.stringutils.to_unicode(json_txt))
def format_yaml(self, value, flow_style=True):
yaml_txt = salt.utils.yaml.safe_dump(
value, default_flow_style=flow_style).strip()
if yaml_txt.endswith('\n...'):
if yaml_txt.endswith(str('\n...')): # future lint: disable=blacklisted-function
yaml_txt = yaml_txt[:len(yaml_txt)-4]
return Markup(yaml_txt)
try:
return Markup(yaml_txt)
except UnicodeDecodeError:
return Markup(salt.utils.stringutils.to_unicode(yaml_txt))
def format_xml(self, value):
"""Render a formatted multi-line XML string from a complex Python