Skip to content
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
51 changes: 6 additions & 45 deletions asciidtype/asciidtype/src/casts.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ascii_to_ascii_resolve_descriptors(PyObject *NPY_UNUSED(self),
}
else {
Py_INCREF(given_descrs[1]);
loop_descrs[1] = given_descrs[0];
loop_descrs[1] = given_descrs[1];
}

if (((ASCIIDTypeObject *)loop_descrs[0])->size ==
Expand All @@ -38,35 +38,9 @@ ascii_to_ascii_resolve_descriptors(PyObject *NPY_UNUSED(self),
}

static int
ascii_to_ascii_contiguous(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[],
npy_intp const NPY_UNUSED(strides[]),
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr **descrs = context->descriptors;
// for contiguous assignment the sizes of the two dtypes should be
// the same, consider adding an assert to check?
long size = ((ASCIIDTypeObject *)descrs[0])->size;

npy_intp N = dimensions[0] * size;
char *in = data[0];
char *out = data[1];

while (N--) {
*out = *in;
out++;
in++;
}

return 0;
}

static int
ascii_to_ascii_strided_or_unaligned(PyArrayMethod_Context *context,
char *const data[],
npy_intp const dimensions[],
npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
ascii_to_ascii(PyArrayMethod_Context *context, char *const data[],
npy_intp const dimensions[], npy_intp const strides[],
NpyAuxData *NPY_UNUSED(auxdata))
{
PyArray_Descr **descrs = context->descriptors;
long in_size = ((ASCIIDTypeObject *)descrs[0])->size;
Expand All @@ -87,7 +61,7 @@ ascii_to_ascii_strided_or_unaligned(PyArrayMethod_Context *context,
npy_intp out_stride = strides[1];

while (N--) {
memcpy(out, in, out_size * sizeof(char)); // NOLINT
memcpy(out, in, copy_size * sizeof(char)); // NOLINT
for (int i = copy_size; i < out_size; i++) {
*(out + i) = '\0';
}
Expand All @@ -106,20 +80,7 @@ ascii_to_ascii_get_loop(PyArrayMethod_Context *context, int aligned,
NpyAuxData **NPY_UNUSED(out_transferdata),
NPY_ARRAYMETHOD_FLAGS *flags)
{
PyArray_Descr **descrs = context->descriptors;

int contig = (strides[0] == ((ASCIIDTypeObject *)descrs[0])->size *
sizeof(char) &&
strides[1] == ((ASCIIDTypeObject *)descrs[1])->size *
sizeof(char));

if (aligned && contig) {
*out_loop = (PyArrayMethod_StridedLoop *)&ascii_to_ascii_contiguous;
}
else {
*out_loop = (PyArrayMethod_StridedLoop
*)&ascii_to_ascii_strided_or_unaligned;
}
*out_loop = (PyArrayMethod_StridedLoop *)&ascii_to_ascii;

*flags = 0;
return 0;
Expand Down
12 changes: 9 additions & 3 deletions asciidtype/asciidtype/src/dtype.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ asciidtype_setitem(ASCIIDTypeObject *descr, PyObject *obj, char *dataptr)

Py_ssize_t len = PyBytes_Size(value);

size_t copysize;
long copysize;

if (len > descr->size) {
copysize = descr->size;
Expand All @@ -138,7 +138,13 @@ asciidtype_setitem(ASCIIDTypeObject *descr, PyObject *obj, char *dataptr)
static PyObject *
asciidtype_getitem(ASCIIDTypeObject *descr, char *dataptr)
{
PyObject *val_obj = PyUnicode_FromString(dataptr);
char scalar_buffer[descr->size + 1];

memcpy(scalar_buffer, dataptr, descr->size * sizeof(char));

scalar_buffer[descr->size] = '\0';

PyObject *val_obj = PyUnicode_FromString(scalar_buffer);
if (val_obj == NULL) {
return NULL;
}
Expand Down Expand Up @@ -205,7 +211,7 @@ asciidtype_repr(ASCIIDTypeObject *self)
}

static PyMemberDef ASCIIDType_members[] = {
{"size", T_OBJECT_EX, offsetof(ASCIIDTypeObject, size), READONLY,
{"size", T_LONG, offsetof(ASCIIDTypeObject, size), READONLY,
"The number of characters per array element"},
{NULL},
};
Expand Down
3 changes: 3 additions & 0 deletions asciidtype/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ requires = [
]
build-backend = "mesonpy"

[tool.black]
line-length = 79

[project]
name = "asciidtype"
description = "A dtype for ASCII data"
Expand Down
57 changes: 55 additions & 2 deletions asciidtype/tests/test_asciidtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,64 @@ def test_dtype_creation():

def test_scalar_creation():
dtype = ASCIIDType(7)
ASCIIScalar('string', dtype)
ASCIIScalar("string", dtype)


def test_creation_with_explicit_dtype():
dtype = ASCIIDType(7)
arr = np.array(["hello", "this", "is", "an", "array"], dtype=dtype)
assert repr(arr) == (
"array(['hello', 'this', 'is', 'an', 'array'], dtype=ASCIIDType(7))")
"array(['hello', 'this', 'is', 'an', 'array'], dtype=ASCIIDType(7))"
)


def test_creation_truncation():
inp = ["hello", "this", "is", "an", "array"]

dtype = ASCIIDType(5)
arr = np.array(inp, dtype=dtype)
assert repr(arr) == (
"array(['hello', 'this', 'is', 'an', 'array'], dtype=ASCIIDType(5))"
)

dtype = ASCIIDType(4)
arr = np.array(inp, dtype=dtype)
assert repr(arr) == (
"array(['hell', 'this', 'is', 'an', 'arra'], dtype=ASCIIDType(4))"
)

dtype = ASCIIDType(1)
arr = np.array(inp, dtype=dtype)
assert repr(arr) == (
"array(['h', 't', 'i', 'a', 'a'], dtype=ASCIIDType(1))"
)
assert arr.tobytes() == b"htiaa"

# dtype = ASCIIDType()
# arr = np.array(["hello", "this", "is", "an", "array"], dtype=dtype)
# assert repr(arr) == ("array(['', '', '', '', ''], dtype=ASCIIDType(0))")
# assert arr.tobytes() == b""
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are commented out because they depend on numpy/numpy#22763 being merged in numpy.



def test_casting_to_asciidtype():
arr = np.array(["hello", "this", "is", "an", "array"], dtype=ASCIIDType(5))

assert repr(arr.astype(ASCIIDType(7))) == (
"array(['hello', 'this', 'is', 'an', 'array'], dtype=ASCIIDType(7))"
)

assert repr(arr.astype(ASCIIDType(5))) == (
"array(['hello', 'this', 'is', 'an', 'array'], dtype=ASCIIDType(5))"
)

assert repr(arr.astype(ASCIIDType(4))) == (
"array(['hell', 'this', 'is', 'an', 'arra'], dtype=ASCIIDType(4))"
)

assert repr(arr.astype(ASCIIDType(1))) == (
"array(['h', 't', 'i', 'a', 'a'], dtype=ASCIIDType(1))"
)

# assert repr(arr.astype(ASCIIDType())) == (
# "array(['', '', '', '', ''], dtype=ASCIIDType(0))"
# )