diff options
Diffstat (limited to 'numpy')
| -rw-r--r-- | numpy/core/src/multiarray/_multiarray_tests.c.src | 18 | ||||
| -rw-r--r-- | numpy/core/tests/test_conversion_utils.py | 30 |
2 files changed, 48 insertions, 0 deletions
diff --git a/numpy/core/src/multiarray/_multiarray_tests.c.src b/numpy/core/src/multiarray/_multiarray_tests.c.src index 318559885..da631c830 100644 --- a/numpy/core/src/multiarray/_multiarray_tests.c.src +++ b/numpy/core/src/multiarray/_multiarray_tests.c.src @@ -2045,6 +2045,21 @@ run_casting_converter(PyObject* NPY_UNUSED(self), PyObject *args) return PyInt_FromLong(casting); } +static PyObject * +run_intp_converter(PyObject* NPY_UNUSED(self), PyObject *args) +{ + PyArray_Dims dims = {NULL, -1}; + if (!PyArg_ParseTuple(args, "O&", PyArray_IntpConverter, &dims)) { + return NULL; + } + if (dims.len == -1) { + Py_RETURN_NONE; + } + + PyObject *tup = PyArray_IntTupleFromIntp(dims.len, dims.ptr); + PyDimMem_FREE(dims.ptr); + return tup; +} static PyMethodDef Multiarray_TestsMethods[] = { {"IsPythonScalar", @@ -2218,6 +2233,9 @@ static PyMethodDef Multiarray_TestsMethods[] = { {"run_casting_converter", run_casting_converter, METH_VARARGS, NULL}, + {"run_intp_converter", + run_intp_converter, + METH_VARARGS, NULL}, {NULL, NULL, 0, NULL} /* Sentinel */ }; diff --git a/numpy/core/tests/test_conversion_utils.py b/numpy/core/tests/test_conversion_utils.py index 3c3f9e6e1..24371b638 100644 --- a/numpy/core/tests/test_conversion_utils.py +++ b/numpy/core/tests/test_conversion_utils.py @@ -154,3 +154,33 @@ class TestCastingConverter(StringConverterTestCase): self._check("safe", "NPY_SAFE_CASTING") self._check("same_kind", "NPY_SAME_KIND_CASTING") self._check("unsafe", "NPY_UNSAFE_CASTING") + + +class TestIntpConverter: + """ Tests of PyArray_IntpConverter """ + conv = mt.run_intp_converter + + def test_basic(self): + assert self.conv(1) == (1,) + assert self.conv((1, 2)) == (1, 2) + assert self.conv([1, 2]) == (1, 2) + assert self.conv(()) == () + + def test_none(self): + # gh-15886: could deprecate this + assert self.conv(None) == () + + def test_float(self): + with pytest.raises(TypeError): + self.conv(1.0) + with pytest.raises(TypeError): + self.conv([1, 1.0]) + + def test_too_large(self): + with pytest.raises(ValueError): + self.conv(2**64) + + def test_too_many_dims(self): + assert self.conv([1]*32) == (1,)*32 + with pytest.raises(ValueError): + self.conv([1]*33) |
