c api, ndarray creation

Rather stupid question here, but I can't figure out by myself: Why does the following c program segfaults? And how can I avoid it? #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include <Python.h> #include <numpy/arrayobject.h> int main(int argc, char *argv[]) { int nd=1; npy_intp dims[] = {3}; npy_intp data[] = {1,2,3}; PyObject* array = PyArray_SimpleNewFromData(nd, dims, NPY_INT, (void*) data); return 0; }

On Tue, Nov 5, 2013 at 10:10 AM, Sergey Petrov <qweqwegod@yahoo.com> wrote:
Rather stupid question here, but I can't figure out by myself: Why does the following c program segfaults? And how can I avoid it?
You need to call import_array before using the C-API, see here: http://docs.scipy.org/doc/numpy/user/c-info.how-to-extend.html#required-subr...
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h> #include <numpy/arrayobject.h>
int main(int argc, char *argv[]) { int nd=1; npy_intp dims[] = {3}; npy_intp data[] = {1,2,3}; PyObject* array = PyArray_SimpleNewFromData(nd, dims, NPY_INT, (void*) data); return 0; } _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- (\__/) ( O.o) ( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes de dominación mundial.

On Tue, 05 Nov 2013 23:20:45 +0500, Jaime Fernández del Río <jaime.frio@gmail.com> wrote:
On Tue, Nov 5, 2013 at 10:10 AM, Sergey Petrov <qweqwegod@yahoo.com> wrote:
Rather stupid question here, but I can't figure out by myself: Why does the following c program segfaults? And how can I avoid it?
You need to call import_array before using the C-API, see here:
Thank you, Jaime, that solved my issue.

On Tue, Nov 5, 2013 at 6:10 PM, Sergey Petrov <qweqwegod@yahoo.com> wrote:
Rather stupid question here, but I can't figure out by myself: Why does the following c program segfaults? And how can I avoid it?
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h> #include <numpy/arrayobject.h>
int main(int argc, char *argv[]) { int nd=1; npy_intp dims[] = {3}; npy_intp data[] = {1,2,3}; PyObject* array = PyArray_SimpleNewFromData(nd, dims, NPY_INT, (void*) data); return 0; }
numpy is not a C library. It is a Python extension module. You can use its C API from other Python extension modules, not C main programs. You have not started a Python interpreter or imported the numpy module. Only then will the numpy API be available. Mechanically speaking, the proximate cause of your segfault is that `PyArray_SimpleNewFromData` is not actually a function, but a macro that looks up a function pointer from a static table defined in the numpy module. Calling the macro `import_array()` will import the numpy module set up this table with the correct function pointers. But you first need a Python interpreter running. -- Robert Kern

On Tue, 05 Nov 2013 23:22:37 +0500, Robert Kern <robert.kern@gmail.com> wrote:
numpy is not a C library. It is a Python extension module. You can use its C API from other Python extension modules, not C >main programs. You have not started a Python interpreter or imported the numpy module. Only then will the numpy API be >available.
Mechanically speaking, the proximate cause of your segfault is that `PyArray_SimpleNewFromData` is not actually a function, >but a macro that looks up a function pointer from a static table defined in the numpy module. Calling the macro >`import_array()` will import the numpy module set up this table with the correct function pointers. But you first need a >Python interpreter running.
I just tried, apparently in vain, to simplify example. Thanks for detailed answer!
participants (3)
-
Jaime Fernández del Río
-
Robert Kern
-
Sergey Petrov