1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Make run_command return native strings

This changes the return value on python3   Return value on python2 was
already byte strings (which is the native str type there.)
This commit is contained in:
Toshio Kuratomi 2016-10-03 11:04:12 -07:00
parent ab93d5aae4
commit ddd20627a4

View file

@ -2030,7 +2030,7 @@ class AnsibleModule(object):
else: else:
self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, exception)) self.fail_json(msg='Could not replace file: %s to %s: %s' % (src, dest, exception))
def run_command(self, args, check_rc=False, close_fds=True, executable=None, data=None, binary_data=False, path_prefix=None, cwd=None, use_unsafe_shell=False, prompt_regex=None, environ_update=None, umask=None): def run_command(self, args, check_rc=False, close_fds=True, executable=None, data=None, binary_data=False, path_prefix=None, cwd=None, use_unsafe_shell=False, prompt_regex=None, environ_update=None, umask=None, encoding='utf-8', errors='surrogate_or_strict'):
''' '''
Execute a command, returns rc, stdout, and stderr. Execute a command, returns rc, stdout, and stderr.
@ -2052,8 +2052,27 @@ class AnsibleModule(object):
:kw prompt_regex: Regex string (not a compiled regex) which can be :kw prompt_regex: Regex string (not a compiled regex) which can be
used to detect prompts in the stdout which would otherwise cause used to detect prompts in the stdout which would otherwise cause
the execution to hang (especially if no input data is specified) the execution to hang (especially if no input data is specified)
:kwarg environ_update: dictionary to *update* os.environ with :kw environ_update: dictionary to *update* os.environ with
:kw umask: Umask to be used when running the command. Default None :kw umask: Umask to be used when running the command. Default None
:kw encoding: Since we return native strings, on python3 we need to
know the encoding to use to transform from bytes to text. If you
want to always get bytes back, use encoding=None. The default is
"utf-8". This does not affect transformation of strings given as
args.
:kw errors: Since we return native strings, on python3 we need to
transform stdout and stderr from bytes to text. If the bytes are
undecodable in the ``encoding`` specified, then use this error
handler to deal with them. The default is ``surrogate_or_strict``
which means that the bytes will be decoded using the
surrogateescape error handler if available (available on all
python3 versions we support) otherwise a UnicodeError traceback
will be raised. This does not affect transformations of strings
given as args.
:returns: A 3-tuple of return code (integer), stdout (native string),
and stderr (native string). On python2, stdout and stderr are both
byte strings. On python3, stdout and stderr are text strings converted
according to the encoding and errors parameters. If you want byte
strings on python3, use encoding=None to turn decoding to text off.
''' '''
shell = False shell = False
@ -2188,11 +2207,7 @@ class AnsibleModule(object):
try: try:
if self._debug: if self._debug:
if isinstance(args, list): self.log('Executing: ' + clean_args)
running = ' '.join(args)
else:
running = args
self.log('Executing: ' + running)
cmd = subprocess.Popen(args, **kwargs) cmd = subprocess.Popen(args, **kwargs)
# the communication logic here is essentially taken from that # the communication logic here is essentially taken from that
@ -2268,6 +2283,9 @@ class AnsibleModule(object):
# reset the pwd # reset the pwd
os.chdir(prev_dir) os.chdir(prev_dir)
if encoding is not None:
return (rc, to_native(stdout, encoding=encoding, errors=errors),
to_native(stderr, encoding=encoding, errors=errors))
return (rc, stdout, stderr) return (rc, stdout, stderr)
def append_to_file(self, filename, str): def append_to_file(self, filename, str):