471,334 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Calling python function from c

varuns
39
if i need to call a python function from c, i can use PyImport _Import()
python-c API. Following code shows calling python function "add1" from python module "def1"
Expand|Select|Wrap|Line Numbers
  1. int add(int x, int y)
  2.    {
  3.        double answer;
  4.        Py_Initialize();
  5.        PyObject *modname, *mod, *mdict, *func, *arg1, *arg2, *args, *rslt;
  6.        printf("############INSIDE ADD############\n");
  7.       modname = PyString_FromString("def1");
  8.       if (modname == NULL)
  9.       {
  10.           Py_Exit(0);
  11.           return 0;
  12.       }
  13.       mod = PyImport_Import(modname);
  14.       if (mod == NULL)
  15.       {
  16.          Py_Exit(0);
  17.          return 0;
  18.       }
  19.  
  20.       if (mod)
  21.       {
  22.           mdict = PyModule_GetDict(mod);
  23.           func = PyDict_GetItemString(mdict, "add1");
  24.           if (func)
  25.           {
  26.               if (PyCallable_Check(func))
  27.               {
  28.                   arg1 = PyLong_FromLong((double)x);
  29.                   arg2 = PyLong_FromLong((double)y);
  30.       printf("############ARGS FORMED############\n");
  31.                   args = PyTuple_New(2);
  32.                   PyTuple_SetItem(args, 0, arg1);
  33.                   PyTuple_SetItem(args, 1, arg2);
  34.       printf("############CALLING############\n");
  35.                   rslt = PyObject_CallObject(func, args);
  36.                   if (rslt)
  37.                   {
  38.                       answer = PyLong_AsLong(rslt);
  39.                       Py_XDECREF(rslt);
  40.                   }
  41.                   Py_XDECREF(arg1);
  42.                   Py_XDECREF(arg2);
  43.                   Py_XDECREF(args);
  44.               }
  45.           }
  46.           Py_XDECREF(mod);
  47.       }
  48.       Py_XDECREF(modname);
  49.   //    Py_Finalize();
  50.   //    printf("############%d",answer);
  51.       return (int)answer;
  52.   }

the problem is i should know the module name and function name in order to call.
Is it possible to get these two things at run time.
Basically i want to implement pointer to function, so i dont know the file name and function name at compile and link time.

And also Py_Finalize returns exception thats why i commented it
Jun 11 '07 #1
0 1281

Post your reply

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

Similar topics

reply views Thread by Natsu Mizutani | last post: by
2 posts views Thread by Satish Chimakurthi | last post: by
11 posts views Thread by Kamilche | last post: by
3 posts views Thread by Travis Berg | last post: by
1 post views Thread by Stephen Kellett | last post: by
4 posts views Thread by Edwin Gomez | last post: by
10 posts views Thread by Finger.Octopus | last post: by
15 posts views Thread by J. Peng | last post: by
16 posts views Thread by Jaco Naude | 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.