diff --git a/core/google/cloud/client.py b/core/google/cloud/client.py index 7fa603b77527..09dd512fdc32 100644 --- a/core/google/cloud/client.py +++ b/core/google/cloud/client.py @@ -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 @@ -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. diff --git a/core/tests/unit/test_client.py b/core/tests/unit/test_client.py index d76e3d776bfe..15fd795b06e5 100644 --- a/core/tests/unit/test_client.py +++ b/core/tests/unit/test_client.py @@ -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