[Tutor] Re: [Python-Help] embedding python in C --- big porblem

Skip Montanaro skip@mojam.com (Skip Montanaro)
Wed, 15 Sep 1999 00:00:43 -0500 (CDT)


Edward,

I seriously doubt you are going to find anyone willing to solve our complete 
examples, but I will give you a few pointers.

To extend Python with a module written in C, first you start by reading the
extending and embedding manual at

    http://www.python.org/doc/current/ext/ext.html

and familiarize yourself with the stub module that is delivered as
xxmodule.c in the Modules directory of the source distribution.  In your
example of a module named mymath that defines a single function called
mymath that takes two integers and adds them, you can ignore all but the
Xxo_demo function, the PyMethodDef array and the initxx function.  You will
need to flesh out the Xxo_demo function to perform the required task
(convert the Python args into C variables, add the two numbers, then convert
the C variable back into a Python object that is returned).  Modify the
Xxo_method array to point to your mymath_mymath routine and suitably
initialize the initxx routine.  You might also look at the soundex module in
the Modules directory as an example of a very simple module that only
exposes a couple functions and doesn't define any new objects.

As for embedding Python into C, again, read the extending and embedding
manual.  In the simplest case, you may have little to do but insert a few
Python calls in your application and give your users some way to enter
Python code that is then interpreted.  Embedding Python into a C app can get 
very hairy as well, and for most useful tasks probably overlaps heavily with 
extending examples.

Skip Montanaro | http://www.mojam.com/
skip@mojam.com | http://www.musi-cal.com/~skip/
847-971-7098   | Python: Programming the way Guido indented...