Skip to content

Commit aa7f796

Browse files
committed
string to string cast resolver returns NPY_NO_CASTING
this makes StringDType() == StringDType() return True
1 parent bd59d33 commit aa7f796

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

stringdtype/stringdtype/src/casts.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,26 @@ string_to_string_resolve_descriptors(PyObject *NPY_UNUSED(self),
77
PyArray_DTypeMeta *NPY_UNUSED(dtypes[2]),
88
PyArray_Descr *given_descrs[2],
99
PyArray_Descr *loop_descrs[2],
10-
npy_intp *NPY_UNUSED(view_offset))
10+
npy_intp *view_offset)
1111
{
12-
Py_INCREF(given_descrs[0]);
13-
loop_descrs[0] = given_descrs[0];
14-
1512
if (given_descrs[1] == NULL) {
16-
loop_descrs[1] = (PyArray_Descr *)new_stringdtype_instance();
13+
StringDTypeObject *new = new_stringdtype_instance();
14+
if (new == NULL) {
15+
return (NPY_CASTING)-1;
16+
}
17+
loop_descrs[1] = (PyArray_Descr *)new;
1718
}
1819
else {
1920
Py_INCREF(given_descrs[1]);
2021
loop_descrs[1] = given_descrs[1];
2122
}
2223

23-
return NPY_SAFE_CASTING;
24+
Py_INCREF(given_descrs[0]);
25+
loop_descrs[0] = given_descrs[0];
26+
27+
*view_offset = 0;
28+
29+
return NPY_NO_CASTING;
2430
}
2531

2632
static int
@@ -59,7 +65,7 @@ PyArrayMethod_Spec StringToStringCastSpec = {
5965
.name = "cast_StringDType_to_StringDType",
6066
.nin = 1,
6167
.nout = 1,
62-
.casting = NPY_UNSAFE_CASTING,
68+
.casting = NPY_NO_CASTING,
6369
.flags = NPY_METH_SUPPORTS_UNALIGNED,
6470
.dtypes = s2s_dtypes,
6571
.slots = s2s_slots,

stringdtype/tests/test_stringdtype.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ def test_dtype_creation():
1717
assert str(StringDType()) == "StringDType"
1818

1919

20+
def test_dtype_equality():
21+
assert StringDType() == StringDType()
22+
assert StringDType() != np.dtype("U")
23+
assert StringDType() != np.dtype("U8")
24+
25+
2026
@pytest.mark.parametrize(
2127
"data",
2228
[

0 commit comments

Comments
 (0)