Skip to content

Commit fae5b53

Browse files
authored
Merge pull request #52 from ngoldbaum/fix-free
free output strings before setting them in string to unicode cast
2 parents 3489135 + e6de6db commit fae5b53

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
213213
return -1;
214214
}
215215
ss *out_ss = (ss *)out;
216+
ssfree(out_ss);
216217
if (ssnewemptylen(out_num_bytes, out_ss) < 0) {
217218
gil_error(PyExc_MemoryError, "ssnewemptylen failed");
218219
return -1;

stringdtype/tests/test_stringdtype.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import concurrent.futures
22
import os
33
import pickle
4+
import string
45
import tempfile
56

67
import numpy as np
@@ -81,7 +82,6 @@ def test_unicode_casts(string_list):
8182
np.testing.assert_array_equal(arr, expected)
8283

8384
arr = np.array(string_list, dtype=StringDType())
84-
8585
np.testing.assert_array_equal(
8686
arr.astype("U8"), np.array(string_list, dtype="U8")
8787
)
@@ -95,6 +95,17 @@ def test_unicode_casts(string_list):
9595
)
9696

9797

98+
def test_additional_unicode_cast(string_list):
99+
RANDS_CHARS = np.array(
100+
list(string.ascii_letters + string.digits), dtype=(np.str_, 1)
101+
)
102+
arr = np.random.choice(RANDS_CHARS, size=10 * 100_000, replace=True).view(
103+
"U10"
104+
)
105+
np.testing.assert_array_equal(arr, arr.astype(StringDType()))
106+
np.testing.assert_array_equal(arr, arr.astype(StringDType()).astype("U10"))
107+
108+
98109
def test_insert_scalar(string_list):
99110
dtype = StringDType()
100111
arr = np.array(string_list, dtype=dtype)

0 commit comments

Comments
 (0)