473,324 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 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 1495

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

Similar topics

0
by: Natsu Mizutani | last post by:
Hello, I'm trying to wrap a c++ library using MPI inside with boost.python (or SWIG). I managed to find that calling `MPI::Init()` embeded in a c++ funtion would not work. So, I decided to use...
2
by: Satish Chimakurthi | last post by:
Hi all, My question is surely a basic one, but somehow, I am not able to figure it out. I have a python file "satish.py" as follows: *satish.py* def main(): y()
11
by: Kamilche | last post by:
What a debug nightmare! I just spent HOURS running my script through the debugger, sprinkling in log statements, and the like, tracking down my problem. I called a function without the ending...
3
by: Travis Berg | last post by:
I'm running into a problem when trying to perform a callback to a Python function from a C extension. Specifically, the callback is being made by a pthread that seems to cause the problem. If I...
1
by: Stephen Kellett | last post by:
Hello everyone, I'm trying to do something in C calling Python and its failing. I'd be grateful if you could take a look and hopefully you have an answer. What I'm trying to do is determine...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
10
by: Finger.Octopus | last post by:
Hello, I have been trying to call the super constructor from my derived class but its not working as expected. See the code: class HTMLMain: def __init__(self): self.text = "<HTML><BODY>";...
15
by: J. Peng | last post by:
Hello, I saw this statement in Core Python Programming book, All arguments of function calls are made by reference, meaning that any changes to these parameters within the function affect the...
16
by: Jaco Naude | last post by:
Hi there, This is my first post over here and I hope someone can give me some guidance. I'm trying to embed Python into a Visual C++ 2008 application and I'm getting linker problems. I've...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.