From 982c4557d2b8218ea7be53402b242228a8ea800a Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Fri, 2 Sep 2016 18:35:14 -0700 Subject: [PATCH] Python 3 fixes for async_wrapper and async_status. (#4671) --- utilities/logic/async_status.py | 2 +- utilities/logic/async_wrapper.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/utilities/logic/async_status.py b/utilities/logic/async_status.py index 2e11a301a42..507780b46a1 100644 --- a/utilities/logic/async_status.py +++ b/utilities/logic/async_status.py @@ -79,7 +79,7 @@ def main(): data = None try: - data = file(log_path).read() + data = open(log_path).read() data = json.loads(data) except Exception: if not data: diff --git a/utilities/logic/async_wrapper.py b/utilities/logic/async_wrapper.py index 320b3c5895a..b1adb9c5bcd 100644 --- a/utilities/logic/async_wrapper.py +++ b/utilities/logic/async_wrapper.py @@ -32,6 +32,8 @@ import signal import time import syslog +PY3 = sys.version_info[0] == 3 + syslog.openlog('ansible-%s' % os.path.basename(__file__)) syslog.syslog(syslog.LOG_NOTICE, 'Invoked with %s' % " ".join(sys.argv[1:])) @@ -64,7 +66,7 @@ def daemonize_self(): e = sys.exc_info()[1] sys.exit("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror)) - dev_null = file('/dev/null','rw') + dev_null = open('/dev/null', 'w') os.dup2(dev_null.fileno(), sys.stdin.fileno()) os.dup2(dev_null.fileno(), sys.stdout.fileno()) os.dup2(dev_null.fileno(), sys.stderr.fileno()) @@ -85,6 +87,9 @@ def _run_module(wrapped_cmd, jid, job_path): cmd = shlex.split(wrapped_cmd) script = subprocess.Popen(cmd, shell=False, stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (outdata, stderr) = script.communicate() + if PY3: + outdata = outdata.decode('utf-8', 'surrogateescape') + stderr = stderr.decode('utf-8', 'surrogateescape') result = json.loads(outdata) if stderr: result['stderr'] = stderr