Skip to content

Commit

Permalink
Make clients explicitly unpickleable.
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sneeringer committed Mar 28, 2017
1 parent 081f93a commit 15529c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions core/google/cloud/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

"""Base classes for client used to interact with Google Cloud APIs."""

from pickle import PicklingError

import google.auth.credentials
from google.oauth2 import service_account
import google_auth_httplib2
Expand Down Expand Up @@ -126,6 +128,14 @@ def __init__(self, credentials=None, http=None):
credentials, self.SCOPE)
self._http_internal = http

def __getstate__(self):
"""Explicitly state that clients are not pickleable."""

raise PicklingError('\n'.join([
'Pickling client objects is explicitly not supported.',
'Clients have non-trivial state that is local and unpickleable.',
]))

@property
def _http(self):
"""Getter for object used for HTTP transport.
Expand Down
7 changes: 7 additions & 0 deletions core/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ def _get_target_class():
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_unpickleable(self):
import pickle

client = self._make_one()
with self.assertRaises(pickle.PicklingError):
pickle.dumps(client)

def test_ctor_defaults(self):
from google.cloud._testing import _Monkey
from google.cloud import client
Expand Down

0 comments on commit 15529c1

Please sign in to comment.