From 1786c81a654611fe2c4ca4f03904aab0b533c1ed Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Tue, 10 Jan 2017 18:14:20 -0800 Subject: [PATCH] Previous fix to this failed to account for open_url returning a filehandle (#20097) * Previous fix to this failed to account for open_url returning a filehandle Fixes the bugs introduced by c6fb355 * read() from HTTPError for python-3.6+ HTTPError is funny. It contains a filehandle to read the response from and also makes it available via a read() method. On earlier versions of python (2 and 3) the read() method was enough to make it work with json.load(). The newer version of json.load() needs a more complete file interface than this and has stopped working. Read the bytes, transform to str and pass it in manually to fix it. --- lib/ansible/galaxy/api.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/ansible/galaxy/api.py b/lib/ansible/galaxy/api.py index 2e02e9be32..883d94f83a 100644 --- a/lib/ansible/galaxy/api.py +++ b/lib/ansible/galaxy/api.py @@ -93,9 +93,9 @@ class GalaxyAPI(object): display.vvv(url) resp = open_url(url, data=args, validate_certs=self._validate_certs, headers=headers, method=method, timeout=20) - data = json.load(to_text(resp, errors='surrogate_or_strict')) + data = json.loads(to_text(resp.read(), errors='surrogate_or_strict')) except HTTPError as e: - res = json.load(e) + res = json.loads(to_text(e.fp.read(), errors='surrogate_or_strict')) raise AnsibleError(res['detail']) return data @@ -119,7 +119,7 @@ class GalaxyAPI(object): raise AnsibleError("Failed to get data from the API server (%s): %s " % (url, to_native(e))) try: - data = json.load(to_text(return_data, errors='surrogate_or_strict')) + data = json.loads(to_text(return_data.read(), errors='surrogate_or_strict')) except Exception as e: raise AnsibleError("Could not process data from the API server (%s): %s " % (url, to_native(e))) @@ -136,7 +136,7 @@ class GalaxyAPI(object): url = '%s/tokens/' % self.baseurl args = urlencode({"github_token": github_token}) resp = open_url(url, data=args, validate_certs=self._validate_certs, method="POST") - data = json.load(to_text(resp, errors='surrogate_or_strict')) + data = json.loads(to_text(resp.read(), errors='surrogate_or_strict')) return data @g_connect