# Buggy code
def assemble_from_fragments(
    src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False
):
    """assemble a file from a directory of fragments"""
    tmpfd, temp_path = tempfile.mkstemp()
    tmp = os.fdopen(tmpfd, "wb")
    delimit_me = False
    add_newline = False

    for f in sorted(os.listdir(src_path)):
        if compiled_regexp and not compiled_regexp.search(f):
            continue
        fragment = "%s/%s" % (src_path, f)
        if not os.path.isfile(fragment) or (
            ignore_hidden and os.path.basename(fragment).startswith(".")
        ):
            continue
        fragment_content = open(fragment, "rb").read()

        # always put a newline between fragments if the previous fragment didn't end with a newline.
        if add_newline:
            tmp.write(b("\n"))

        # delimiters should only appear between fragments
        if delimit_me:
            if delimiter:
                # un-escape anything like newlines
                delimiter = codecs.escape_decode(delimiter)[0]
                tmp.write(delimiter)
                # always make sure there's a newline after the
                # delimiter, so lines don't run together
                if delimiter[-1] != b("\n"):
                    tmp.write(b("\n"))

        tmp.write(fragment_content)
        delimit_me = True
        if fragment_content.endswith(b("\n")):
            add_newline = False
        else:
            add_newline = True

    tmp.close()
    return temp_path


# Traceback (UnicodeDecodeError)
An exception occurred during task execution. The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_FZJteE/ansible_module_assemble.py", line 258, in <module>
main()
File "/tmp/ansible_FZJteE/ansible_module_assemble.py", line 220, in main
path = assemble_from_fragments(src, delimiter, compiled_regexp, ignore_hidden)
File "/tmp/ansible_FZJteE/ansible_module_assemble.py", line 131, in assemble_from_fragments
fragment = u"%s/%s" % (src_path, f)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 5: ordinal not in range(128)

# Fixed code
def assemble_from_fragments(
    src_path, delimiter=None, compiled_regexp=None, ignore_hidden=False
):
    """assemble a file from a directory of fragments"""
    tmpfd, temp_path = tempfile.mkstemp()
    tmp = os.fdopen(tmpfd, "wb")
    delimit_me = False
    add_newline = False

    for f in sorted(os.listdir(src_path)):
        if compiled_regexp and not compiled_regexp.search(f):
            continue
        fragment = os.path.join(src_path, f)
        if not os.path.isfile(fragment) or (
            ignore_hidden and os.path.basename(fragment).startswith(".")
        ):
            continue
        fragment_content = open(fragment, "rb").read()

        # always put a newline between fragments if the previous fragment didn't end with a newline.
        if add_newline:
            tmp.write(b("\n"))

        # delimiters should only appear between fragments
        if delimit_me:
            if delimiter:
                # un-escape anything like newlines
                delimiter = codecs.escape_decode(delimiter)[0]
                tmp.write(delimiter)
                # always make sure there's a newline after the
                # delimiter, so lines don't run together
                if delimiter[-1] != b("\n"):
                    tmp.write(b("\n"))

        tmp.write(fragment_content)
        delimit_me = True
        if fragment_content.endswith(b("\n")):
            add_newline = False
        else:
            add_newline = True

    tmp.close()
    return temp_path