Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

PyArray_AsCArray causes segmentation fault with 3D-arrays #5313

Closed
@hensing

Description

@hensing

I tried to use numpy arrays with the C-API and found some strange behavior:

I'm trying to create a C-array-like view onto an numpy (2d and 3d) array using this function:

c-function: print requested item from 2D or 3D-array

static PyObject* test(PyObject* self, PyObject* args) {    PyArrayObject *symbol_series_obj;    npy_intp i=0, j=0, k=0;    if (!PyArg_ParseTuple(args, "O!ll|l",                &PyArray_Type, &symbol_series_obj,                &i, &j, &k)    ) return NULL;    if (NULL == symbol_series_obj)  return NULL;    // get number of dimensions:    npy_intp num_dims = PyArray_NDIM(symbol_series_obj);    // help-vars    int typenum = NPY_DOUBLE;    PyArray_Descr *descr = PyArray_DescrFromType(typenum);    npy_intp dims[4];    // incref, as PyArray_AsCArray steals reference    Py_INCREF(symbol_series_obj);    // is 2D-array    if (num_dims == 2){        double **symbol_series;        if (PyArray_AsCArray((PyObject **) &symbol_series_obj,                    (void **) &symbol_series, dims, 2, descr) < 0){            PyErr_SetString(PyExc_TypeError, "error converting to c array");            return NULL;}        printf("2D-array[%i][%i]: %.2f\n",                (int) i, (int) j, symbol_series[i][j]);        // free C-like array        PyArray_Free((PyObject *) symbol_series_obj, (void *) symbol_series);    }    // is 3D-array    else if (num_dims == 3){        double ***symbol_series;        if (PyArray_AsCArray((PyObject **) &symbol_series_obj,                    (void ***) &symbol_series, dims, 3, descr) < 0){            PyErr_SetString(PyExc_TypeError, "error converting to c array");            return NULL;}        printf("3D-array[%i][%i][%i]: %.2f\n",                (int) i, (int) j, (int) k, symbol_series[i][j][k]);        // free C-like array        PyArray_Free((PyObject *) symbol_series_obj, (void *) symbol_series);    }    else {        printf("error not 2D or 3D");        return NULL;    }    Py_RETURN_NONE;}

python test-case: create array and pick random items

import numpy as npfrom my_module import testdef get_random_item(arr):    "selects a random item from given array with numpy and c-ext"    # get coord:    cords = tuple([np.random.randint(dim) for dim in arr.shape])    str_cords = ''.join(("[{}]".format(cord) for cord in cords))    # numpy:    print("numpy: arr{} = {}".format(str_cords, arr[cords]))    # c-version:    test(arr, *cords)if __name__ == '__main__':    ARR2D = np.arange(0, 0.27, 0.01).reshape(3, 9)    for _ in range(10):        get_random_item(ARR2D)    ARR3D = np.arange(0, 0.27, 0.01).reshape(3, 3, 3)    for _ in range(10):        get_random_item(ARR3D)

issue: python segfaults randomly when getting random items from an 3D-array

(not always during runtime - at the latest on exit)
If I run the test-case only with 2D-arrays, everything works fine and no segfault occurs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp