Skip to content

Commit 457fc76

Browse files
committed
use NPY_LIST_PICKLE flag to support picking SringDType
1 parent a790e5e commit 457fc76

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

stringdtype/stringdtype/src/dtype.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ new_stringdtype_instance(void)
1919
new->base.elsize = sizeof(ss *);
2020
new->base.alignment = _Alignof(ss *);
2121
new->base.flags |= NPY_NEEDS_INIT;
22+
new->base.flags |= NPY_LIST_PICKLE;
2223

2324
return new;
2425
}

stringdtype/tests/test_stringdtype.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import concurrent.futures
12
import os
23
import pickle
34
import tempfile
@@ -129,15 +130,34 @@ def test_memory_usage(string_list):
129130
_memory_usage(np.array([1, 2, 3]))
130131

131132

132-
def test_pickle_dtype():
133+
def _pickle_load(filename):
134+
with open(filename, "rb") as f:
135+
res = pickle.load(f)
136+
137+
return res
138+
139+
140+
def test_pickle(string_list):
133141
dtype = StringDType()
134142

143+
arr = np.array(string_list, dtype=dtype)
144+
135145
with tempfile.NamedTemporaryFile("wb", delete=False) as f:
136-
pickle.dump(dtype, f)
146+
pickle.dump([arr, dtype], f)
137147

138148
with open(f.name, "rb") as f:
139-
load_dtype = pickle.load(f)
149+
res = pickle.load(f)
150+
151+
np.testing.assert_array_equal(res[0], arr)
152+
assert res[1] == dtype
153+
154+
# load the pickle in a subprocess to ensure the string data are
155+
# actually stored in the pickle file
156+
with concurrent.futures.ProcessPoolExecutor() as executor:
157+
e = executor.submit(_pickle_load, f.name)
158+
res = e.result()
140159

141-
assert dtype == load_dtype
160+
np.testing.assert_array_equal(res[0], arr)
161+
assert res[1] == dtype
142162

143163
os.remove(f.name)

0 commit comments

Comments
 (0)