Skip to content

Commit

Permalink
Make clients explicitly unpickleable. (googleapis#3230)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer authored Mar 30, 2017
1 parent 9cb1e37 commit 26cfff9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 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,13 @@ 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
10 changes: 10 additions & 0 deletions core/tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ def _get_target_class():
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_unpickleable(self):
import pickle

CREDENTIALS = _make_credentials()
HTTP = object()

client_obj = self._make_one(credentials=CREDENTIALS, http=HTTP)
with self.assertRaises(pickle.PicklingError):
pickle.dumps(client_obj)

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

0 comments on commit 26cfff9

Please sign in to comment.