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

Skip to content

Commit c9075fa

Browse files
committed
Merge pull request #5417 from charris/tests-for-PyArray_AsCArray
TST: added test for PyArray_AsCArray #5313
2 parents 678d481 + 1e052f3 commit c9075fa

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
@@ -719,6 +719,81 @@ array_indexing(PyObject *NPY_UNUSED(self), PyObject *args)
719719
return NULL;
720720
}
721721

722+
/*
723+
* Test C-api PyArray_AsCArray item getter
724+
*/
725+
static PyObject *
726+
test_as_c_array(PyObject *NPY_UNUSED(self), PyObject *args)
727+
{
728+
PyArrayObject *array_obj;
729+
npy_intp dims[3]; // max 3-dim
730+
npy_intp i=0, j=0, k=0;
731+
npy_intp num_dims = 0;
732+
PyArray_Descr *descr = NULL;
733+
double *array1 = NULL;
734+
double **array2 = NULL;
735+
double ***array3 = NULL;
736+
double temp = 9999;
737+
738+
if (!PyArg_ParseTuple(args, "O!l|ll",
739+
&PyArray_Type, &array_obj,
740+
&i, &j, &k)) {
741+
return NULL;
742+
}
743+
744+
if (NULL == array_obj) {
745+
return NULL;
746+
}
747+
748+
num_dims = PyArray_NDIM(array_obj);
749+
descr = PyArray_DESCR(array_obj);
750+
751+
switch (num_dims) {
752+
case 1:
753+
if (PyArray_AsCArray(
754+
(PyObject **) &array_obj,
755+
(void *) &array1,
756+
dims,
757+
1,
758+
descr) < 0) {
759+
PyErr_SetString(PyExc_RuntimeError, "error converting 1D array");
760+
return NULL;
761+
}
762+
temp = array1[i];
763+
PyArray_Free((PyObject *) array_obj, (void *) array1);
764+
break;
765+
case 2:
766+
if (PyArray_AsCArray(
767+
(PyObject **) &array_obj,
768+
(void **) &array2,
769+
dims,
770+
2,
771+
descr) < 0) {
772+
PyErr_SetString(PyExc_RuntimeError, "error converting 2D array");
773+
return NULL;
774+
}
775+
temp = array2[i][j];
776+
PyArray_Free((PyObject *) array_obj, (void *) array2);
777+
break;
778+
case 3:
779+
if (PyArray_AsCArray(
780+
(PyObject **) &array_obj,
781+
(void ***) &array3,
782+
dims,
783+
3,
784+
descr) < 0) {
785+
PyErr_SetString(PyExc_RuntimeError, "error converting 3D array");
786+
return NULL;
787+
}
788+
temp = array3[i][j][k];
789+
PyArray_Free((PyObject *) array_obj, (void *) array3);
790+
break;
791+
default:
792+
PyErr_SetString(PyExc_ValueError, "array.ndim not in [1, 3]");
793+
return NULL;
794+
}
795+
return Py_BuildValue("f", temp);
796+
}
722797

723798
/*
724799
* Test nditer of too large arrays using remove axis, etc.
@@ -850,6 +925,9 @@ static PyMethodDef Multiarray_TestsMethods[] = {
850925
{"array_indexing",
851926
array_indexing,
852927
METH_VARARGS, NULL},
928+
{"test_as_c_array",
929+
test_as_c_array,
930+
METH_VARARGS, NULL},
853931
{"test_nditer_too_large",
854932
test_nditer_too_large,
855933
METH_VARARGS, NULL},

numpy/core/tests/test_multiarray.py

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

42034203

4204+
class TestAsCArray(TestCase):
4205+
def test_1darray(self):
4206+
array = np.arange(24, dtype=np.double)
4207+
from_c = test_as_c_array(array, 3)
4208+
assert_equal(array[3], from_c)
4209+
4210+
def test_2darray(self):
4211+
array = np.arange(24, dtype=np.double).reshape(3, 8)
4212+
from_c = test_as_c_array(array, 2, 4)
4213+
assert_equal(array[2, 4], from_c)
4214+
4215+
def test_3darray(self):
4216+
array = np.arange(24, dtype=np.double).reshape(2, 3, 4)
4217+
from_c = test_as_c_array(array, 1, 2, 3)
4218+
assert_equal(array[1, 2, 3], from_c)
4219+
4220+
42044221
class PriorityNdarray():
42054222
__array_priority__ = 1000
42064223

0 commit comments

Comments
 (0)
0