# Buggy code
def _get_template_texts(
    source_list=None, template="jinja", defaults=None, context=None, **kwargs
):
    """
    Iterate a list of sources and process them as templates.
    Returns a list of 'chunks' containing the rendered templates.
    """

    ret = {
        "name": "_get_template_texts",
        "changes": {},
        "result": True,
        "comment": "",
        "data": [],
    }

    if source_list is None:
        return _error(ret, "_get_template_texts called with empty source_list")

    txtl = []

    for source, source_hash in source_list:
        tmpctx = defaults if defaults else {}
        if context:
            tmpctx.update(context)
        rndrd_templ_fn = __salt__["cp.get_template"](
            source, "", template=template, saltenv=__env__, context=tmpctx, **kwargs
        )
        msg = "cp.get_template returned {0} (Called with: {1})"
        log.debug(msg.format(rndrd_templ_fn, source))
        if rndrd_templ_fn:
            tmplines = None
            with salt.utils.files.fopen(rndrd_templ_fn, "rb") as fp_:
                tmplines = fp_.read()
                tmplines = tmplines.decode(__salt_system_encoding__)
                tmplines = tmplines.splitlines(True)
            if not tmplines:
                msg = "Failed to read rendered template file {0} ({1})"
                log.debug(msg.format(rndrd_templ_fn, source))
                ret["name"] = source
                return _error(ret, msg.format(rndrd_templ_fn, source))
            txtl.append("".join(tmplines))
        else:
            msg = "Failed to load template file {0}".format(source)
            log.debug(msg)
            ret["name"] = source
            return _error(ret, msg)

    ret["data"] = txtl
    return ret


# Traceback (UnicodeDecodeError)
local:
----------
ID: somefile-exists
Function: file.managed
Name: /home/werner/somefile
Result: True
Comment: Empty file
Started: 21:57:12.447617
Duration: 71.766 ms
Changes:
----------
new:
file /home/werner/somefile created
----------
ID: somefile-blockreplace
Function: file.blockreplace
Name: /home/werner/somefile
Result: False
Comment: An exception occurred in this state: Traceback (most recent call last):
File "/home/werner/git/saltstack/salt/state.py", line 1905, in call
**cdata['kwargs'])
File "/home/werner/git/saltstack/salt/loader.py", line 1830, in wrapper
return f(*args, **kwargs)
File "/home/werner/git/saltstack/salt/states/file.py", line 4391, in blockreplace
context=context)
File "/home/werner/git/saltstack/salt/states/file.py", line 1116, in _get_template_texts
txtl.append(''.join(tmplines))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)
Started: 21:57:12.520045
Duration: 69.739 ms
Changes:

Summary for local
------------
Succeeded: 1 (changed=1)
Failed:    1
------------
Total states run:     2
Total run time: 141.505 ms

# Fixed code
def _get_template_texts(
    source_list=None, template="jinja", defaults=None, context=None, **kwargs
):
    """
    Iterate a list of sources and process them as templates.
    Returns a list of 'chunks' containing the rendered templates.
    """

    ret = {
        "name": "_get_template_texts",
        "changes": {},
        "result": True,
        "comment": "",
        "data": [],
    }

    if source_list is None:
        return _error(ret, "_get_template_texts called with empty source_list")

    txtl = []

    for source, source_hash in source_list:
        tmpctx = defaults if defaults else {}
        if context:
            tmpctx.update(context)
        rndrd_templ_fn = __salt__["cp.get_template"](
            source, "", template=template, saltenv=__env__, context=tmpctx, **kwargs
        )
        msg = "cp.get_template returned {0} (Called with: {1})"
        log.debug(msg.format(rndrd_templ_fn, source))
        if rndrd_templ_fn:
            tmplines = None
            with salt.utils.files.fopen(rndrd_templ_fn, "rb") as fp_:
                tmplines = fp_.read()
                tmplines = salt.utils.stringutils.to_unicode(tmplines)
                tmplines = tmplines.splitlines(True)
            if not tmplines:
                msg = "Failed to read rendered template file {0} ({1})"
                log.debug(msg.format(rndrd_templ_fn, source))
                ret["name"] = source
                return _error(ret, msg.format(rndrd_templ_fn, source))
            txtl.append("".join(tmplines))
        else:
            msg = "Failed to load template file {0}".format(source)
            log.debug(msg)
            ret["name"] = source
            return _error(ret, msg)

    ret["data"] = txtl
    return ret