From f9d472430225e6d6a83b1a54faa40c4150d92e55 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 29 Aug 2023 15:06:21 -0600 Subject: [PATCH 1/4] expand ssdup docstring --- stringdtype/stringdtype/src/static_string.h | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/stringdtype/stringdtype/src/static_string.h b/stringdtype/stringdtype/src/static_string.h index 25acd2b9..0e77ded1 100644 --- a/stringdtype/stringdtype/src/static_string.h +++ b/stringdtype/stringdtype/src/static_string.h @@ -22,9 +22,10 @@ ssnewlen(const char *init, size_t len, ss *to_init); void ssfree(ss *str); -// copies the contents out *in* into *out*. Allocates a new string buffer for -// *out*. Returns -1 if malloc fails and -2 if *out* is not empty. Returns 0 on -// success. +// Copies the contents of *in* into *out*. Allocates a new string buffer for +// *out* and assumes that *out* is uninitialized. Returns -1 if malloc fails +// and -2 if *out* is not initialized. ssfree *must* be called before this is +// called if *in* points to an existing string. Returns 0 on success. int ssdup(const ss *in, ss *out); From 7a8528a3a1aeaa29f9630bf1046dd19cfced5bba Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 29 Aug 2023 15:45:20 -0600 Subject: [PATCH 2/4] deal with removal of np.unicode_ --- stringdtype/tests/test_char.py | 2 +- stringdtype/tests/test_stringdtype.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/stringdtype/tests/test_char.py b/stringdtype/tests/test_char.py index a5e92efd..45707fe1 100644 --- a/stringdtype/tests/test_char.py +++ b/stringdtype/tests/test_char.py @@ -14,7 +14,7 @@ def string_array(): @pytest.fixture def unicode_array(): - return np.array(TEST_DATA, dtype=np.unicode_) + return np.array(TEST_DATA, dtype=np.str_) UNARY_FUNCTIONS = [ diff --git a/stringdtype/tests/test_stringdtype.py b/stringdtype/tests/test_stringdtype.py index e14fe0be..5bf8b27b 100644 --- a/stringdtype/tests/test_stringdtype.py +++ b/stringdtype/tests/test_stringdtype.py @@ -145,7 +145,7 @@ def test_scalars_string_conversion(data, dtype): ], ) def test_unicode_casts(dtype, strings): - arr = np.array(strings, dtype=np.unicode_).astype(dtype) + arr = np.array(strings, dtype=np.str_).astype(dtype) expected = np.array(strings, dtype=dtype) np.testing.assert_array_equal(arr, expected) From 8ab07ef3a9852eaeaeaf5dc2377dc3c1455226f1 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Tue, 29 Aug 2023 15:46:25 -0600 Subject: [PATCH 3/4] use PyMem allocators instead of malloc/free --- stringdtype/stringdtype/src/casts.c | 6 +++--- stringdtype/stringdtype/src/dtype.c | 6 ++++-- stringdtype/stringdtype/src/static_string.c | 8 +++++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/stringdtype/stringdtype/src/casts.c b/stringdtype/stringdtype/src/casts.c index 9d8dece5..d57ab303 100644 --- a/stringdtype/stringdtype/src/casts.c +++ b/stringdtype/stringdtype/src/casts.c @@ -1171,7 +1171,7 @@ get_cast_spec(const char *name, NPY_CASTING casting, NPY_ARRAYMETHOD_FLAGS flags, PyArray_DTypeMeta **dtypes, PyType_Slot *slots) { - PyArrayMethod_Spec *ret = malloc(sizeof(PyArrayMethod_Spec)); + PyArrayMethod_Spec *ret = PyMem_Malloc(sizeof(PyArrayMethod_Spec)); ret->name = name; ret->nin = 1; @@ -1187,7 +1187,7 @@ get_cast_spec(const char *name, NPY_CASTING casting, PyArray_DTypeMeta ** get_dtypes(PyArray_DTypeMeta *dt1, PyArray_DTypeMeta *dt2) { - PyArray_DTypeMeta **ret = malloc(2 * sizeof(PyArray_DTypeMeta *)); + PyArray_DTypeMeta **ret = PyMem_Malloc(2 * sizeof(PyArray_DTypeMeta *)); ret[0] = dt1; ret[1] = dt2; @@ -1297,7 +1297,7 @@ get_casts() dt2s_dtypes, dt2s_slots); PyArrayMethod_Spec **casts = - malloc((num_casts + 1) * sizeof(PyArrayMethod_Spec *)); + PyMem_Malloc((num_casts + 1) * sizeof(PyArrayMethod_Spec *)); int cast_i = 0; diff --git a/stringdtype/stringdtype/src/dtype.c b/stringdtype/stringdtype/src/dtype.c index af29587a..333306b5 100644 --- a/stringdtype/stringdtype/src/dtype.c +++ b/stringdtype/stringdtype/src/dtype.c @@ -788,10 +788,12 @@ init_string_dtype(void) StringDType.base.singleton = singleton; for (int i = 0; StringDType_casts[i] != NULL; i++) { - free(StringDType_casts[i]->dtypes); - free(StringDType_casts[i]); + PyMem_Free(StringDType_casts[i]->dtypes); + PyMem_Free(StringDType_casts[i]); } + PyMem_Free(StringDType_casts); + return 0; } diff --git a/stringdtype/stringdtype/src/static_string.c b/stringdtype/stringdtype/src/static_string.c index 9fc965b7..7cc48ce5 100644 --- a/stringdtype/stringdtype/src/static_string.c +++ b/stringdtype/stringdtype/src/static_string.c @@ -1,3 +1,5 @@ +#include "Python.h" + #include "static_string.h" // defined this way so it has an in-memory representation that is distinct @@ -16,7 +18,7 @@ ssnewlen(const char *init, size_t len, ss *to_init) return 0; } - char *ret_buf = (char *)malloc(sizeof(char) * len); + char *ret_buf = (char *)PyMem_RawMalloc(sizeof(char) * len); if (ret_buf == NULL) { return -1; @@ -35,7 +37,7 @@ void ssfree(ss *str) { if (str->buf != NULL && str->buf != EMPTY_STRING.buf) { - free(str->buf); + PyMem_RawFree(str->buf); str->buf = NULL; } str->len = 0; @@ -68,7 +70,7 @@ ssnewemptylen(size_t num_bytes, ss *out) return 0; } - char *buf = (char *)malloc(sizeof(char) * num_bytes); + char *buf = (char *)PyMem_RawMalloc(sizeof(char) * num_bytes); if (buf == NULL) { return -1; From 58ac555eab08ad2ad7859d6a26b369be91031146 Mon Sep 17 00:00:00 2001 From: Nathan Goldbaum Date: Wed, 30 Aug 2023 09:29:28 -0600 Subject: [PATCH 4/4] remove usage of np.unicode_ from asciidtype --- asciidtype/tests/test_asciidtype.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/asciidtype/tests/test_asciidtype.py b/asciidtype/tests/test_asciidtype.py index 8e7c7ba4..4c44b9d5 100644 --- a/asciidtype/tests/test_asciidtype.py +++ b/asciidtype/tests/test_asciidtype.py @@ -157,7 +157,7 @@ def test_casting_safety(): def test_unicode_to_ascii_to_unicode(): arr = np.array(["hello", "this", "is", "an", "array"]) ascii_arr = arr.astype(ASCIIDType(5)) - for dtype in ["U5", np.unicode_, np.str_]: + for dtype in ["U5", np.str_]: round_trip_arr = ascii_arr.astype(dtype) np.testing.assert_array_equal(arr, round_trip_arr)