Skip to content

Commit

Permalink
Some cleanups, fix memoryview support
Browse files Browse the repository at this point in the history
  • Loading branch information
pitrou committed Oct 26, 2017
1 parent 2da4c24 commit f8187e9
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 48 deletions.
68 changes: 20 additions & 48 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,12 @@ def dump(self, obj):
raise pickle.PicklingError(msg)

def save_memoryview(self, obj):
"""Fallback to save_string"""
Pickler.save_string(self, str(obj))
self.save(obj.tobytes())
dispatch[memoryview] = save_memoryview

def save_buffer(self, obj):
"""Fallback to save_string"""
Pickler.save_string(self,str(obj))
if PY3:
dispatch[memoryview] = save_memoryview
else:
if not PY3:
def save_buffer(self, obj):
self.save(str(obj))
dispatch[buffer] = save_buffer

def save_unsupported(self, obj):
Expand Down Expand Up @@ -387,7 +384,7 @@ def save_function(self, obj, name=None):
rv = (getattr, (obj.__self__, name))
else:
raise pickle.PicklingError("Can't pickle %r" % obj)
return Pickler.save_reduce(self, obj=obj, *rv)
return self.save_reduce(obj=obj, *rv)

# if func is lambda, def'ed at prompt, is in main, or is nested, then
# we'll pickle the actual function object rather than simply saving a
Expand Down Expand Up @@ -477,18 +474,12 @@ def save_dynamic_class(self, obj):
# Push the rehydration function.
save(_rehydrate_skeleton_class)

# Mark the start of the args for the rehydration function.
# Mark the start of the args tuple for the rehydration function.
write(pickle.MARK)

# Create and memoize an empty class with obj's name and bases.
save(type(obj))
save((
obj.__name__,
obj.__bases__,
type_kwargs,
))
write(pickle.REDUCE)
self.memoize(obj)
# Create and memoize an skeleton class with obj's name and bases.
tp = type(obj)
self.save_reduce(tp, (obj.__name__, obj.__bases__, type_kwargs), obj=obj)

# Now save the rest of obj's __dict__. Any references to obj
# encountered while saving will point to the skeleton class.
Expand Down Expand Up @@ -627,37 +618,18 @@ def save_global(self, obj, name=None, pack=struct.pack):
The name of this method is somewhat misleading: all types get
dispatched here.
"""
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)

if name is None:
name = obj.__name__

modname = getattr(obj, "__module__", None)
if modname is None:
try:
# whichmodule() could fail, see
# https://bitbucket.org/gutworth/six/issues/63/importing-six-breaks-pickling
modname = pickle.whichmodule(obj, name)
except Exception:
modname = '__main__'

if modname == '__main__':
themodule = None
else:
__import__(modname)
themodule = sys.modules[modname]
self.modules.add(themodule)
try:
return Pickler.save_global(self, obj, name=name)
except Exception:
if obj.__module__ == "__builtin__" or obj.__module__ == "builtins":
if obj in _BUILTIN_TYPE_NAMES:
return self.save_reduce(_builtin_type, (_BUILTIN_TYPE_NAMES[obj],), obj=obj)

if hasattr(themodule, name) and getattr(themodule, name) is obj:
return Pickler.save_global(self, obj, name)
typ = type(obj)
if typ is not obj and isinstance(obj, (type, types.ClassType)):
return self.save_dynamic_class(obj)

typ = type(obj)
if typ is not obj and isinstance(obj, (type, types.ClassType)):
self.save_dynamic_class(obj)
else:
raise pickle.PicklingError("Can't pickle %r" % obj)
raise

dispatch[type] = save_global
dispatch[types.ClassType] = save_global
Expand Down
4 changes: 4 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ def test_buffer(self):
except NameError: # Python 3 does no longer support buffers
pass

def test_memoryview(self):
buffer_obj = memoryview(b"Hello")
self.assertEqual(pickle_depickle(buffer_obj), buffer_obj.tobytes())

def test_lambda(self):
self.assertEqual(pickle_depickle(lambda: 1)(), 1)

Expand Down

0 comments on commit f8187e9

Please sign in to comment.