Skip to content
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
24 changes: 21 additions & 3 deletions cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,13 @@ def save_global(self, obj, name=None, pack=struct.pack):

def save_instancemethod(self, obj):
# Memoization rarely is ever useful due to python bounding
if PY3:
self.save_reduce(types.MethodType, (obj.__func__, obj.__self__), obj=obj)
if obj.__self__ is None:
self.save_reduce(getattr, (obj.im_class, obj.__name__))
else:
self.save_reduce(types.MethodType, (obj.__func__, obj.__self__, obj.__self__.__class__),
if PY3:
self.save_reduce(types.MethodType, (obj.__func__, obj.__self__), obj=obj)
else:
self.save_reduce(types.MethodType, (obj.__func__, obj.__self__, obj.__self__.__class__),
obj=obj)
dispatch[types.MethodType] = save_instancemethod

Expand Down Expand Up @@ -698,3 +701,18 @@ def _make_skel_func(code, closures, base_globals = None):
def _getobject(modname, attribute):
mod = __import__(modname, fromlist=[attribute])
return mod.__dict__[attribute]


""" Use copy_reg to extend global pickle definitions """
Copy link
Contributor

Choose a reason for hiding this comment

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

Style: better use # instead of inline string objects for commenting the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Noted. Will do this in the future.


if sys.version_info < (3, 4):
method_descriptor = type(str.upper)

def _reduce_method_descriptor(obj):
return (getattr, (obj.__objclass__, obj.__name__))

try:
import copy_reg as copyreg
except ImportError:
import copyreg
copyreg.pickle(method_descriptor, _reduce_method_descriptor)
16 changes: 16 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,5 +237,21 @@ def test_cm(cls):
self.assertEqual(A.test_sm(), "sm")
self.assertEqual(A.test_cm(), "cm")

def test_method_descriptors(self):
f = pickle_depickle(str.upper)
self.assertEqual(f('abc'), 'ABC')

def test_instancemethods_without_self(self):
class F(object):
def f(self, x):
return x + 1

g = pickle_depickle(F.f)
self.assertEqual(g.__name__, F.f.__name__)
if sys.version_info[0] < 3:
self.assertEqual(g.im_class.__name__, F.f.im_class.__name__)
# self.assertEqual(g(F(), 1), 2) # still fails


if __name__ == '__main__':
unittest.main()