Skip to content

Commit

Permalink
Previous fix to this failed to account for open_url returning a fileh…
Browse files Browse the repository at this point in the history
…andle (ansible#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.
  • Loading branch information
abadger authored Jan 11, 2017
1 parent f340b8d commit 1786c81
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/ansible/galaxy/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ def __call_galaxy(self, url, args=None, headers=None, method=None):
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

Expand All @@ -119,7 +119,7 @@ def _get_server_api_version(self):
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)))

Expand All @@ -136,7 +136,7 @@ def authenticate(self, github_token):
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
Expand Down

0 comments on commit 1786c81

Please sign in to comment.