Skip to content

Commit 2707c03

Browse files
committed
Add clear loop implementation
1 parent aa1d862 commit 2707c03

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

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,8 @@ 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;
24+
new->base.flags |= NPY_ITEM_IS_POINTER;
2325

2426
return new;
2527
}
@@ -162,6 +164,36 @@ stringdtype_ensure_canonical(StringDTypeObject *self)
162164
return self;
163165
}
164166

167+
static int
168+
stringdtype_clear_loop(void *NPY_UNUSED(traverse_context),
169+
PyArray_Descr *NPY_UNUSED(descr), char *data,
170+
npy_intp size, npy_intp stride,
171+
NpyAuxData *NPY_UNUSED(auxdata))
172+
{
173+
while (size--) {
174+
if (data != NULL) {
175+
free(*(ss **)data);
176+
}
177+
data += stride;
178+
}
179+
180+
return 0;
181+
}
182+
183+
static int
184+
stringdtype_get_clear_loop(void *NPY_UNUSED(traverse_context),
185+
PyArray_Descr *NPY_UNUSED(descr),
186+
int NPY_UNUSED(aligned),
187+
npy_intp NPY_UNUSED(fixed_stride),
188+
traverse_loop_function **out_loop,
189+
NpyAuxData **NPY_UNUSED(out_auxdata),
190+
NPY_ARRAYMETHOD_FLAGS *flags)
191+
{
192+
*flags = NPY_METH_REQUIRES_PYAPI | NPY_METH_NO_FLOATINGPOINT_ERRORS;
193+
*out_loop = &stringdtype_clear_loop;
194+
return 0;
195+
}
196+
165197
static PyType_Slot StringDType_Slots[] = {
166198
{NPY_DT_common_instance, &common_instance},
167199
{NPY_DT_common_dtype, &common_dtype},
@@ -170,6 +202,7 @@ static PyType_Slot StringDType_Slots[] = {
170202
{NPY_DT_setitem, &stringdtype_setitem},
171203
{NPY_DT_getitem, &stringdtype_getitem},
172204
{NPY_DT_ensure_canonical, &stringdtype_ensure_canonical},
205+
{NPY_DT_get_clear_loop, &stringdtype_get_clear_loop},
173206
{0, NULL}};
174207

175208
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(7) < 0) {
93+
if (import_experimental_dtype_api(9) < 0) {
9494
return NULL;
9595
}
9696

0 commit comments

Comments
 (0)