Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 895e369

Browse files
committed
Using six.raise_from wherever possible.
1 parent b7b48f1 commit 895e369

12 files changed

Lines changed: 63 additions & 40 deletions

File tree

google/auth/_default.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import logging
2323
import os
2424

25+
import six
26+
2527
from google.auth import environment_vars
2628
from google.auth import exceptions
2729
import google.auth.transport._http_client
@@ -67,9 +69,10 @@ def _load_credentials_from_file(filename):
6769
with io.open(filename, 'r') as file_obj:
6870
try:
6971
info = json.load(file_obj)
70-
except ValueError as exc:
71-
raise exceptions.DefaultCredentialsError(
72-
'File {} is not a valid json file.'.format(filename), exc)
72+
except ValueError as caught_exc:
73+
new_exc = exceptions.DefaultCredentialsError(
74+
'File {} is not a valid json file.'.format(filename))
75+
six.raise_from(new_exc, caught_exc)
7376

7477
# The type key should indicate that the file is either a service account
7578
# credentials file or an authorized user credentials file.
@@ -80,10 +83,11 @@ def _load_credentials_from_file(filename):
8083

8184
try:
8285
credentials = _cloud_sdk.load_authorized_user_credentials(info)
83-
except ValueError as exc:
84-
raise exceptions.DefaultCredentialsError(
86+
except ValueError as caught_exc:
87+
new_exc = exceptions.DefaultCredentialsError(
8588
'Failed to load authorized user credentials from {}'.format(
86-
filename), exc)
89+
filename))
90+
six.raise_from(new_exc, caught_exc)
8791
# Authorized user credentials do not contain the project ID.
8892
return credentials, None
8993

@@ -93,10 +97,11 @@ def _load_credentials_from_file(filename):
9397
try:
9498
credentials = (
9599
service_account.Credentials.from_service_account_info(info))
96-
except ValueError as exc:
97-
raise exceptions.DefaultCredentialsError(
100+
except ValueError as caught_exc:
101+
new_exc = exceptions.DefaultCredentialsError(
98102
'Failed to load service account credentials from {}'.format(
99-
filename), exc)
103+
filename))
104+
six.raise_from(new_exc, caught_exc)
100105
return credentials, info.get('project_id')
101106

102107
else:

google/auth/_oauth2client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
import google.oauth2.credentials
2727
import google.oauth2.service_account
2828

29+
import six
2930
try:
3031
import oauth2client.client
3132
import oauth2client.contrib.gce
3233
import oauth2client.service_account
33-
except ImportError:
34-
raise ImportError('oauth2client is not installed.')
34+
except ImportError as caught_exc:
35+
new_exc = ImportError('oauth2client is not installed.')
36+
six.raise_from(new_exc, caught_exc)
3537

3638
try:
3739
import oauth2client.contrib.appengine
@@ -162,5 +164,6 @@ def convert(credentials):
162164

163165
try:
164166
return _CLASS_CONVERSION_MAP[credentials_class](credentials)
165-
except KeyError:
166-
raise ValueError(_CONVERT_ERROR_TMPL.format(credentials_class))
167+
except KeyError as caught_exc:
168+
new_exc = ValueError(_CONVERT_ERROR_TMPL.format(credentials_class))
169+
six.raise_from(new_exc, caught_exc)

google/auth/compute_engine/_metadata.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import logging
2323
import os
2424

25+
import six
2526
from six.moves import http_client
2627
from six.moves.urllib import parse as urlparse
2728

@@ -118,10 +119,11 @@ def get(request, path, root=_METADATA_ROOT, recursive=False):
118119
if response.headers['content-type'] == 'application/json':
119120
try:
120121
return json.loads(content)
121-
except ValueError:
122-
raise exceptions.TransportError(
122+
except ValueError as caught_exc:
123+
new_exc = exceptions.TransportError(
123124
'Received invalid JSON from the Google Compute Engine'
124125
'metadata service: {:.20}'.format(content))
126+
six.raise_from(new_exc, caught_exc)
125127
else:
126128
return content
127129
else:

google/auth/compute_engine/credentials.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
2020
"""
2121

22+
import six
23+
2224
from google.auth import credentials
2325
from google.auth import exceptions
2426
from google.auth.compute_engine import _metadata
@@ -89,8 +91,9 @@ def refresh(self, request):
8991
self.token, self.expiry = _metadata.get_service_account_token(
9092
request,
9193
service_account=self._service_account_email)
92-
except exceptions.TransportError as exc:
93-
raise exceptions.RefreshError(exc)
94+
except exceptions.TransportError as caught_exc:
95+
new_exc = exceptions.RefreshError()
96+
six.raise_from(new_exc, caught_exc)
9497

9598
@property
9699
def service_account_email(self):

google/auth/jwt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import json
4848

4949
import cachetools
50+
import six
5051
from six.moves import urllib
5152

5253
from google.auth import _helpers
@@ -101,8 +102,9 @@ def _decode_jwt_segment(encoded_section):
101102
section_bytes = _helpers.padded_urlsafe_b64decode(encoded_section)
102103
try:
103104
return json.loads(section_bytes.decode('utf-8'))
104-
except ValueError:
105-
raise ValueError('Can\'t parse segment: {0}'.format(section_bytes))
105+
except ValueError as caught_exc:
106+
new_exc = ValueError('Can\'t parse segment: {0}'.format(section_bytes))
107+
six.raise_from(new_exc, caught_exc)
106108

107109

108110
def _unverified_decode(token):

google/auth/transport/_http_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import logging
1818
import socket
1919

20+
import six
2021
from six.moves import http_client
2122
from six.moves import urllib
2223

@@ -104,8 +105,9 @@ def __call__(self, url, method='GET', body=None, headers=None,
104105
response = connection.getresponse()
105106
return Response(response)
106107

107-
except (http_client.HTTPException, socket.error) as exc:
108-
raise exceptions.TransportError(exc)
108+
except (http_client.HTTPException, socket.error) as caught_exc:
109+
new_exc = exceptions.TransportError()
110+
six.raise_from(new_exc, caught_exc)
109111

110112
finally:
111113
connection.close()

google/auth/transport/grpc.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
from __future__ import absolute_import
1818

19+
import six
1920
try:
2021
import grpc
21-
except ImportError: # pragma: NO COVER
22-
raise ImportError(
22+
except ImportError as caught_exc: # pragma: NO COVER
23+
new_exc = ImportError(
2324
'gRPC is not installed, please install the grpcio package to use the '
2425
'gRPC transport.')
25-
import six
26+
six.raise_from(new_exc, caught_exc)
2627

2728

2829
class AuthMetadataPlugin(grpc.AuthMetadataPlugin):

google/auth/transport/requests.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import logging
2020

21+
import six
2122
try:
2223
import requests
23-
except ImportError: # pragma: NO COVER
24-
raise ImportError(
24+
except ImportError as caught_exc: # pragma: NO COVER
25+
new_exc = ImportError(
2526
'The requests library is not installed, please install the requests '
2627
'package to use the requests transport.')
28+
six.raise_from(new_exc, caught_exc)
2729
import requests.exceptions
2830

2931
from google.auth import exceptions
@@ -111,8 +113,9 @@ def __call__(self, url, method='GET', body=None, headers=None,
111113
method, url, data=body, headers=headers, timeout=timeout,
112114
**kwargs)
113115
return _Response(response)
114-
except requests.exceptions.RequestException as exc:
115-
raise exceptions.TransportError(exc)
116+
except requests.exceptions.RequestException as caught_exc:
117+
new_exc = exceptions.TransportError()
118+
six.raise_from(new_exc, caught_exc)
116119

117120

118121
class AuthorizedSession(requests.Session):

google/auth/transport/urllib3.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@
3030
except ImportError: # pragma: NO COVER
3131
certifi = None
3232

33+
import six
3334
try:
3435
import urllib3
35-
except ImportError: # pragma: NO COVER
36-
raise ImportError(
36+
except ImportError as caught_exc: # pragma: NO COVER
37+
new_exc = ImportError(
3738
'The urllib3 library is not installed, please install the urllib3 '
3839
'package to use the urllib3 transport.')
40+
six.raise_from(new_exc, caught_exc)
3941
import urllib3.exceptions
4042

4143
from google.auth import exceptions
@@ -126,8 +128,9 @@ def __call__(self, url, method='GET', body=None, headers=None,
126128
response = self.http.request(
127129
method, url, body=body, headers=headers, **kwargs)
128130
return _Response(response)
129-
except urllib3.exceptions.HTTPError as exc:
130-
raise exceptions.TransportError(exc)
131+
except urllib3.exceptions.HTTPError as caught_exc:
132+
new_exc = exceptions.TransportError()
133+
six.raise_from(new_exc, caught_exc)
131134

132135

133136
def _make_default_http():

google/oauth2/_client.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import datetime
2727
import json
2828

29+
import six
2930
from six.moves import http_client
3031
from six.moves import urllib
3132

@@ -144,9 +145,10 @@ def jwt_grant(request, token_uri, assertion):
144145

145146
try:
146147
access_token = response_data['access_token']
147-
except KeyError:
148-
raise exceptions.RefreshError(
148+
except KeyError as caught_exc:
149+
new_exc = exceptions.RefreshError(
149150
'No access token in response.', response_data)
151+
six.raise_from(new_exc, caught_exc)
150152

151153
expiry = _parse_expiry(response_data)
152154

@@ -190,9 +192,10 @@ def refresh_grant(request, token_uri, refresh_token, client_id, client_secret):
190192

191193
try:
192194
access_token = response_data['access_token']
193-
except KeyError:
194-
raise exceptions.RefreshError(
195+
except KeyError as caught_exc:
196+
new_exc = exceptions.RefreshError(
195197
'No access token in response.', response_data)
198+
six.raise_from(new_exc, caught_exc)
196199

197200
refresh_token = response_data.get('refresh_token', refresh_token)
198201
expiry = _parse_expiry(response_data)

0 commit comments

Comments
 (0)