I am going to talk about calling Python functions from C and vice versa.

I am not really great at this thing as I just learnt it.

And I learnt it because I had to learn for my work.

There seem to be two ways to marry Python with C. One is called
extending Python and
another is called embedding Python. Embedding seems to be the tougher thing
with extending being a subset of embedding Python.

Anyway this is only a primer. Nothing more.

There are also other advanced mechanisms using SWIG in which you can
marry several
scripting languages with C.

Python + C is very interesting and really easy.

First copy paste this file.

<<<<<<<<<<<<<StartCopy<<<<<<<<<<<<<<<<

#include <Python.h>

/*
 * Function to be called from Python
 */
static PyObject* py_myFunction(PyObject* self, PyObject* args)
{
        char *s = "Hello from C!";
        return Py_BuildValue("s", s);
}

/*
 * Another function to be called from Python
 */
static PyObject* py_myOtherFunction(PyObject* self, PyObject* args)
{
        double x, y;
        PyArg_ParseTuple(args, "dd", &x, &y);
        return Py_BuildValue("d", x*y);
}

/*
 * Bind Python function names to our C functions
 */
static PyMethodDef myModule_methods[] = {
        {"myFunction", py_myFunction, METH_VARARGS},
        {"myOtherFunction", py_myOtherFunction, METH_VARARGS},
        {NULL, NULL}
};

/*
 * Python calls this to let us initialize our module
 */
void initpyfoo()
{
        (void) Py_InitModule("pyfoo", myModule_methods);
}

<<<<<<<<<<<<<EndCopy<<<<<<<<<<<<<<<<

Name it as pyfoo.c .

This is important. You cannot have arbitrary names. If you give some
other name then you
also need initba() (if you name your file ba.c) instead of initpyfoo()
as above inside the file.

Anyway now compile it using this command  on linux.

$ gcc -shared -I/usr/include/python2.5 -lpython2.5 -opyfoo.so pyfoo.c

It should go through. If not please find out why it doesn't work.

Then create a python file called whatever(this name doesn't matter).

<<<<<<<<<<<<<<<<<

from pyfoo import *

print "Result from myFunction():", myFunction()
print "Result from myOtherFunction():", myOtherFunction(4.0, 5.0)

<<<<<<<<<<<<<<<<<

Now call it i.py and then

$ python i.py

will print this.

Result from myFunction(): Hello from C!
Result from myOtherFunction(): 20.0


So what did we do?

We just called C functions from inside python!

And the two most important Python methods are

PyArg_ParseTuple() which converts Python objects into C datatypes.

And

Py_BuildValue() which converts the C datatypes into Python objects.

This allows us to manipulate C variables and Python objects with
either C or Python.

And remember to define the functions as static and every function
invariably returns
PyObject *  .

Please try it.

Thanks.

Have a nice day.

-Girish
_______________________________________________
To unsubscribe, email ilugc-requ...@ae.iitm.ac.in with 
"unsubscribe <password> <address>"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc

Reply via email to