From 2896df6c1234d1bcb5f9851a28c682b2092e9231 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Fri, 27 Jun 2014 18:15:58 +1000 Subject: [PATCH 01/16] Make setup.py py3 compatible. I've done a basic update to the library to begin getting it to be python3 compatible. A lot of upstream projects still rely on this unfortunately and their communities are not willing to change to oauthlib. This will help to prolong the inevitable. --- setup.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/setup.py b/setup.py index acc41e17..811c2941 100755 --- a/setup.py +++ b/setup.py @@ -1,5 +1,6 @@ #!/usr/bin/env python from setuptools import setup, find_packages +from __future__ import print_function import os, re PKG='oauth2' @@ -15,7 +16,7 @@ if mo: mverstr = mo.group(1) else: - print "unable to find version in %s" % (VERSIONFILE,) + print ("unable to find version in %s") % (VERSIONFILE,) raise RuntimeError("if %s.py exists, it must be well-formed" % (VERSIONFILE,)) AVSRE = r"^auto_build_num *= *['\"]([^'\"]*)['\"]" mo = re.search(AVSRE, verstrline, re.M) From ae83b9b17e0c9c420f0e68d2c2b29225f16ac86a Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Fri, 27 Jun 2014 22:14:47 +1000 Subject: [PATCH 02/16] Update setup.pu Future imports must be at the beginning of the file. --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 811c2941..d112ed95 100755 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -from setuptools import setup, find_packages from __future__ import print_function +from setuptools import setup, find_packages import os, re PKG='oauth2' From 1716e792004481d0f9996c56715806ba69340091 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Fri, 27 Jun 2014 22:19:19 +1000 Subject: [PATCH 03/16] Update __init__.py Fix incorrect syntax --- oauth2/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 835270e3..8fc11a74 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -102,7 +102,7 @@ def to_unicode(s): raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s)) try: s = s.decode('utf-8') - except UnicodeDecodeError, le: + except UnicodeDecodeError as le: raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) return s @@ -131,7 +131,7 @@ def to_unicode_optional_iterator(x): try: l = list(x) - except TypeError, e: + except TypeError as e: assert 'is not iterable' in str(e) return x else: @@ -147,7 +147,7 @@ def to_utf8_optional_iterator(x): try: l = list(x) - except TypeError, e: + except TypeError as e: assert 'is not iterable' in str(e) return x else: @@ -460,7 +460,7 @@ def get_normalized_parameters(self): else: try: value = list(value) - except TypeError, e: + except TypeError as e: assert 'is not iterable' in str(e) items.append((to_utf8_if_string(key), to_utf8_if_string(value))) else: From cc4b905e7129d84acde67b07d99ff609dcdf3765 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Fri, 27 Jun 2014 22:34:22 +1000 Subject: [PATCH 04/16] Update test_oauth.py --- tests/test_oauth.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/test_oauth.py b/tests/test_oauth.py index 099e5794..f8a74482 100644 --- a/tests/test_oauth.py +++ b/tests/test_oauth.py @@ -49,19 +49,19 @@ class TestError(unittest.TestCase): def test_message(self): try: raise oauth.Error - except oauth.Error, e: + except oauth.Error as e: self.assertEqual(e.message, 'OAuth error occurred.') msg = 'OMG THINGS BROKE!!!!' try: raise oauth.Error(msg) - except oauth.Error, e: + except oauth.Error as e: self.assertEqual(e.message, msg) def test_str(self): try: raise oauth.Error - except oauth.Error, e: + except oauth.Error as e: self.assertEquals(str(e), 'OAuth error occurred.') class TestGenerateFunctions(unittest.TestCase): @@ -286,7 +286,7 @@ def test_deleter(self): self.fail("AttributeError should have been raised on empty url.") except AttributeError: pass - except Exception, e: + except Exception as e: self.fail(str(e)) def test_url(self): From 30060ce42879b20daa45f370049560a1e671a35e Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 22:05:17 +1000 Subject: [PATCH 05/16] try catch urlparse for python3 --- oauth2/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 8fc11a74..ab0a74be 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -26,11 +26,16 @@ import urllib import time import random -import urlparse import hmac import binascii import httplib2 +try: + import urlparse +except ImportError: + # urlparse location changed in python 3 + from urllib.parse import urlparse + try: from urlparse import parse_qs parse_qs # placate pyflakes From 500fde78b518f33238b1e68cddfef46423bfe1e5 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:09:42 +1000 Subject: [PATCH 06/16] Update __init__.py Python 3 strings are unicode (utf-8) by default. This will break python2 applications. --- oauth2/__init__.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index ab0a74be..411e84b1 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -102,13 +102,14 @@ def build_xoauth_string(url, consumer, token=None): def to_unicode(s): """ Convert to unicode, raise exception with instructive error message if s is not unicode, ascii, or utf-8. """ - if not isinstance(s, unicode): - if not isinstance(s, str): - raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s)) - try: - s = s.decode('utf-8') - except UnicodeDecodeError as le: - raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) + # Python 3 strings are unicode (utf-8) by default + #if not isinstance(s, unicode): + # if not isinstance(s, str): + # raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s)) + # try: + # s = s.decode('utf-8') + # except UnicodeDecodeError as le: + # raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) return s def to_utf8(s): From 92b265b52ecd60209a6a4940f5f7af4b22d2bb6a Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:12:25 +1000 Subject: [PATCH 07/16] Update __init__.py Maintain backwards compat as much as is possible. --- oauth2/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 411e84b1..9f9081bf 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -34,7 +34,7 @@ import urlparse except ImportError: # urlparse location changed in python 3 - from urllib.parse import urlparse + from urllib.parse as urlparse try: from urlparse import parse_qs From 14e9230e4a3605474668b67979cc9b5ef5124e6a Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:13:32 +1000 Subject: [PATCH 08/16] Update __init__.py --- oauth2/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 9f9081bf..f165079e 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -34,7 +34,7 @@ import urlparse except ImportError: # urlparse location changed in python 3 - from urllib.parse as urlparse + from urllib import parse as urlparse try: from urlparse import parse_qs From bc04cd5b0b6ea3b9207f8145c2f159c0c14bcca8 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:15:28 +1000 Subject: [PATCH 09/16] Update __init__.py --- oauth2/__init__.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index f165079e..ccbcd07b 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -92,7 +92,7 @@ def build_xoauth_string(url, consumer, token=None): request.sign_request(signing_method, consumer, token) params = [] - for k, v in sorted(request.iteritems()): + for k, v in sorted(request.items()): if v is not None: params.append('%s="%s"' % (k, escape(v))) @@ -351,7 +351,7 @@ def __init__(self, method=HTTP_METHOD, url=None, parameters=None, self.url = to_unicode(url) self.method = method if parameters is not None: - for k, v in parameters.iteritems(): + for k, v in parameters.items(): k = to_unicode(k) v = to_unicode_optional_iterator(v) self[k] = v @@ -388,7 +388,7 @@ def _get_timestamp_nonce(self): def get_nonoauth_parameters(self): """Get any non-OAuth parameters.""" - return dict([(k, v) for k, v in self.iteritems() + return dict([(k, v) for k, v in self.items() if not k.startswith('oauth_')]) def to_header(self, realm=''): @@ -408,7 +408,7 @@ def to_header(self, realm=''): def to_postdata(self): """Serialize as post data for a POST request.""" d = {} - for k, v in self.iteritems(): + for k, v in self.items(): d[k.encode('utf-8')] = to_utf8_optional_iterator(v) # tell urlencode to deal with sequence values and map them correctly @@ -456,7 +456,7 @@ def get_parameter(self, parameter): def get_normalized_parameters(self): """Return a string that contains the parameters that must be signed.""" items = [] - for key, value in self.iteritems(): + for key, value in self.items(): if key == 'oauth_signature': continue # 1.0a/9.1.1 states that kvp must be sorted by key, then by value, @@ -613,7 +613,7 @@ def _split_header(header): def _split_url_string(param_str): """Turn URL string into parameters.""" parameters = parse_qs(param_str.encode('utf-8'), keep_blank_values=True) - for k, v in parameters.iteritems(): + for k, v in parameters.items(): parameters[k] = urllib.unquote(v[0]) return parameters From b22f98f304d157ba819d10b15ad5ff573454e7ae Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:22:14 +1000 Subject: [PATCH 10/16] Update __init__.py Fixes for basestring and hopefully keeps backwards compatibility --- oauth2/__init__.py | 81 +++++++++++++++++++++++++++++++--------------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index ccbcd07b..f22faea1 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -103,28 +103,37 @@ def to_unicode(s): """ Convert to unicode, raise exception with instructive error message if s is not unicode, ascii, or utf-8. """ # Python 3 strings are unicode (utf-8) by default - #if not isinstance(s, unicode): - # if not isinstance(s, str): - # raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s)) - # try: - # s = s.decode('utf-8') - # except UnicodeDecodeError as le: - # raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) + try: + if not isinstance(s, unicode): + if not isinstance(s, str): + raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s)) + try: + s = s.decode('utf-8') + except UnicodeDecodeError as le: + raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) + except NameError: + pass return s def to_utf8(s): return to_unicode(s).encode('utf-8') def to_unicode_if_string(s): - if isinstance(s, basestring): - return to_unicode(s) - else: + try: + if isinstance(s, basestring): + return to_unicode(s) + else: + return s + except NameError: return s def to_utf8_if_string(s): - if isinstance(s, basestring): - return to_utf8(s) - else: + try: + if isinstance(s, basestring): + return to_utf8(s) + else: + return s + except NameError: return s def to_unicode_optional_iterator(x): @@ -132,8 +141,12 @@ def to_unicode_optional_iterator(x): Raise TypeError if x is a str containing non-utf8 bytes or if x is an iterable which contains such a str. """ - if isinstance(x, basestring): - return to_unicode(x) + try: + if isinstance(x, basestring): + return to_unicode(x) + except NameError: + if isinstance(x, str): + return x try: l = list(x) @@ -148,8 +161,12 @@ def to_utf8_optional_iterator(x): Raise TypeError if x is a str or if x is an iterable which contains a str. """ - if isinstance(x, basestring): - return to_utf8(x) + try: + if isinstance(x, basestring): + return to_utf8(x) + except NameError: + if isinstance(x, str): + return x try: l = list(x) @@ -461,16 +478,28 @@ def get_normalized_parameters(self): continue # 1.0a/9.1.1 states that kvp must be sorted by key, then by value, # so we unpack sequence values into multiple items for sorting. - if isinstance(value, basestring): - items.append((to_utf8_if_string(key), to_utf8(value))) - else: - try: - value = list(value) - except TypeError as e: - assert 'is not iterable' in str(e) - items.append((to_utf8_if_string(key), to_utf8_if_string(value))) + try: + if isinstance(value, basestring): + items.append((to_utf8_if_string(key), to_utf8(value))) + else: + try: + value = list(value) + except TypeError as e: + assert 'is not iterable' in str(e) + items.append((to_utf8_if_string(key), to_utf8_if_string(value))) + else: + items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value) + except NameError: + if isinstance(value, str): + items.append((to_utf8_if_string(key), to_utf8(value))) else: - items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value) + try: + value = list(value) + except TypeError as e: + assert 'is not iterable' in str(e) + items.append((to_utf8_if_string(key), to_utf8_if_string(value))) + else: + items.extend((to_utf8_if_string(key), to_utf8_if_string(item)) for item in value) # Include any query string parameters from the provided URL query = urlparse.urlparse(self.url)[4] From 1ffb490e1836330b7a1614c0af8197b1304aab83 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:26:24 +1000 Subject: [PATCH 11/16] Update __init__.py Fix location or urllib.quote and urllib.unquote for py3k --- oauth2/__init__.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index f22faea1..8dd18c26 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -178,7 +178,10 @@ def to_utf8_optional_iterator(x): def escape(s): """Escape a URL including any /.""" - return urllib.quote(s.encode('utf-8'), safe='~') + try: + return urllib.quote(s.encode('utf-8'), safe='~') + except AttributeError: + return urllib.parse.quote(s.encode('utf-8'), safe='~') def generate_timestamp(): """Get seconds since epoch (UTC).""" @@ -635,7 +638,10 @@ def _split_header(header): # Split key-value. param_parts = param.split('=', 1) # Remove quotes and unescape the value. - params[param_parts[0]] = urllib.unquote(param_parts[1].strip('\"')) + try: + params[param_parts[0]] = urllib.unquote(param_parts[1].strip('\"')) + except AttributeError: + params[param_parts[0]] = urllib.parse.unquote(param_parts[1].strip('\"')) return params @staticmethod @@ -643,7 +649,10 @@ def _split_url_string(param_str): """Turn URL string into parameters.""" parameters = parse_qs(param_str.encode('utf-8'), keep_blank_values=True) for k, v in parameters.items(): - parameters[k] = urllib.unquote(v[0]) + try: + parameters[k] = urllib.unquote(v[0]) + except AttributeError: + parameters[k] = urllib.parse.unquote(v[0]) return parameters From 77098c510de8adcf02dc5a6d9c23a4e3ce8ada86 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:35:09 +1000 Subject: [PATCH 12/16] Update __init__.py --- oauth2/__init__.py | 50 +++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 8dd18c26..b4de18d4 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -181,7 +181,7 @@ def escape(s): try: return urllib.quote(s.encode('utf-8'), safe='~') except AttributeError: - return urllib.parse.quote(s.encode('utf-8'), safe='~') + return urlparse.quote(s.encode('utf-8'), safe='~') def generate_timestamp(): """Get seconds since epoch (UTC).""" @@ -231,8 +231,10 @@ def __init__(self, key, secret): def __str__(self): data = {'oauth_consumer_key': self.key, 'oauth_consumer_secret': self.secret} - - return urllib.urlencode(data) + try: + return urllib.urlencode(data) + except AttributeError: + return urlparse.urlencode(data) class Token(object): @@ -300,7 +302,10 @@ def to_string(self): if self.callback_confirmed is not None: data['oauth_callback_confirmed'] = self.callback_confirmed - return urllib.urlencode(data) + try: + return urllib.urlencode(data) + except AttributeError: + return urlparse.urlencode(data) @staticmethod def from_string(s): @@ -434,7 +439,10 @@ def to_postdata(self): # tell urlencode to deal with sequence values and map them correctly # to resulting querystring. for example self["k"] = ["v1", "v2"] will # result in 'k=v1&k=v2' and not k=%5B%27v1%27%2C+%27v2%27%5D - return urllib.urlencode(d, True).replace('+', '%20') + try: + return urllib.urlencode(d, True).replace('+', '%20') + except AttributeError: + return urlparse.urlencode(d, True).replace('+', '%20') def to_url(self): """Serialize as a URL for a GET request.""" @@ -462,9 +470,14 @@ def to_url(self): params = base_url[3] fragment = base_url[5] - url = (scheme, netloc, path, params, - urllib.urlencode(query, True), fragment) - return urlparse.urlunparse(url) + try: + url = (scheme, netloc, path, params, + urllib.urlencode(query, True), fragment) + return urllib.urlunparse(url) + except AttributeError: + url = (scheme, netloc, path, params, + urlparse.urlencode(query, True), fragment) + return urlparse.urlunparse(url) def get_parameter(self, parameter): ret = self.get(parameter) @@ -512,7 +525,10 @@ def get_normalized_parameters(self): items.extend(url_items) items.sort() - encoded_str = urllib.urlencode(items) + try: + encoded_str = urllib.urlencode(items) + except AttributeError: + encoded_str = urlparse.urlencode(items) # Encode signature parameters per Oauth Core 1.0 protocol # spec draft 7, section 3.6 # (http://tools.ietf.org/html/draft-hammer-oauth-07#section-3.6) @@ -641,7 +657,7 @@ def _split_header(header): try: params[param_parts[0]] = urllib.unquote(param_parts[1].strip('\"')) except AttributeError: - params[param_parts[0]] = urllib.parse.unquote(param_parts[1].strip('\"')) + params[param_parts[0]] = urlparse.unquote(param_parts[1].strip('\"')) return params @staticmethod @@ -652,7 +668,7 @@ def _split_url_string(param_str): try: parameters[k] = urllib.unquote(v[0]) except AttributeError: - parameters[k] = urllib.parse.unquote(v[0]) + parameters[k] = urlparse.unquote(v[0]) return parameters @@ -704,13 +720,19 @@ def request(self, uri, method="GET", body='', headers=None, parameters=parameters, body=body, is_form_encoded=is_form_encoded) req.sign_request(self.method, self.consumer, self.token) - - schema, rest = urllib.splittype(uri) + + try: + schema, rest = urllib.splittype(uri) + except AttributeError: + schema, rest = urlparse.splittype(uri) if rest.startswith('//'): hierpart = '//' else: hierpart = '' - host, rest = urllib.splithost(rest) + try: + host, rest = urllib.splithost(rest) + except AttributeError: + host, rest = urlparse.splithost(rest) realm = schema + ':' + hierpart + host From c05dff2e6f81cc615d967d34097d391fd0a9a851 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:39:42 +1000 Subject: [PATCH 13/16] Update __init__.py hmac.new expects key to be a bytearray --- oauth2/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index b4de18d4..6aa3a60b 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -903,7 +903,7 @@ def sign(self, request, consumer, token): """Builds the base signature string.""" key, raw = self.signing_base(request, consumer, token) - hashed = hmac.new(key, raw, sha) + hashed = hmac.new(key.encode('bytearray'), raw, sha) # Calculate the digest base 64. return binascii.b2a_base64(hashed.digest())[:-1] From 15ec5d23eee9ad375dc1a5cc43b4d2c98548c473 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:57:29 +1000 Subject: [PATCH 14/16] Update __init__.py Unicode fix for hmac --- oauth2/__init__.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 6aa3a60b..ce9e378b 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -112,7 +112,12 @@ def to_unicode(s): except UnicodeDecodeError as le: raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) except NameError: - pass + if not isinstance(s, str): + raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s)) + try: + s = s.decode('utf-8') + except UnicodeDecodeError as le: + raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) return s def to_utf8(s): @@ -125,7 +130,10 @@ def to_unicode_if_string(s): else: return s except NameError: - return s + if isinstance(s, str): + return to_unicode(s) + else: + return s def to_utf8_if_string(s): try: @@ -134,7 +142,11 @@ def to_utf8_if_string(s): else: return s except NameError: - return s + if isinstance(s, str): + return to_utf8(s) + else: + return s + except NameError: def to_unicode_optional_iterator(x): """ @@ -146,7 +158,7 @@ def to_unicode_optional_iterator(x): return to_unicode(x) except NameError: if isinstance(x, str): - return x + return to_unicode(x) try: l = list(x) @@ -166,7 +178,7 @@ def to_utf8_optional_iterator(x): return to_utf8(x) except NameError: if isinstance(x, str): - return x + return to_utf8(x) try: l = list(x) From b7e92804f6178067031f15a7e6464f71f332d546 Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Sun, 29 Jun 2014 23:58:31 +1000 Subject: [PATCH 15/16] Fix accidental except --- oauth2/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index ce9e378b..07e1fb3a 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -146,7 +146,6 @@ def to_utf8_if_string(s): return to_utf8(s) else: return s - except NameError: def to_unicode_optional_iterator(x): """ From e8d757a762e9a60ce80e4224ce9deae447463d6a Mon Sep 17 00:00:00 2001 From: Daniel Holmes Date: Fri, 4 Jul 2014 19:11:58 +1000 Subject: [PATCH 16/16] Make python-oauth2 compatible with python3 Final commit to make python-oauth2 py3k compatible. --- oauth2/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/oauth2/__init__.py b/oauth2/__init__.py index 07e1fb3a..4bf8ae3c 100644 --- a/oauth2/__init__.py +++ b/oauth2/__init__.py @@ -115,7 +115,7 @@ def to_unicode(s): if not isinstance(s, str): raise TypeError('You are required to pass either unicode or string here, not: %r (%s)' % (type(s), s)) try: - s = s.decode('utf-8') + s = s.encode('utf-8') except UnicodeDecodeError as le: raise TypeError('You are required to pass either a unicode object or a utf-8 string here. You passed a Python string object which contained non-utf-8: %r. The UnicodeDecodeError that resulted from attempting to interpret it as utf-8 was: %s' % (s, le,)) return s @@ -914,7 +914,7 @@ def sign(self, request, consumer, token): """Builds the base signature string.""" key, raw = self.signing_base(request, consumer, token) - hashed = hmac.new(key.encode('bytearray'), raw, sha) + hashed = hmac.new(key.encode('utf-8'), raw.encode('utf-8'), sha) # Calculate the digest base 64. return binascii.b2a_base64(hashed.digest())[:-1]