# Buggy code
def create_script(command):
    """Write out a script onto a target.

    This method should be backward compatible with Python 2.4+ when executing
    from within the container.

    :param command: command to run, this can be a script and can use spacing
                    with newlines as separation.
    :type command: ``str``
    """

    (fd, script_file) = tempfile.mkstemp(prefix="lxc-attach-script")
    f = os.fdopen(fd, "wb")
    try:
        f.write(ATTACH_TEMPLATE % {"container_command": command})
        f.flush()
    finally:
        f.close()

    # Ensure the script is executable.
    os.chmod(script_file, int("0700", 8))

    # Output log file.
    stdout_file = os.fdopen(tempfile.mkstemp(prefix="lxc-attach-script-log")[0], "ab")

    # Error log file.
    stderr_file = os.fdopen(tempfile.mkstemp(prefix="lxc-attach-script-err")[0], "ab")

    # Execute the script command.
    try:
        subprocess.Popen(
            [script_file], stdout=stdout_file, stderr=stderr_file
        ).communicate()
    finally:
        # Close the log files.
        stderr_file.close()
        stdout_file.close()

        # Remove the script file upon completion of execution.
        os.remove(script_file)


# Traceback (nTypeError)
failed: [host] (item={'template': 'ubuntu', 'name': 'py3test', 'state': 'started', 'template_options': '--release zesty', 'container_config': []}) => {"changed": false, "failed": true, "item": {"container_config": ["lxc.start.auto=0"], "name": "py3test", "state": "started", "template": "ubuntu", "template_options": "--release zesty"}, "module_stderr": "Shared connection to dd-c closed.\r\n", "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_4rnj_ldq/ansible_module_lxc_container.py\", line 1771, in <module>\r\n    main()\r\n  File \"/tmp/ansible_4rnj_ldq/ansible_module_lxc_container.py\", line 1767, in main\r\n    lxc_manage.run()\r\n  File \"/tmp/ansible_4rnj_ldq/ansible_module_lxc_container.py\", line 1647, in run\r\n    action()\r\n  File \"/tmp/ansible_4rnj_ldq/ansible_module_lxc_container.py\", line 1182, in _started\r\n    self._config()\r\n  File \"/tmp/ansible_4rnj_ldq/ansible_module_lxc_container.py\", line 743, in _config\r\n    if keyre.match(option_line):\r\nTypeError: cannot use a string pattern on a bytes-like object\r\n", "msg": "MODULE FAILURE", "rc": 0}

# Fixed code
def create_script(command):
    """Write out a script onto a target.

    This method should be backward compatible with Python 2.4+ when executing
    from within the container.

    :param command: command to run, this can be a script and can use spacing
                    with newlines as separation.
    :type command: ``str``
    """

    (fd, script_file) = tempfile.mkstemp(prefix="lxc-attach-script")
    f = os.fdopen(fd, "wb")
    try:
        f.write(
            to_bytes(
                ATTACH_TEMPLATE % {"container_command": command},
                errors="surrogate_or_strict",
            )
        )
        f.flush()
    finally:
        f.close()

    # Ensure the script is executable.
    os.chmod(script_file, int("0700", 8))

    # Output log file.
    stdout_file = os.fdopen(tempfile.mkstemp(prefix="lxc-attach-script-log")[0], "ab")

    # Error log file.
    stderr_file = os.fdopen(tempfile.mkstemp(prefix="lxc-attach-script-err")[0], "ab")

    # Execute the script command.
    try:
        subprocess.Popen(
            [script_file], stdout=stdout_file, stderr=stderr_file
        ).communicate()
    finally:
        # Close the log files.
        stderr_file.close()
        stdout_file.close()

        # Remove the script file upon completion of execution.
        os.remove(script_file)