8000 Merge pull request #5415 from charris/tests-for-PyArray_AsCArray · numpy/numpy@d2b6e96 · GitHub
[go: up one dir, main page]

Skip to content

Commit d2b6e96

Browse files
committed
Merge pull request #5415 from charris/tests-for-PyArray_AsCArray
TST: added test for PyArray_AsCArray #5313
2 parents 3a9b333 + 1e052f3 commit d2b6e96

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

numpy/core/src/multiarray/multiarray_tests.c.src

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,81 @@ array_indexing(PyObject *NPY_UNUSED(self), PyObject *args)
771771
return NULL;
772772
}
773773

774+
/*
775+
* Test C-api PyArray_AsCArray item getter
776+
*/
777+
static PyObject *
778+
test_as_c_array(PyObject *NPY_UNUSED(self), PyObject *args)
779+
{
780+
PyArrayObject *array_obj;
781+
npy_intp dims[3]; // max 3-dim
782+
npy_intp i=0, j=0, k=0;
783+
npy_intp num_dims = 0;
784+
PyArray_Descr *descr = NULL;
785+
double *array1 = NULL;
786+
double **array2 = NULL;
787+
double ***array3 = NULL;
788+
double temp = 9999;
789+
790+
if (!PyArg_ParseTuple(args, "O!l|ll",
791+
&PyArray_Type, &array_obj,
792+
&i, &j, &k)) {
793+
return NULL;
794+
}
795+
796+
if (NULL == array_obj) {
797+
return NULL;
798+
}
799+
800+
num_dims = PyArray_NDIM(array_obj);
801+
descr = PyArray_DESCR(array_obj);
802+
803+
switch (num_dims) {
804+
case 1:
805+
if (PyArray_AsCArray(
806+
(PyObject **) &array_obj,
807+
(void *) &array1,
808+
dims,
809+
1,
810+
descr) < 0) {
811+
PyErr_SetString(PyExc_RuntimeError, "error converting 1D array");
812+
return NULL;
813+
}
814+
temp = array1[i];
815+
PyArray_Free((PyObject *) array_obj, (void *) array1);
816+
break;
817+
case 2:
818+
if (PyArray_AsCArray(
819+
(PyObject **) &array_obj,
820+
(void **) &array2,
821+
dims,
822+
2,
823+
descr) < 0) {
824+
PyErr_SetString(PyExc_RuntimeError, "error converting 2D array");
825+
return NULL;
826+
}
827+
temp = array2[i][j];
828+
PyArray_Free((PyObject *) array_obj, (void *) array2);
829+
break;
830+
case 3:
831+
if (PyArray_AsCArray(
832+
(PyObject **) &array_obj,
833+
(void ***) &array3,
834+
dims,
835+
3,
836+
descr) < 0) {
837+
PyErr_SetString(PyExc_RuntimeError, "error converting 3D array");
838+
return NULL;
839+
}
840+
temp = array3[i][j][k];
841+
PyArray_Free((PyObject *) array_obj, (void *) array3);
842+
break;
843+
default:
844+
PyErr_SetString(PyExc_ValueError, "array.ndim not in [1, 3]");
845+
return NULL;
846+
}
847+
return Py_BuildValue("f", temp);
848+
}
774849

775850
/*
776851
* Test nditer of too large arrays using remove axis, etc.
@@ -911,6 +986,9 @@ static PyMethodDef Multiarray_TestsMethods[] = {
911986
{"array_indexing",
912987
array_indexing,
913988
METH_VARARGS, NULL},
989+
{"test_as_c_array",
990+
test_as_c_array,
991+
METH_VARARGS, NULL},
914992
{"test_nditer_too_large",
915993
test_nditer_too_large,
916994
METH_VARARGS, NULL},

numpy/core/tests/test_multiarray.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from numpy.core.multiarray_tests import (
2424
test_neighborhood_iterator, test_neighborhood_iterator_oob,
2525
test_pydatamem_seteventhook_start, test_pydatamem_seteventhook_end,
26-
test_inplace_increment, get_buffer_info
26+
test_inplace_increment, get_buffer_info, test_as_c_array
2727
)
2828
from numpy.testing import (
2929
TestCase, run_module_suite, assert_, assert_raises,
@@ -4468,6 +4468,23 @@ def test_mapiter(self):
44684468
assert_equal(b, [ 100.1, 51., 6., 3., 4., 5. ])
44694469

44704470

4471+
class TestAsCArray(TestCase):
4472+
def test_1darray(self):
4473+
array = np.arange(24, dtype=np.double)
4474+
from_c = test_as_c_array(array, 3)
4475+
assert_equal(array[3], from_c)
4476+
4477+
def test_2darray(self):
4478+
array = np.arange(24, dtype=np.double).reshape(3, 8)
4479+
from_c = test_as_c_array(array, 2, 4)
4480+
assert_equal(array[2, 4], from_c)
4481+
4482+
def test_3darray(self):
4483+
array = np.arange(24, dtype=np.double).reshape(2, 3, 4)
4484+
from_c = test_as_c_array(array, 1, 2, 3)
4485+
assert_equal(array[1, 2, 3], from_c)
4486+
4487+
44714488
class PriorityNdarray():
44724489
__array_priority__ = 1000
44734490

0 commit comments

Comments
 (0)
0