Skip to content

Commit

Permalink
BUG: Handle instancemethods of builtin types.
Browse files Browse the repository at this point in the history
Fixes a crash in Python 2 when serializing non-hashable instancemethods
of built-in types.
  • Loading branch information
Scott Sanderson authored and rgbkrk committed Feb 6, 2018
1 parent 1105da1 commit d719f11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cloudpickle/cloudpickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,13 @@ def save_function(self, obj, name=None):
Determines what kind of function obj is (e.g. lambda, defined at
interactive prompt, etc) and handles the pickling appropriately.
"""
if obj in _BUILTIN_TYPE_CONSTRUCTORS:
try:
should_special_case = obj in _BUILTIN_TYPE_CONSTRUCTORS
except TypeError:
# Methods of builtin types aren't hashable in python 2.
should_special_case = False

if should_special_case:
# We keep a special-cased cache of built-in type constructors at
# global scope, because these functions are structured very
# differently in different python versions and implementations (for
Expand Down
6 changes: 6 additions & 0 deletions tests/cloudpickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,12 @@ def test_pickle_reraise(self):
with pytest.raises((exc_type, pickle.PicklingError)):
cloudpickle.dumps(obj)

def test_unhashable_function(self):
d = {'a': 1}
depickled_method = pickle_depickle(d.get)
self.assertEquals(depickled_method('a'), 1)
self.assertEquals(depickled_method('b'), None)


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

0 comments on commit d719f11

Please sign in to comment.