Expand|Select|Wrap|Line Numbers
- int main()
- {
- string arg1 = "runme"; //name of python file
- string arg2 = "sub"; // name of python function
- Py_Initialize();
- vector<string> arguments;
- arguments.push_back("100");
- arguments.push_back("10");
- callPython(arg1,arg2,arguments);
- Py_Finalize(); // crash here
- return 1;
- }
Expand|Select|Wrap|Line Numbers
- void callPython(string py_file, string py_function, vector<string> args)
- {
- int i;
- PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
- // Build the name object
- pName = PyString_FromString( py_file.c_str() );
- // Load the module object
- pModule = PyImport_Import(pName);
- // pDict is a borrowed reference
- pDict = PyModule_GetDict(pModule);
- // pFunc is also a borrowed reference
- pFunc = PyDict_GetItemString(pDict, py_function.c_str() );
- if (PyCallable_Check(pFunc))
- {
- pValue = PyObject_CallObject(pFunc, NULL);//måste returna något tex 1
- // Prepare the argument list for the call
- if( args.size() > 0 )
- {
- pArgs = PyTuple_New(args.size());
- for (i = 0; i < args.size(); i++)
- {
- pValue = PyInt_FromLong(atoi(args[i].c_str()));
- if (!pValue)
- {
- PyErr_Print();
- }
- PyTuple_SetItem(pArgs, i, pValue);
- }
- pValue = PyObject_CallObject(pFunc, pArgs);
- if (pArgs != NULL)
- {
- Py_DECREF(pArgs);
- }
- } else
- {
- pValue = PyObject_CallObject(pFunc, NULL);
- }
- if (pValue != NULL)
- {
- Py_DECREF(pValue);
- }
- else
- {
- PyErr_Print();
- }
- } else
- {
- PyErr_Print();
- }
- // Clean up
- Py_DECREF(pModule);
- Py_DECREF(pName);
- }
Expand|Select|Wrap|Line Numbers
- def sub(a,b):
- print a-b
- return 0
I get the following error:
Expand|Select|Wrap|Line Numbers
- Exception exceptions.TypeError: 'sub() takes exactly 2 arguments (0 given)' in 'garbage collection' ignored
- Fatal Python error: unexpected exception during garbage collection
I'm using Python 2.5.1, Windows XP pro and VS2005.
Thanks