Use default SLS encoding, fall back to system encoding

This commit is contained in:
Daniel A. Wozniak 2018-03-22 14:30:30 -07:00
parent 6548d550d0
commit 9f95c50061
No known key found for this signature in database
GPG key ID: 166B9D2C06C82D61
2 changed files with 11 additions and 5 deletions

View file

@ -288,7 +288,7 @@ def _get_jinja_error(trace, context=None):
return line, out
def render_jinja_tmpl(tmplstr, context, tmplpath=None, encoding=None):
def render_jinja_tmpl(tmplstr, context, tmplpath=None, encoding=SLS_ENCODING):
opts = context['opts']
saltenv = context['saltenv']
loader = None
@ -296,7 +296,7 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None, encoding=None):
if tmplstr and not isinstance(tmplstr, six.text_type):
# http://jinja.pocoo.org/docs/api/#unicode
tmplstr = tmplstr.decode(SLS_ENCODING)
tmplstr = tmplstr.decode(encoding)
if tmplstr.endswith(os.linesep):
newline = True
@ -410,7 +410,14 @@ def render_jinja_tmpl(tmplstr, context, tmplpath=None, encoding=None):
decoded_context[key] = value
continue
decoded_context[key] = salt.utils.to_unicode(value, encoding=encoding)
try:
decoded_context[key] = salt.utils.to_unicode(value, encoding=encoding)
except UnicodeDecodeError as ex:
log.debug(
"Failed to decode using default encoding (%s), trying system encoding",
encoding,
)
decoded_context[key] = salt.utils.locales.sdecode(value)
try:
template = jinja_env.from_string(tmplstr)

View file

@ -303,8 +303,7 @@ class TestGetTemplate(TestCase):
dict(opts={'cachedir': TEMPLATES_DIR, 'file_client': 'remote',
'file_roots': self.local_opts['file_roots'],
'pillar_roots': self.local_opts['pillar_roots']},
a='Hi', b='Sàlt', saltenv='test', salt=self.local_salt),
encoding='utf-8')
a='Hi', b='Sàlt', saltenv='test', salt=self.local_salt))
self.assertEqual(out, u'Hey world !Hi Sàlt !' + os.linesep)
self.assertEqual(fc.requests[0]['path'], 'salt://macro')