diff options
| author | Charles Harris <charlesr.harris@gmail.com> | 2022-11-07 12:59:51 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-11-07 12:59:51 -0600 |
| commit | 93991066060a2f7384ecc7992e145a56899d81b0 (patch) | |
| tree | 3f5e4d3c3d187466680155351cdfb67be72f6337 /numpy | |
| parent | fcafb6560e37c948a594dce36d300888148bc599 (diff) | |
| parent | 26342d7f117ed5759fefdaba7e60569e674f9a12 (diff) | |
| download | numpy-93991066060a2f7384ecc7992e145a56899d81b0.tar.gz | |
Merge pull request #22542 from seberg/dlpack-buffererror
API: Always use BufferError when dlpack export fails
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/core/src/multiarray/dlpack.c | 31 | ||||
| -rw-r--r-- | numpy/core/tests/test_dlpack.py | 10 |
2 files changed, 22 insertions, 19 deletions
diff --git a/numpy/core/src/multiarray/dlpack.c b/numpy/core/src/multiarray/dlpack.c index 5a46d5a85..b20674f5e 100644 --- a/numpy/core/src/multiarray/dlpack.c +++ b/numpy/core/src/multiarray/dlpack.c @@ -133,14 +133,15 @@ array_dlpack(PyArrayObject *self, } if (stream != Py_None) { - PyErr_SetString(PyExc_RuntimeError, "NumPy only supports " - "stream=None."); + PyErr_SetString(PyExc_RuntimeError, + "NumPy only supports stream=None."); return NULL; } if ( !(PyArray_FLAGS(self) & NPY_ARRAY_WRITEABLE)) { - PyErr_SetString(PyExc_TypeError, "NumPy currently only supports " - "dlpack for writeable arrays"); + PyErr_SetString(PyExc_BufferError, + "Cannot export readonly array since signalling readonly " + "is unsupported by DLPack."); return NULL; } @@ -152,7 +153,7 @@ array_dlpack(PyArrayObject *self, if (!PyArray_IS_C_CONTIGUOUS(self) && PyArray_SIZE(self) != 1) { for (int i = 0; i < ndim; ++i) { if (shape[i] != 1 && strides[i] % itemsize != 0) { - PyErr_SetString(PyExc_RuntimeError, + PyErr_SetString(PyExc_BufferError, "DLPack only supports strides which are a multiple of " "itemsize."); return NULL; @@ -164,8 +165,8 @@ array_dlpack(PyArrayObject *self, PyArray_Descr *dtype = PyArray_DESCR(self); if (PyDataType_ISBYTESWAPPED(dtype)) { - PyErr_SetString(PyExc_TypeError, "DLPack only supports native " - "byte swapping."); + PyErr_SetString(PyExc_BufferError, + "DLPack only supports native byte order."); return NULL; } @@ -182,8 +183,9 @@ array_dlpack(PyArrayObject *self, // We can't be sure that the dtype is // IEEE or padded. if (itemsize > 8) { - PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " - "floating point types without padding."); + PyErr_SetString(PyExc_BufferError, + "DLPack only supports IEEE floating point types " + "without padding (longdouble typically is not IEEE)."); return NULL; } managed_dtype.code = kDLFloat; @@ -192,16 +194,17 @@ array_dlpack(PyArrayObject *self, // We can't be sure that the dtype is // IEEE or padded. if (itemsize > 16) { - PyErr_SetString(PyExc_TypeError, "DLPack only supports IEEE " - "complex point types without padding."); + PyErr_SetString(PyExc_BufferError, + "DLPack only supports IEEE floating point types " + "without padding (longdouble typically is not IEEE)."); return NULL; } managed_dtype.code = kDLComplex; } else { - PyErr_SetString(PyExc_TypeError, - "DLPack only supports signed/unsigned integers, float " - "and complex dtypes."); + PyErr_SetString(PyExc_BufferError, + "DLPack only supports signed/unsigned integers, float " + "and complex dtypes."); return NULL; } diff --git a/numpy/core/tests/test_dlpack.py b/numpy/core/tests/test_dlpack.py index 717210b54..278bdd12d 100644 --- a/numpy/core/tests/test_dlpack.py +++ b/numpy/core/tests/test_dlpack.py @@ -26,7 +26,7 @@ class TestDLPack: y = np.zeros((5,), dtype=dt) z = y['int'] - with pytest.raises(RuntimeError): + with pytest.raises(BufferError): np.from_dlpack(z) @pytest.mark.skipif(IS_PYPY, reason="PyPy can't get refcounts.") @@ -53,14 +53,14 @@ class TestDLPack: def test_invalid_dtype(self): x = np.asarray(np.datetime64('2021-05-27')) - with pytest.raises(TypeError): + with pytest.raises(BufferError): np.from_dlpack(x) def test_invalid_byte_swapping(self): dt = np.dtype('=i8').newbyteorder() x = np.arange(5, dtype=dt) - with pytest.raises(TypeError): + with pytest.raises(BufferError): np.from_dlpack(x) def test_non_contiguous(self): @@ -100,7 +100,7 @@ class TestDLPack: x = np.arange(5) _ = x.__dlpack__() raise RuntimeError - + def test_dlpack_destructor_exception(self): with pytest.raises(RuntimeError): self.dlpack_deleter_exception() @@ -108,7 +108,7 @@ class TestDLPack: def test_readonly(self): x = np.arange(5) x.flags.writeable = False - with pytest.raises(TypeError): + with pytest.raises(BufferError): x.__dlpack__() def test_ndim0(self): |
