Skip to content

Commit 84470c8

Browse files
committed
adding NPY_METH_REQUIRES_PYAPI flag
1 parent 8db38b0 commit 84470c8

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

quaddtype/numpy_quaddtype/src/casts.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,7 +1321,7 @@ init_casts_internal(void)
13211321
.nin = 1,
13221322
.nout = 1,
13231323
.casting = NPY_UNSAFE_CASTING,
1324-
.flags = NPY_METH_SUPPORTS_UNALIGNED,
1324+
.flags = static_cast<NPY_ARRAYMETHOD_FLAGS>(NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI),
13251325
.dtypes = unicode_to_quad_dtypes,
13261326
.slots = unicode_to_quad_slots,
13271327
};
@@ -1340,7 +1340,7 @@ init_casts_internal(void)
13401340
.nin = 1,
13411341
.nout = 1,
13421342
.casting = NPY_UNSAFE_CASTING,
1343-
.flags = NPY_METH_SUPPORTS_UNALIGNED,
1343+
.flags = static_cast<NPY_ARRAYMETHOD_FLAGS>(NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI),
13441344
.dtypes = quad_to_unicode_dtypes,
13451345
.slots = quad_to_unicode_slots,
13461346
};
@@ -1359,7 +1359,7 @@ init_casts_internal(void)
13591359
.nin = 1,
13601360
.nout = 1,
13611361
.casting = NPY_UNSAFE_CASTING,
1362-
.flags = NPY_METH_SUPPORTS_UNALIGNED,
1362+
.flags = static_cast<NPY_ARRAYMETHOD_FLAGS>(NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI),
13631363
.dtypes = bytes_to_quad_dtypes,
13641364
.slots = bytes_to_quad_slots,
13651365
};
@@ -1378,7 +1378,7 @@ init_casts_internal(void)
13781378
.nin = 1,
13791379
.nout = 1,
13801380
.casting = NPY_UNSAFE_CASTING,
1381-
.flags = NPY_METH_SUPPORTS_UNALIGNED,
1381+
.flags = static_cast<NPY_ARRAYMETHOD_FLAGS>(NPY_METH_SUPPORTS_UNALIGNED | NPY_METH_REQUIRES_PYAPI),
13821382
.dtypes = quad_to_bytes_dtypes,
13831383
.slots = quad_to_bytes_slots,
13841384
};

quaddtype/tests/test_quaddtype.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,18 @@ def test_empty_bytes_raises_error(self):
746746
with pytest.raises(ValueError):
747747
bytes_array.astype(QuadPrecDType())
748748

749+
@pytest.mark.parametrize('dtype', ['S50', 'U50'])
750+
@pytest.mark.parametrize('size', [500, 1000, 10000])
751+
def test_large_array_casting(self, dtype, size):
752+
"""Test long array casting won't lead segfault, GIL enabled"""
753+
arr = np.arange(size).astype(np.float32).astype(dtype)
754+
quad_arr = arr.astype(QuadPrecDType())
755+
assert quad_arr.dtype == QuadPrecDType()
756+
assert quad_arr.size == size
757+
758+
# check roundtrip
759+
roundtrip = quad_arr.astype(dtype)
760+
np.testing.assert_array_equal(arr, roundtrip)
749761

750762
class TestStringParsingEdgeCases:
751763
"""Test edge cases in NumPyOS_ascii_strtoq string parsing"""

0 commit comments

Comments
 (0)