'This is the function which executes the low level shell command, which
may be commands to create/remove directories for temporary files, or to
run the module code or python directly when pipelining.
:kwarg encoding_errors: If the value returned by the command isn\'t
utf-8 then we have to figure out how to transform it to unicode.
If the value is just going to be displayed to the user (or
discarded) then the default of \'replace\' is fine.  If the data is
used as a key or is going to be written back out to a file
verbatim, then this won\'t work.  May have to use some sort of
replacement strategy (python3 could use surrogateescape)
:kwarg chdir: cd into this directory before executing the command.'

def _low_level_execute_command(self, cmd, sudoable=True, in_data=None, executable=None, encoding_errors='surrogate_then_replace', chdir=None):

    display.debug('_low_level_execute_command():    starting')
    if chdir:
        display.debug(('_low_level_execute_command():    changing    cwd    to    %s    for    this    command' % chdir))
        cmd = self._connection._shell.append_command(('cd    %s' % chdir), cmd)
    allow_same_user = C.BECOME_ALLOW_SAME_USER
    same_user = (self._play_context.become_user == self._play_context.remote_user)
    if (sudoable and self._play_context.become and (allow_same_user or (not same_user))):
        display.debug('_low_level_execute_command():    using    become    for    this    command')
        cmd = self._play_context.make_become_cmd(cmd, executable=executable)
    if self._connection.allow_executable:
        if (executable is None):
            executable = self._play_context.executable
            cmd = self._connection._shell.append_command(cmd, 'sleep    0')
        if executable:
            cmd = ((executable + '    -c    ') + shlex_quote(cmd))
    display.debug(('_low_level_execute_command():    executing:    %s' % (cmd,)))
    if (self._connection.transport == 'local'):
        cwd = os.getcwd()
        os.chdir(self._loader.get_basedir())
    try:
        (rc, stdout, stderr) = self._connection.exec_command(cmd, in_data=in_data, sudoable=sudoable)
    finally:
        if (self._connection.transport == 'local'):
            os.chdir(cwd)
    if isinstance(stdout, binary_type):
        out = to_text(stdout, errors=encoding_errors)
    elif (not isinstance(stdout, text_type)):
        out = to_text(''.join(stdout.readlines()), errors=encoding_errors)
    else:
        out = stdout
    if isinstance(stderr, binary_type):
        err = to_text(stderr, errors=encoding_errors)
    elif (not isinstance(stderr, text_type)):
        err = to_text(''.join(stderr.readlines()), errors=encoding_errors)
    else:
        err = stderr
    if (rc is None):
        rc = 0
    out = self._strip_success_message(out)
    display.debug((u'_low_level_execute_command()    done:    rc=%d,    stdout=%s,    stderr=%s' % (rc, out, err)))
    return dict(rc=rc, stdout=out, stdout_lines=out.splitlines(), stderr=err)
