Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make clients explicitly unpickleable. #3230

Merged
merged 4 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

This comment was marked as spam.

This comment was marked as spam.


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