1

This maybe asked before, but I need more clarification. I am working on a project where I need to send a 3D array from C to the embedded Python function.

I referred this, but couldn't get it to work for a 3D array. Also, I tried including numpy/arrayobject.h, but it gave me a depreciation warning. The structure of my code follows as below:

static PyObject *pName, *pModule,*pDict,*pFunc,
                *pArgs, *pValue, *pPredReturn;

int main(){

  time_t t;
  srand((unsigned) time(&t));

  int A[1][1280][64] = {{{0}}};

for (int j = 0; j<64; j++){
  for(int i = 0; i<1280; i++) {
        A[0][i][j] = (rand() % 2);
     }
}

  Py_Initialize();

  PyObject *sys = PyImport_ImportModule("sys");
  PyObject *path = PyObject_GetAttrString(sys, "path");
  PyList_Append(path, PyUnicode_FromString("."));


  // Build name object
    pName = PyUnicode_FromString("interface");

    // Load module object
    pModule = PyImport_Import(pName);

    // pDict is a borrowed reference
    pDict = PyModule_GetDict(pModule);

    // pFunction
    pFunc = PyDict_GetItemString(pDict, "some_function");

     // What should go here.. ? How should I proceed?
     if (PyCallable_Check(pFunc))
    {
             pArgs = PyTuple_New(1);
             pValue = ...  

            PyTuple_SetItem() ...
            pFuncReturn = PyObject_CallObject(pFunc, pArgs); 
    }

  Py_Finalize();

  return 0;
}

How should I modify my code to send the 3D array? Also I will be getting back a 3D array from the Python. How should I declare the datatype for the return value from the Python function?

2
  • It depends on your function pFunc, what does it expect? Commented Feb 18, 2019 at 12:05
  • Hello Ead, it expects either a list of a numpy array. Commented Feb 19, 2019 at 9:46

0

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.