Skip to content

Commit d719f11

Browse files
Scott Sandersonrgbkrk
authored andcommitted
BUG: Handle instancemethods of builtin types.
Fixes a crash in Python 2 when serializing non-hashable instancemethods of built-in types.
1 parent 1105da1 commit d719f11

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
@@ -331,7 +331,13 @@ def save_function(self, obj, name=None):
331331
Determines what kind of function obj is (e.g. lambda, defined at
332332
interactive prompt, etc) and handles the pickling appropriately.
333333
"""
334-
if obj in _BUILTIN_TYPE_CONSTRUCTORS:
334+
try:
335+
should_special_case = obj in _BUILTIN_TYPE_CONSTRUCTORS
336+
except TypeError:
337+
# Methods of builtin types aren't hashable in python 2.
338+
should_special_case = False
339+
340+
if should_special_case:
335341
# We keep a special-cased cache of built-in type constructors at
336342
# global scope, because these functions are structured very
337343
# 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
@@ -835,6 +835,12 @@ def test_pickle_reraise(self):
835835
with pytest.raises((exc_type, pickle.PicklingError)):
836836
cloudpickle.dumps(obj)
837837

838+
def test_unhashable_function(self):
839+
d = {'a': 1}
840+
depickled_method = pickle_depickle(d.get)
841+
self.assertEquals(depickled_method('a'), 1)
842+
self.assertEquals(depickled_method('b'), None)
843+
838844

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

0 commit comments

Comments
 (0)