I am using Python code within C++ code and trying to pass a list argument to a function written in Python. I tried the normal way of executing the Python code without passing any argument and it was all working fine but when I pass a list as an argument, I get a segmentation fault.
Here is my code:
#define PY_SSIZE_T_CLEAN
#include</usr/include/python3.6/Python.h>
#include <bits/stdc++.h>
using namespace std;
int callModuleFunc(int array[], size_t size) {
    PyObject *mymodule = PyImport_ImportModule("test");
    PyObject *myfunc = PyObject_GetAttrString(mymodule, "get_lists");
    cout<<"Imported"<<endl;
    PyObject *mylist = PyList_New(size);
    cout<<"Imported3"<<endl;
    for (size_t i = 0; i != size; ++i) {
    
        PyList_SET_ITEM(mylist, i, PyLong_FromLong(array[i]));
    }
    PyObject *arglist = Py_BuildValue("(O)", mylist);
    cout<<"Imported1"<<endl;
    PyObject *result = PyObject_CallObject(myfunc, arglist); // getting segmentation fault here
    cout<<"Imported5"<<endl;
    int retval = (int)PyLong_AsLong(result);
    Py_DECREF(result);
    Py_DECREF(arglist);
    Py_DECREF(mylist);
    Py_DECREF(myfunc);
    Py_DECREF(mymodule);
    return retval;
    }
int main(int argc, char const *argv[])
{
    wchar_t * program = Py_DecodeLocale(argv[0], NULL);
    if(!program){
        cout<<"***Error***"<<endl;
        exit(1);
    }
    Py_SetProgramName(program);
    Py_Initialize();
    PyObject *module = NULL, *result = NULL;
    PyRun_SimpleString("print('Hello from python')\n"
                   "print('Hiii')");
    int arr[5] = {1,3,4,5,6};
    callModuleFunc(arr, 5);
    if(Py_FinalizeEx() < 0){
        cout<<"***Error***"<<endl;
        exit(120);
    
    }
    PyMem_RawFree(program);
    return 0;
}
When I call PyObject_CallObject(myfunc, arglist), I get a segmentation fault.
I am totally new to it so I'm just trying stuff from the internet.
I'm using Python version 3.6 with g++ compiler 7.5.
Here is my test.py:
def get_lists(l1):
    print("Lists: ", l1)
    return 10
Please let me know how I can resolve this.
Thanks
 
     
    