Skip to content

Add clear loop implementation #40

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion asciidtype/asciidtype/src/asciidtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ PyInit__asciidtype_main(void)
return NULL;
}

if (import_experimental_dtype_api(8) < 0) {
if (import_experimental_dtype_api(9) < 0) {
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion metadatadtype/metadatadtype/src/metadatadtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ PyInit__metadatadtype_main(void)
if (_import_array() < 0) {
return NULL;
}
if (import_experimental_dtype_api(8) < 0) {
if (import_experimental_dtype_api(9) < 0) {
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion mpfdtype/mpfdtype/src/mpfdtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ PyInit__mpfdtype_main(void)
if (_import_array() < 0) {
return NULL;
}
if (import_experimental_dtype_api(8) < 0) {
if (import_experimental_dtype_api(9) < 0) {
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion quaddtype/quaddtype/src/quaddtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PyInit__quaddtype_main(void)
return NULL;

// Fail to init if the experimental DType API version 5 isn't supported
if (import_experimental_dtype_api(8) < 0) {
if (import_experimental_dtype_api(9) < 0) {
PyErr_SetString(PyExc_ImportError,
"Error encountered importing the experimental dtype API.");
return NULL;
Expand Down
1 change: 0 additions & 1 deletion stringdtype/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ py.install_sources(
py.extension_module(
'_main',
srcs,
c_args: ['-g', '-O0', '-pg'],
install: true,
subdir: 'stringdtype',
include_directories: includes
Expand Down
6 changes: 6 additions & 0 deletions stringdtype/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ dependencies = [
[tool.ruff]
line-length = 79
per-file-ignores = {"__init__.py" = ["F401"]}

[tool.meson-python.args]
dist = []
setup = ["-Ddebug=true", "-Doptimization=0"]
compile = []
install = []
3 changes: 2 additions & 1 deletion stringdtype/stringdtype/src/casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,10 @@ unicode_to_string(PyArrayMethod_Context *context, char *const data[],
ss *out_ss = ssnewempty(out_num_bytes);
if (out_ss == NULL) {
gil_error(PyExc_MemoryError, "ssnewempty failed");
return -1;
}
char *out_buf = out_ss->buf;
for (int i = 0; i < num_codepoints; i++) {
for (size_t i = 0; i < num_codepoints; i++) {
// get code point
Py_UCS4 code = in[i];

Expand Down
33 changes: 33 additions & 0 deletions stringdtype/stringdtype/src/dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ new_stringdtype_instance(void)
new->base.alignment = _Alignof(ss *);
new->base.flags |= NPY_NEEDS_INIT;
new->base.flags |= NPY_LIST_PICKLE;
new->base.flags |= NPY_ITEM_REFCOUNT;

return new;
}
Expand Down Expand Up @@ -172,6 +173,37 @@ stringdtype_ensure_canonical(StringDTypeObject *self)
return self;
}

static int
stringdtype_clear_loop(void *NPY_UNUSED(traverse_context),
PyArray_Descr *NPY_UNUSED(descr), char *data,
npy_intp size, npy_intp stride,
NpyAuxData *NPY_UNUSED(auxdata))
{
while (size--) {
if (data != NULL) {
free(*(ss **)data);
*(ss **)data = NULL;
}
data += stride;
}

return 0;
}

static int
stringdtype_get_clear_loop(void *NPY_UNUSED(traverse_context),
PyArray_Descr *NPY_UNUSED(descr),
int NPY_UNUSED(aligned),
npy_intp NPY_UNUSED(fixed_stride),
traverse_loop_function **out_loop,
NpyAuxData **NPY_UNUSED(out_auxdata),
NPY_ARRAYMETHOD_FLAGS *flags)
{
*flags = NPY_METH_NO_FLOATINGPOINT_ERRORS;
*out_loop = &stringdtype_clear_loop;
return 0;
}

static PyType_Slot StringDType_Slots[] = {
{NPY_DT_common_instance, &common_instance},
{NPY_DT_common_dtype, &common_dtype},
Expand All @@ -181,6 +213,7 @@ static PyType_Slot StringDType_Slots[] = {
{NPY_DT_getitem, &stringdtype_getitem},
{NPY_DT_ensure_canonical, &stringdtype_ensure_canonical},
{NPY_DT_PyArray_ArrFuncs_compare, &compare_strings},
{NPY_DT_get_clear_loop, &stringdtype_get_clear_loop},
{0, NULL}};

static PyObject *
Expand Down
8 changes: 4 additions & 4 deletions stringdtype/stringdtype/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ _memory_usage(PyObject *NPY_UNUSED(self), PyObject *obj)
return NULL;
}

NpyIter *iter =
NpyIter_New(arr, NPY_ITER_READONLY | NPY_ITER_EXTERNAL_LOOP,
NPY_KEEPORDER, NPY_NO_CASTING, NULL);
NpyIter *iter = NpyIter_New(
arr, NPY_ITER_READONLY | NPY_ITER_EXTERNAL_LOOP | NPY_ITER_REFS_OK,
NPY_KEEPORDER, NPY_NO_CASTING, NULL);

if (iter == NULL) {
return NULL;
Expand Down Expand Up @@ -90,7 +90,7 @@ PyInit__main(void)
if (_import_array() < 0) {
return NULL;
}
if (import_experimental_dtype_api(8) < 0) {
if (import_experimental_dtype_api(9) < 0) {
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion unytdtype/unytdtype/src/unytdtype_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ PyInit__unytdtype_main(void)
if (_import_array() < 0) {
return NULL;
}
if (import_experimental_dtype_api(8) < 0) {
if (import_experimental_dtype_api(9) < 0) {
return NULL;
}

Expand Down