1

Similar kind of questions have been asked by other users but none of the answers work for me. Here is my code, which is inspired by https://ubuntuforums.org/showthread.php?t=1266059

#include <stdio.h>
#include <Python.h>
#include "numpy/arrayobject.h"

int main() {
    printf("Embedding Python\n");
    PyObject *pName, *pModule, *pFunc;
    PyObject *pArgs, *pValue, *vec;

    const char* script = "TestScript";
    const char* function = "Test";

    Py_Initialize();

    pName = PyUnicode_FromString(script);
    pModule = PyImport_Import(pName);

    if (pModule != NULL) {
        printf("Step-1\n");

        pFunc = PyObject_GetAttrString(pModule, function);

        fflush( stdout );
        printf("Step-2\n");

        if (pFunc && PyCallable_Check(pFunc)) {
            // define a vector
            double* v = new double[3];
            v[0] = 1.0;
            v[1] = 2.0;
            v[2] = 3.0;             

            fflush( stdout );
            printf("Step-3\n");

            npy_intp vdim[] = { 3 };

            fflush( stdout );
            printf("Step-4\n");

            vec = PyArray_SimpleNewFromData(1, vdim, PyArray_DOUBLE, (void*)v);

            fflush( stdout );
            printf("Step-5");

            pArgs = PyTuple_New(2);
            PyTuple_SetItem(pArgs, 0, vec);
            PyTuple_SetItem(pArgs, 1, vec);

            pValue = PyObject_CallObject(pFunc, pArgs);
            Py_DECREF(pArgs);
            Py_DECREF(pName);

            if (pValue != NULL) {
                printf("Result of call: %f\n", PyFloat_AsDouble(pValue));
                Py_DECREF(pValue);
            }

            delete [] v;
        } else {
            if (PyErr_Occurred()) {
                PyErr_Print();
                fprintf(stderr, "Cannot find function \"%s\"\n", function);
            }
        }
    } else {
        PyErr_Print();
        //fprintf(stderr, "Failed to load \"%s\"\n", argv[1]);
        return 1;
    }

    Py_Finalize();
    return 0;
}

I compile this code using Python3.5 on Ubuntu 16.04. Here is command to compile the code

g++ main.cc -I/usr/include/python3.5m -I/usr/include/python3.5m -Wno-unused-result -Wsign-compare -g -fstack-protector-strong -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -L/usr/lib -lpython3.5m -lpthread -ldl -lutil -lm -Xlinker -export-dynamic -Wl,-O1 -Wl,-Bsymbolic-functions -Wunused-function

In file included from /usr/include/python3.5m/numpy/ndarraytypes.h:1777:0,
                 from /usr/include/python3.5m/numpy/ndarrayobject.h:18,
                 from /usr/include/python3.5m/numpy/arrayobject.h:4,
                 from main.cc:3: /usr/include/python3.5m/numpy/npy_1_7_deprecated_api.h:15:2: warning:
#warning "Using deprecated NumPy API, disable it by " "#defining NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]  #warning "Using deprecated NumPy API, disable it by " \   ^ In file included from /usr/include/python3.5m/numpy/ndarrayobject.h:27:0,
                 from /usr/include/python3.5m/numpy/arrayobject.h:4,
                 from main.cc:3: /usr/include/python3.5m/numpy/__multiarray_api.h:1448:1: warning: ‘int
_import_array()’ defined but not used [-Wunused-function]  _import_array(void)

On executing I get following output

Embedding Python 
Step-1 
Step-2 
Step-3 
Step-4 
Segmentation fault(core dumped)

1 Answer 1

1

You need to call the import_array() function in order to use numpy's C-API, or else you will see this kind of error.

See https://docs.scipy.org/doc/numpy/reference/c-api.array.html?highlight=import_array#importing-the-api

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. The program is working now. However, on printing array on the Python side I get [1. 2. 3.] rather than [1, 2, 3,]; there is a period instead of comma. Why is that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.