summaryrefslogtreecommitdiff
path: root/cloudinit/templater.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2018-04-12 15:51:07 -0600
committerChad Smith <chad.smith@canonical.com>2018-04-12 15:51:07 -0600
commit4b86ab9a25b512420ecfe98953a3f3a6e4b4bba1 (patch)
treeadeb2faf312a92bfb2a76a3a4557d841d7d95d24 /cloudinit/templater.py
parentc6dff581a9c253170d5e3f12fb83d16a8dec8257 (diff)
downloadcloud-init-git-4b86ab9a25b512420ecfe98953a3f3a6e4b4bba1.tar.gz
renderer: support unicode in render_from_file.
If a file passed to render_from_file had non-ascii text then jinja in python2 would decode as ascii, which would cause UnicodeDecodeError. This issue can be re-created in python2 with just: 'can\xe2\x80\x99t'.decode() The solution here is to explicitly pass in unicode supporting type (py3 str, py2 unicode). Those are six.text_type. Then jinja does not try to decode. The reason we hit this is that load_file calls decode_binary. decode_binary believes it has no work to do if it got a six.string_types. isinstance('can\xe2\x80\x99t', six.string_types) == True So it returns the original string which will blow up for jinja. Our fix here then is to load the file in binary mode and explicitly decode it to utf-8. Then in python2 we'll have a unicode type and in python3 we'll have a string type.
Diffstat (limited to 'cloudinit/templater.py')
-rw-r--r--cloudinit/templater.py10
1 files changed, 9 insertions, 1 deletions
diff --git a/cloudinit/templater.py b/cloudinit/templater.py
index b3ea64e4..9a087e1c 100644
--- a/cloudinit/templater.py
+++ b/cloudinit/templater.py
@@ -121,7 +121,11 @@ def detect_template(text):
def render_from_file(fn, params):
if not params:
params = {}
- template_type, renderer, content = detect_template(util.load_file(fn))
+ # jinja in python2 uses unicode internally. All py2 str will be decoded.
+ # If it is given a str that has non-ascii then it will raise a
+ # UnicodeDecodeError. So we explicitly convert to unicode type here.
+ template_type, renderer, content = detect_template(
+ util.load_file(fn, decode=False).decode('utf-8'))
LOG.debug("Rendering content of '%s' using renderer %s", fn, template_type)
return renderer(content, params)
@@ -132,11 +136,15 @@ def render_to_file(fn, outfn, params, mode=0o644):
def render_string_to_file(content, outfn, params, mode=0o644):
+ """Render string (or py2 unicode) to file.
+ Warning: py2 str with non-ascii chars will cause UnicodeDecodeError."""
contents = render_string(content, params)
util.write_file(outfn, contents, mode=mode)
def render_string(content, params):
+ """Render string (or py2 unicode).
+ Warning: py2 str with non-ascii chars will cause UnicodeDecodeError."""
if not params:
params = {}
template_type, renderer, content = detect_template(content)