# Buggy code
def apply_template_on_contents(contents, template, context, defaults, saltenv):
    """
    Return the contents after applying the templating engine

    contents
        template string

    template
        template format

    context
        Overrides default context variables passed to the template.

    defaults
        Default context passed to the template.

    CLI Example:

    .. code-block:: bash

        salt '*' file.apply_template_on_contents \\
            contents='This is a {{ template }} string.' \\
            template=jinja \\
            "context={}" "defaults={'template': 'cool'}" \\
            saltenv=base
    """
    if template in salt.utils.templates.TEMPLATE_REGISTRY:
        context_dict = defaults if defaults else {}
        if context:
            context_dict.update(context)
        # Apply templating
        contents = salt.utils.templates.TEMPLATE_REGISTRY[template](
            contents,
            from_str=True,
            to_str=True,
            context=context_dict,
            saltenv=saltenv,
            grains=__opts__["grains"],
            pillar=__pillar__,
            salt=__salt__,
            opts=__opts__,
        )["data"].encode("utf-8")
    else:
        ret = {}
        ret["result"] = False
        ret["comment"] = ("Specified template format {0} is not supported").format(
            template
        )
        return ret
    return contents


# Traceback (TypeError)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Transplanting configuration files to '/private/tmp/salt-tests-tmpdir/config'
* Current Directory: /Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace
* Test suite is running under PID 46580
* Logging tests on /private/tmp/salt-runtests.log
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Starting unit.modules.file_test Tests
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...........F........EEEEEEEEEE.
======================================================================
ERROR: test_backup (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 130, in test_backup
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=fext)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_dry_run (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 145, in test_dry_run
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', dry_run=True)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_nobackup (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 139, in test_nobackup
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_noshow_changes (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 160, in test_noshow_changes
show_changes=False)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_numeric_repl (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 181, in test_numeric_repl
filemod.replace(self.tfile.name, r'Etiam', 123)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_re_int_flags (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 171, in test_re_int_flags
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', flags=10)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_re_str_flags (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 168, in test_re_str_flags
flags=['MULTILINE', 'ignorecase'])
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_replace (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 72, in test_replace
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_replace_append_if_not_found (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 92, in test_replace_append_if_not_found
filemod.replace(tfile.name, **args)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
ERROR: test_show_changes (unit.modules.file_test.FileReplaceTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 153, in test_show_changes
show_changes=True)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object

======================================================================
FAIL: test_apply_template_on_contents (unit.modules.file_test.FileModuleTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 644, in test_apply_template_on_contents
self.assertEqual(ret, 'This is a templated file.')
AssertionError: b'This is a templated file.' != 'This is a templated file.'

----------------------------------------------------------------------
Ran 31 tests in 0.328s

FAILED (failures=1, errors=10)

===================================================  Overall Tests Report  ====================================================
*** unit.modules.file_test Tests  *********************************************************************************************
--------  Tests with Errors  -------------------------------------------------------------------------------------------------
-> unit.modules.file_test.FileReplaceTestCase.test_backup  .................................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 130, in test_backup
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=fext)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_dry_run  ................................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 145, in test_dry_run
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', dry_run=True)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_nobackup  ...............................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 139, in test_nobackup
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_noshow_changes  .........................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 160, in test_noshow_changes
show_changes=False)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_numeric_repl  ...........................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 181, in test_numeric_repl
filemod.replace(self.tfile.name, r'Etiam', 123)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_re_int_flags  ...........................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 171, in test_re_int_flags
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', flags=10)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_re_str_flags  ...........................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 168, in test_re_str_flags
flags=['MULTILINE', 'ignorecase'])
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_replace  ................................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 72, in test_replace
filemod.replace(self.tfile.name, r'Etiam', 'Salticus', backup=False)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_replace_append_if_not_found  ............................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 92, in test_replace_append_if_not_found
filemod.replace(tfile.name, **args)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
-> unit.modules.file_test.FileReplaceTestCase.test_show_changes  ...........................................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 153, in test_show_changes
show_changes=True)
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/salt/modules/file.py", line 2041, in replace
result, nrepl = re.subn(cpattern, repl, r_data, count)
File "/Users/aaksenov/.virtualenvs/salt-test/lib/python3.5/re.py", line 193, in subn
return _compile(pattern, flags).subn(repl, string, count)
TypeError: cannot use a string pattern on a bytes-like object
............................................................................................................................
------------------------------------------------------------------------------------------------------------------------------
--------  Failed Tests  ------------------------------------------------------------------------------------------------------
-> unit.modules.file_test.FileModuleTestCase.test_apply_template_on_contents  ..............................................
Traceback (most recent call last):
File "/Users/aaksenov/workspace/salt-bug-hunting/salt-file-replace/tests/unit/modules/file_test.py", line 644, in test_apply_template_on_contents
self.assertEqual(ret, 'This is a templated file.')
AssertionError: b'This is a templated file.' != 'This is a templated file.'
............................................................................................................................
------------------------------------------------------------------------------------------------------------------------------
===============================================================================================================================
FAILED (total=31, skipped=0, passed=20, failures=1, errors=10)
===================================================  Overall Tests Report  ====================================================

# Fixed code
def apply_template_on_contents(contents, template, context, defaults, saltenv):
    """
    Return the contents after applying the templating engine

    contents
        template string

    template
        template format

    context
        Overrides default context variables passed to the template.

    defaults
        Default context passed to the template.

    CLI Example:

    .. code-block:: bash

        salt '*' file.apply_template_on_contents \\
            contents='This is a {{ template }} string.' \\
            template=jinja \\
            "context={}" "defaults={'template': 'cool'}" \\
            saltenv=base
    """
    if template in salt.utils.templates.TEMPLATE_REGISTRY:
        context_dict = defaults if defaults else {}
        if context:
            context_dict.update(context)
        # Apply templating
        contents = salt.utils.templates.TEMPLATE_REGISTRY[template](
            contents,
            from_str=True,
            to_str=True,
            context=context_dict,
            saltenv=saltenv,
            grains=__opts__["grains"],
            pillar=__pillar__,
            salt=__salt__,
            opts=__opts__,
        )["data"]
        if six.PY2:
            contents = contents.encode("utf-8")
    else:
        ret = {}
        ret["result"] = False
        ret["comment"] = ("Specified template format {0} is not supported").format(
            template
        )
        return ret
    return contents