471,310 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Problem with embedding python in c++.

1
Hello, I'm quite new to Python and embedding python in c++. I'm trying to write a function that i can use to call a python function. It should take 3 arguments, the name of the python file, the python function name and a std:vector with arguments for the python function. This is my code:

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     string arg1 = "runme"; //name of python file
  4.     string arg2 = "sub"; // name of python function
  5.  
  6.     Py_Initialize();
  7.  
  8.     vector<string> arguments;
  9.     arguments.push_back("100");
  10.     arguments.push_back("10");
  11.  
  12.     callPython(arg1,arg2,arguments);
  13.  
  14.     Py_Finalize(); // crash here
  15.  
  16.     return 1;
  17. }
  18.  
Expand|Select|Wrap|Line Numbers
  1. void callPython(string py_file, string py_function, vector<string> args)
  2. {
  3.     int i;
  4.     PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *pValue;
  5.  
  6.     // Build the name object
  7.     pName = PyString_FromString( py_file.c_str() );
  8.  
  9.     // Load the module object
  10.     pModule = PyImport_Import(pName);
  11.  
  12.     // pDict is a borrowed reference 
  13.     pDict = PyModule_GetDict(pModule);
  14.  
  15.     // pFunc is also a borrowed reference 
  16.     pFunc = PyDict_GetItemString(pDict, py_function.c_str() );
  17.  
  18.     if (PyCallable_Check(pFunc)) 
  19.     {
  20.         pValue = PyObject_CallObject(pFunc, NULL);//måste returna något tex 1
  21.  
  22.         // Prepare the argument list for the call
  23.         if( args.size() > 0 )
  24.         {
  25.                 pArgs = PyTuple_New(args.size());
  26.                 for (i = 0; i < args.size(); i++)
  27.                 {
  28.                     pValue = PyInt_FromLong(atoi(args[i].c_str()));
  29.                     if (!pValue)
  30.                     {                        
  31.                         PyErr_Print();
  32.                     }
  33.                     PyTuple_SetItem(pArgs, i, pValue);    
  34.                 }
  35.  
  36.                 pValue = PyObject_CallObject(pFunc, pArgs);
  37.  
  38.                 if (pArgs != NULL)
  39.                 {
  40.                     Py_DECREF(pArgs);
  41.                 }
  42.         } else
  43.         {
  44.                 pValue = PyObject_CallObject(pFunc, NULL);
  45.         }
  46.  
  47.         if (pValue != NULL) 
  48.         {
  49.             Py_DECREF(pValue);
  50.         }
  51.         else 
  52.         {
  53.             PyErr_Print();
  54.         }
  55.     } else 
  56.     {
  57.         PyErr_Print();
  58.     }
  59.  
  60.     // Clean up
  61.     Py_DECREF(pModule);
  62.     Py_DECREF(pName);
  63. }
  64.  
And the python file (runme.py):
Expand|Select|Wrap|Line Numbers
  1. def sub(a,b):
  2.     print a-b
  3.     return 0
  4.  
Now to the problem. It does print out the proper result (90 in this case) so it calls the python function. But I get an error and it crashes when at Py_Finalize() in main.

I get the following error:
Expand|Select|Wrap|Line Numbers
  1. Exception exceptions.TypeError: 'sub() takes exactly 2 arguments (0 given)' in 'garbage collection' ignored
  2. Fatal Python error: unexpected exception during garbage collection
  3.  
What have i done wrong? I did get it to work with adding PyErr_Clear(); before Py_Finalize() but that doesnt seem like the right thing to do?

I'm using Python 2.5.1, Windows XP pro and VS2005.

Thanks
Dec 19 '07 #1
1 2251
Have you tried called PyErr_Print() before Py_Finalize() to ensure that there are no other errors?
Mar 10 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Robey Holderith | last post: by
4 posts views Thread by Alicia Haumann | last post: by
reply views Thread by adsheehan | last post: by
1 post views Thread by amit | last post: by
1 post views Thread by Tommy Nordgren | last post: by
6 posts views Thread by John Dean | last post: by
reply views Thread by DevEng | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.