Skip to content

Commit 4f63d5d

Browse files
author
Cat Lee Ball 🎷🐛
committed
Ensure string for Py3 compat
1 parent 8129b70 commit 4f63d5d

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

apitools/base/py/credentials_lib.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,15 @@ def GetCredentials(package_name, scopes, client_id, client_secret, user_agent,
137137
**kwds):
138138
"""Attempt to get credentials, using an oauth dance as the last resort."""
139139
scopes = util.NormalizeScopes(scopes)
140+
if isinstance(scope_spec, six.string_types):
141+
scope_spec = six.ensure_str(scope_spec)
142+
return set(scope_spec.split(' '))
143+
elif isinstance(scope_spec, collections.Iterable):
144+
scope_spec = [six.ensure_str(x) for x in scope_spec]
145+
return set(scope_spec)
146+
raise exceptions.TypecheckError(
147+
'NormalizeScopes expected string or iterable, found %s' % (
148+
type(scope_spec),))
140149
client_info = {
141150
'client_id': client_id,
142151
'client_secret': client_secret,
@@ -355,15 +364,15 @@ def _ScopesFromMetadataServer(self, scopes):
355364
def GetServiceAccount(self, account):
356365
relative_url = 'instance/service-accounts'
357366
response = _GceMetadataRequest(relative_url)
358-
response_lines = [six.ensure_text(line).rstrip(u'/\n\r')
367+
response_lines = [six.ensure_str(line).rstrip(u'/\n\r')
359368
for line in response.readlines()]
360369
return account in response_lines
361370

362371
def GetInstanceScopes(self):
363372
relative_url = 'instance/service-accounts/{0}/scopes'.format(
364373
self.__service_account_name)
365374
response = _GceMetadataRequest(relative_url)
366-
return util.NormalizeScopes(scope.strip()
375+
return util.NormalizeScopes(six.ensure_str(scope).strip()
367376
for scope in response.readlines())
368377

369378
# pylint: disable=arguments-differ

apitools/base/py/util.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ def DetectGce():
7676
def NormalizeScopes(scope_spec):
7777
"""Normalize scope_spec to a set of strings."""
7878
if isinstance(scope_spec, six.string_types):
79+
scope_spec = six.ensure_str(scope_spec)
7980
return set(scope_spec.split(' '))
8081
elif isinstance(scope_spec, collections.Iterable):
82+
scope_spec = [six.ensure_str(x) for x in scope_spec]
8183
return set(scope_spec)
8284
raise exceptions.TypecheckError(
8385
'NormalizeScopes expected string or iterable, found %s' % (

0 commit comments

Comments
 (0)