خرید بک لینک

Vote count: 0

I am trying to call a Python method from C++ code and the method I am calling takes a file name as an argument retus a list.

My sample code currently converts PyObject* result of the method call to a double but I want to convert this to a list.

#include "include.h"

int main(int argc, char* argv[])
{
   double answer = 0;
   PyObject *modname, *mod, *mdict, *func, *stringarg, *args, *result;

   Py_Initialize();

   PyRun_SimpleString("import sys");
   modname = PyUnicode_FromString("module");
   mod = PyImport_Import(modname);
   if (!mod)
   {
       PyErr_Print();
       exit(1);
   }

   mdict = PyModule_GetDict(mod);
   func = PyDict_GetItemString(mdict, "func"); 
   if (!func)
   {
       printf("function does not existn");
   }

   stringarg = PyUnicode_FromString("test.txt");
   args = PyTuple_New(1);
   PyTuple_SetItem(args, 0, stringarg);

   result = PyObject_CallObject(func, args);
   if (result)
   {
       answer = PyFloat_AsDouble(result);
       Py_XDECREF(result);
   }
   Py_XDECREF(stringarg);
   Py_XDECREF(args);

   Py_XDECREF(mod);

   Py_XDECREF(modname);

   Py_Finalize();

   retu 0;
   }

How would I go about doing this? I have seen examples of wrapper classes to create list objects passed as an argument but I haven't seen examples to convert a retued value to a list.

asked 5 mins ago

- - , .

برچسب: نویسنده: استخدام کار تاريخ: جمعه 14 اسفند 1394 ساعت: 23:02

صفحه بندی