Skip to content

Commit 21c5618

Browse files
authored
Merge pull request #40 from ngoldbaum/string-clear
Add clear loop implementation
2 parents 988da4b + 0eee013 commit 21c5618

File tree

10 files changed

+50
-11
lines changed

10 files changed

+50
-11
lines changed

asciidtype/asciidtype/src/asciidtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PyInit__asciidtype_main(void)
2222
return NULL;
2323
}
2424

25-
if (import_experimental_dtype_api(8) < 0) {
25+
if (import_experimental_dtype_api(9) < 0) {
2626
return NULL;
2727
}
2828

metadatadtype/metadatadtype/src/metadatadtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PyInit__metadatadtype_main(void)
2121
if (_import_array() < 0) {
2222
return NULL;
2323
}
24-
if (import_experimental_dtype_api(8) < 0) {
24+
if (import_experimental_dtype_api(9) < 0) {
2525
return NULL;
2626
}
2727

mpfdtype/mpfdtype/src/mpfdtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ PyInit__mpfdtype_main(void)
2222
if (_import_array() < 0) {
2323
return NULL;
2424
}
25-
if (import_experimental_dtype_api(8) < 0) {
25+
if (import_experimental_dtype_api(9) < 0) {
2626
return NULL;
2727
}
2828

quaddtype/quaddtype/src/quaddtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ PyInit__quaddtype_main(void)
2323
return NULL;
2424

2525
// Fail to init if the experimental DType API version 5 isn't supported
26-
if (import_experimental_dtype_api(8) < 0) {
26+
if (import_experimental_dtype_api(9) < 0) {
2727
PyErr_SetString(PyExc_ImportError,
2828
"Error encountered importing the experimental dtype API.");
2929
return NULL;

stringdtype/meson.build

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ py.install_sources(
4343
py.extension_module(
4444
'_main',
4545
srcs,
46-
c_args: ['-g', '-O0', '-pg'],
4746
install: true,
4847
subdir: 'stringdtype',
4948
include_directories: includes

stringdtype/pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ dependencies = [
2828
[tool.ruff]
2929
line-length = 79
3030
per-file-ignores = {"__init__.py" = ["F401"]}
31+
32+
[tool.meson-python.args]
33+
dist = []
34+
setup = ["-Ddebug=true", "-Doptimization=0"]
35+
compile = []
36+
install = []

stringdtype/stringdtype/src/casts.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,10 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
220220
ss *out_ss = ssnewempty(out_num_bytes);
221221
if (out_ss == NULL) {
222222
gil_error(PyExc_MemoryError, "ssnewempty failed");
223+
return -1;
223224
}
224225
char *out_buf = out_ss->buf;
225-
for (int i = 0; i < num_codepoints; i++) {
226+
for (size_t i = 0; i < num_codepoints; i++) {
226227
// get code point
227228
Py_UCS4 code = in[i];
228229

stringdtype/stringdtype/src/dtype.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ new_stringdtype_instance(void)
2020
new->base.alignment = _Alignof(ss *);
2121
new->base.flags |= NPY_NEEDS_INIT;
2222
new->base.flags |= NPY_LIST_PICKLE;
23+
new->base.flags |= NPY_ITEM_REFCOUNT;
2324

2425
return new;
2526
}
@@ -172,6 +173,37 @@ stringdtype_ensure_canonical(StringDTypeObject *self)
172173
return self;
173174
}
174175

176+
static int
177+
stringdtype_clear_loop(void *NPY_UNUSED(traverse_context),
178+
PyArray_Descr *NPY_UNUSED(descr), char *data,
179+
npy_intp size, npy_intp stride,
180+
NpyAuxData *NPY_UNUSED(auxdata))
181+
{
182+
while (size--) {
183+
if (data != NULL) {
184+
free(*(ss **)data);
185+
*(ss **)data = NULL;
186+
}
187+
data += stride;
188+
}
189+
190+
return 0;
191+
}
192+
193+
static int
194+
stringdtype_get_clear_loop(void *NPY_UNUSED(traverse_context),
195+
PyArray_Descr *NPY_UNUSED(descr),
196+
int NPY_UNUSED(aligned),
197+
npy_intp NPY_UNUSED(fixed_stride),
198+
traverse_loop_function **out_loop,
199+
NpyAuxData **NPY_UNUSED(out_auxdata),
200+
NPY_ARRAYMETHOD_FLAGS *flags)
201+
{
202+
*flags = NPY_METH_NO_FLOATINGPOINT_ERRORS;
203+
*out_loop = &stringdtype_clear_loop;
204+
return 0;
205+
}
206+
175207
static PyType_Slot StringDType_Slots[] = {
176208
{NPY_DT_common_instance, &common_instance},
177209
{NPY_DT_common_dtype, &common_dtype},
@@ -181,6 +213,7 @@ static PyType_Slot StringDType_Slots[] = {
181213
{NPY_DT_getitem, &stringdtype_getitem},
182214
{NPY_DT_ensure_canonical, &stringdtype_ensure_canonical},
183215
{NPY_DT_PyArray_ArrFuncs_compare, &compare_strings},
216+
{NPY_DT_get_clear_loop, &stringdtype_get_clear_loop},
184217
{0, NULL}};
185218

186219
static PyObject *

stringdtype/stringdtype/src/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ _memory_usage(PyObject *NPY_UNUSED(self), PyObject *obj)
2929
return NULL;
3030
}
3131

32-
NpyIter *iter =
33-
NpyIter_New(arr, NPY_ITER_READONLY | NPY_ITER_EXTERNAL_LOOP,
34-
NPY_KEEPORDER, NPY_NO_CASTING, NULL);
32+
NpyIter *iter = NpyIter_New(
33+
arr, NPY_ITER_READONLY | NPY_ITER_EXTERNAL_LOOP | NPY_ITER_REFS_OK,
34+
NPY_KEEPORDER, NPY_NO_CASTING, NULL);
3535

3636
if (iter == NULL) {
3737
return NULL;
@@ -90,7 +90,7 @@ PyInit__main(void)
9090
if (_import_array() < 0) {
9191
return NULL;
9292
}
93-
if (import_experimental_dtype_api(8) < 0) {
93+
if (import_experimental_dtype_api(9) < 0) {
9494
return NULL;
9595
}
9696

unytdtype/unytdtype/src/unytdtype_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ PyInit__unytdtype_main(void)
2121
if (_import_array() < 0) {
2222
return NULL;
2323
}
24-
if (import_experimental_dtype_api(8) < 0) {
24+
if (import_experimental_dtype_api(9) < 0) {
2525
return NULL;
2626
}
2727

0 commit comments

Comments
 (0)