8000 BUG TST: Closes #5313 PyArray_AsCArray caused segfault and adds testcase by hensing · Pull Request #5334 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG TST: Closes #5313 PyArray_AsCArray caused segfault and adds testcase #5334

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions numpy/core/src/multiarray/multiarray_tests.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,81 @@ array_indexing(PyObject *NPY_UNUSED(self), PyObject *args)
return NULL;
}

/*
* Test C-api PyArray_AsCArray item getter
*/
static PyObject *
test_as_c_array(PyObject *NPY_UNUSED(self), PyObject *args)
{
PyArrayObject *array_obj;
npy_intp dims[3]; // max 3-dim
npy_intp i=0, j=0, k=0;
npy_intp num_dims = 0;
PyArray_Descr *descr = NULL;
double *array1 = NULL;
double **array2 = NULL;
double ***array3 = NULL;
double temp = 9999;

if (!PyArg_ParseTuple(args, "O!l|ll",
&PyArray_Type, &array_obj,
&i, &j, &k)) {
return NULL;
}

if (NULL == array_obj) {
return NULL;
}

num_dims = PyArray_NDIM(array_obj);
descr = PyArray_DESCR(array_obj);

switch (num_dims) {
case 1:
if (PyArray_AsCArray(
(PyObject **) &array_obj,
(void *) &array1,
dims,
1,
descr) < 0) {
PyErr_SetString(PyExc_RuntimeError, "error converting 1D array");
return NULL;
}
temp = array1[i];
PyArray_Free((PyObject *) array_obj, (void *) array1);
break;
case 2:
if (PyArray_AsCArray(
(PyObject **) &array_obj,
(void **) &array2,
dims,
2,
descr) < 0) {
PyErr_SetString(PyExc_RuntimeError, "error converting 2D array");
return NULL;
}
temp = array2[i][j];
PyArray_Free((PyObject *) array_obj, (void *) array2);
break;
case 3:
if (PyArray_AsCArray(
(PyObject **) &array_obj,
(void ***) &array3,
dims,
3,
descr) < 0) {
PyErr_SetString(PyExc_RuntimeError, "error converting 3D array");
return NULL;
}
temp = array3[i][j][k];
PyArray_Free((PyObject *) array_obj, (void *) array3);
break;
default:
PyErr_SetString(PyExc_ValueError, "array.ndim not in [1, 3]");
return NULL;
}
return Py_BuildValue("f", temp);
}

/*
* Test nditer of too large arrays using remove axis, etc.
Expand Down Expand Up @@ -850,6 +925,9 @@ static PyMethodDef Multiarray_TestsMethods[] = {
{"array_indexing",
array_indexing,
METH_VARARGS, NULL},
{"test_as_c_array",
test_as_c_array,
METH_VARARGS, NULL},
{"test_nditer_too_large",
test_nditer_too_large,
METH_VARARGS, NULL},
Expand Down
2 changes: 1 addition & 1 deletion numpy/core/src/multiarray/multiarraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ PyArray_AsCArray(PyObject **op, void *ptr, npy_intp *dims, int nd,
goto fail;
}
for (i = 0; i < n; i++) {
ptr3[i] = ptr3[n + (m-1)*i];
ptr3[i] = (char **) &ptr3[n + m * i];
for (j = 0; j < m; j++) {
ptr3[i][j] = PyArray_BYTES(ap) + i*PyArray_STRIDES(ap)[0] + j*PyArray_STRIDES(ap)[1];
}
Expand Down
19 changes: 18 additions & 1 deletion numpy/core/tests/test_multiarray.py
58E4
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from numpy.core.multiarray_tests import (
test_neighborhood_iterator, test_neighborhood_iterator_oob,
test_pydatamem_seteventhook_start, test_pydatamem_seteventhook_end,
test_inplace_increment, get_buffer_info
test_inplace_increment, get_buffer_info, test_as_c_array
)
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_raises,
Expand Down Expand Up @@ -4201,6 +4201,23 @@ def test_mapiter(self):
assert_equal(b, [ 100.1, 51., 6., 3., 4., 5. ])


class TestAsCArray(TestCase):
def test_1darray(self):
array = np.arange(24, dtype=np.double)
from_c = test_as_c_array(array, 3)
assert_equal(array[3], from_c)

def test_2darray(self):
array = np.arange(24, dtype=np.double).reshape(3, 8)
from_c = test_as_c_array(array, 2, 4)
assert_equal(array[2, 4], from_c)

def test_3darray(self):
array = np.arange(24, dtype=np.double).reshape(2, 3, 4)
from_c = test_as_c_array(array, 1, 2, 3)
assert_equal(array[1, 2, 3], from_c)


class PriorityNdarray():
__array_priority__ = 1000

Expand Down
0