Skip to content

Commit d0451f8

Browse files
author
Scott Sanderson
authored
Merge pull request #145 from cloudpipe/serialize-dict-get
BUG: Handle instance methods of builtin types.
2 parents 03292a3 + 8a41060 commit d0451f8

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

cloudpickle/cloudpickle.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,13 @@ def save_function(self, obj, name=None):
339339
Determines what kind of function obj is (e.g. lambda, defined at
340340
interactive prompt, etc) and handles the pickling appropriately.
341341
"""
342-
if obj in _BUILTIN_TYPE_CONSTRUCTORS:
342+
try:
343+
should_special_case = obj in _BUILTIN_TYPE_CONSTRUCTORS
344+
except TypeError:
345+
# Methods of builtin types aren't hashable in python 2.
346+
should_special_case = False
347+
348+
if should_special_case:
343349
# We keep a special-cased cache of built-in type constructors at
344350
# global scope, because these functions are structured very
345351
# differently in different python versions and implementations (for

tests/cloudpickle_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,12 @@ def test_pickle_reraise(self):
877877
with pytest.raises((exc_type, pickle.PicklingError)):
878878
cloudpickle.dumps(obj)
879879

880+
def test_unhashable_function(self):
881+
d = {'a': 1}
882+
depickled_method = pickle_depickle(d.get)
883+
self.assertEquals(depickled_method('a'), 1)
884+
self.assertEquals(depickled_method('b'), None)
885+
880886

881887
class Protocol2CloudPickleTest(CloudPickleTest):
882888

0 commit comments

Comments
 (0)