Skip to content

Commit

Permalink
Merge pull request #120 from 1313e/dill_fix
Browse files Browse the repository at this point in the history
Dill fix
  • Loading branch information
1313e authored Mar 12, 2020
2 parents 66b9500 + 8f813b6 commit 1419bab
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 16 deletions.
11 changes: 2 additions & 9 deletions hickle/hickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,8 @@
if PY3:
file = io.TextIOWrapper

# Import a default 'pickler'
# Not the nicest import code, but should work on Py2/Py3
try:
import dill as pickle
except ImportError:
try:
import cPickle as pickle
except ImportError:
import pickle
# Import dill as pickle
import dill as pickle

try:
from pathlib import Path
Expand Down
6 changes: 6 additions & 0 deletions hickle/loaders/load_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ def load_unicode_dataset(h_node):
def load_none_dataset(h_node):
return None

def load_pickled_data(h_node):
py_type, data = get_type_and_data(h_node)
import dill as pickle
return pickle.loads(data[0])

def load_python_dtype_dataset(h_node):
py_type, data = get_type_and_data(h_node)
subtype = h_node.attrs["python_subdtype"]
Expand Down Expand Up @@ -136,6 +141,7 @@ def load_python_dtype_dataset(h_node):
"python_dtype" : load_python_dtype_dataset,
"string" : load_string_dataset,
"unicode" : load_unicode_dataset,
"pickle" : load_pickled_data,
"none" : load_none_dataset
}

5 changes: 1 addition & 4 deletions hickle/loaders/load_python3.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ def load_none_dataset(h_node):

def load_pickled_data(h_node):
py_type, data = get_type_and_data(h_node)
try:
import cPickle as pickle
except ModuleNotFoundError:
import pickle
import dill as pickle
return pickle.loads(data[0])


Expand Down
17 changes: 17 additions & 0 deletions hickle/tests/test_hickle.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import six
import time
from pprint import pprint
import pytest

from py.path import local

Expand Down Expand Up @@ -42,9 +43,24 @@
}
}

# Define a test function that must be serialized and unpacked again
def func(a, b, c=0):
return(a, b, c)


DUMP_CACHE = [] # Used in test_track_times()


def test_local_func():
""" Dumping and loading a local function """
filename, mode = 'test.h5', 'w'
with pytest.warns(SerializedWarning):
dump(func, filename, mode)
func_hkl = load(filename)
assert type(func) == type(func_hkl)
assert func(1, 2) == func_hkl(1, 2)


def test_string():
""" Dumping and loading a string """
if six.PY2:
Expand Down Expand Up @@ -821,6 +837,7 @@ def test_np_scalar():
test_complex_dict()
test_multi_hickle()
test_dict_int_key()
test_local_func()

# Cleanup
print("ALL TESTS PASSED!")
7 changes: 4 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
h5py
numpy
dill
dill>=0.3.0
h5py>=2.8.0
numpy>=1.8
six>=1.11.0

0 comments on commit 1419bab

Please sign in to comment.