Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
Use jsonpickle if possible
Browse files Browse the repository at this point in the history
Fallback to pickle if jsonpickle is not installed
  • Loading branch information
chripede committed Nov 17, 2016
1 parent 92e4ad3 commit d007870
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions oauth2client/contrib/devshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class CommunicationError(Error):
class NoDevshellServer(Error):
"""Error when no Developer Shell server can be contacted."""


# The request for credential information to the Developer Shell client socket
# is always an empty PBLite-formatted JSON object, so just define it as a
# constant.
Expand Down
11 changes: 9 additions & 2 deletions oauth2client/contrib/django_util/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from django.db import models
from django.utils import encoding
import jsonpickle

import oauth2client

Expand Down Expand Up @@ -48,7 +49,12 @@ def to_python(self, value):
elif isinstance(value, oauth2client.client.Credentials):
return value
else:
return pickle.loads(base64.b64decode(encoding.smart_bytes(value)))
try:
return jsonpickle.decode(
base64.b64decode(encoding.smart_bytes(value)).decode())
except ValueError:
return pickle.loads(
base64.b64decode(encoding.smart_bytes(value)))

def get_prep_value(self, value):
"""Overrides ``models.Field`` method. This is used to convert
Expand All @@ -58,7 +64,8 @@ def get_prep_value(self, value):
if value is None:
return None
else:
return encoding.smart_text(base64.b64encode(pickle.dumps(value)))
return encoding.smart_text(
base64.b64encode(jsonpickle.encode(value).encode()))

def value_to_string(self, obj):
"""Convert the field value from the provided model to a string.
Expand Down
1 change: 1 addition & 0 deletions oauth2client/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def _CreateArgumentParser():
help='Set the logging level of detail.')
return parser


# argparser is an ArgumentParser that contains command-line options expected
# by tools.run(). Pass it in as part of the 'parents' argument to your own
# ArgumentParser.
Expand Down
12 changes: 10 additions & 2 deletions tests/contrib/django_util/test_django_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import pickle
import unittest

import jsonpickle

from oauth2client import _helpers
from oauth2client import client
from oauth2client.contrib.django_util import models
Expand All @@ -36,6 +38,8 @@ def setUp(self):
self.credentials = client.Credentials()
self.pickle_str = _helpers._from_bytes(
base64.b64encode(pickle.dumps(self.credentials)))
self.jsonpickle_str = _helpers._from_bytes(
base64.b64encode(jsonpickle.encode(self.credentials).encode()))

def test_field_is_text(self):
self.assertEqual(self.field.get_internal_type(), 'BinaryField')
Expand All @@ -44,6 +48,10 @@ def test_field_unpickled(self):
self.assertIsInstance(
self.field.to_python(self.pickle_str), client.Credentials)

def test_field_jsonunpickled(self):
self.assertIsInstance(
self.field.to_python(self.jsonpickle_str), client.Credentials)

def test_field_already_unpickled(self):
self.assertIsInstance(
self.field.to_python(self.credentials), client.Credentials)
Expand All @@ -62,12 +70,12 @@ def test_field_unpickled_none(self):
def test_field_pickled(self):
prep_value = self.field.get_db_prep_value(self.credentials,
connection=None)
self.assertEqual(prep_value, self.pickle_str)
self.assertEqual(prep_value, self.jsonpickle_str)

def test_field_value_to_string(self):
self.fake_model.credentials = self.credentials
value_str = self.fake_model_field.value_to_string(self.fake_model)
self.assertEqual(value_str, self.pickle_str)
self.assertEqual(value_str, self.jsonpickle_str)

def test_field_value_to_string_none(self):
self.fake_model.credentials = None
Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/test_devshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ def run(self):
s.recv(to_read, socket.MSG_WAITALL))
if resp_buffer != devshell.CREDENTIAL_INFO_REQUEST_JSON:
self.bad_request = True
l = len(self.response)
s.sendall('{0}\n{1}'.format(l, self.response).encode())
response_len = len(self.response)
s.sendall('{0}\n{1}'.format(response_len, self.response).encode())
finally:
# Will fail if s is None, but these tests never encounter
# that scenario.
Expand Down
1 change: 1 addition & 0 deletions tests/test_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ def test_access_token(self, utcnow):

self.assertEqual(credentials.access_token, token2)


TOKEN_LIFE = service_account._JWTAccessCredentials._MAX_TOKEN_LIFETIME_SECS
T1 = 42
T1_DATE = datetime.datetime(1970, 1, 1, second=T1)
Expand Down

0 comments on commit d007870

Please sign in to comment.