473,390 Members | 917 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,390 software developers and data experts.

Calling python function from c

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.       }
  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 1507

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

Similar topics

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...
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()
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...
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...
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...
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...
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>";...
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...
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...
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.