Skip to content

Alias CloudPickler in the cloudpickle namespace as Pickler #235

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

Closed
wants to merge 2 commits into from
Closed
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
18 changes: 10 additions & 8 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@


if sys.version < '3':
from pickle import Pickler
from pickle import Pickler as _Pickler
Copy link
Member

@rgbkrk rgbkrk Jan 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with this, we better leave a comment as to why we're using _Pickler here as some future contributor & maintainer may not know the context and end up breaking this use case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dbczumar -- could you add the context around this PR to the import(s) to help our future selves?

try:
from cStringIO import StringIO
except ImportError:
Expand All @@ -73,7 +73,7 @@
PY3 = False
else:
types.ClassType = type
from pickle import _Pickler as Pickler
from pickle import _Pickler
from io import BytesIO as StringIO
string_types = (str,)
PY3 = True
Expand Down Expand Up @@ -266,21 +266,21 @@ def _walk_global_ops(code):
yield op, instr.arg


class CloudPickler(Pickler):
class CloudPickler(_Pickler):

dispatch = Pickler.dispatch.copy()
dispatch = _Pickler.dispatch.copy()

def __init__(self, file, protocol=None):
if protocol is None:
protocol = DEFAULT_PROTOCOL
Pickler.__init__(self, file, protocol=protocol)
_Pickler.__init__(self, file, protocol=protocol)
# map ids to dictionary. used to ensure that functions can share global env
self.globals_ref = {}

def dump(self, obj):
self.inject_addons()
try:
return Pickler.dump(self, obj)
return _Pickler.dump(self, obj)
except RuntimeError as e:
if 'recursion' in e.args[0]:
msg = """Could not pickle object as excessively deep recursion required."""
Expand Down Expand Up @@ -709,7 +709,7 @@ def save_global(self, obj, name=None, pack=struct.pack):
return self.save_dynamic_class(obj)

try:
return Pickler.save_global(self, obj, name=name)
return _Pickler.save_global(self, obj, name=name)
except Exception:
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
Expand Down Expand Up @@ -965,9 +965,11 @@ def dumps(obj, protocol=None):
file.close()


# including pickles unloading functions in this namespace
# including pickle's unloading functions in this namespace
load = pickle.load
loads = pickle.loads
# alias CloudPickler in the namespace as Pickler for consistency with Python's pickle API
Pickler = CloudPickler


# hack for __import__ not working as desired
Expand Down