[Tutor] Embedding Python

Dave Kuhlman dkuhlman at rexx.com
Sun Feb 25 19:46:37 CET 2007


On Sat, Feb 24, 2007 at 02:50:41AM -0800, Dj Gilcrease wrote:
> I am attempting to embed Python in a C++ app and have a question
> before I get too far into it.
> 
> Is is possible to to dynamically create an extension module?
> eg, I want a module name spam and I have a stuct that contains all my
> method names, and a pointer to the proper c++ function to call

If you have not already, you will want to look at SWIG
(http://www.swig.org/).  SWIG will generate C or C++ code from a
header file containing structs and classes and function
declarations.  That generated code can then be compiled and linked
to create a shared library (.so on Linux/UNIX or .dll on Windows),
which can then be loaded with the Python "import" statement.

It seems a bit of a stretch to me, but I suppose that could all be
done from within your application, perhaps by using os.system or
popen.popenx (x = 2,3, 4).  See 
http://docs.python.org/lib/module-popen2.html.

I'l let others on the list comment on whether this is dangerous.  I
suppose if all the code (that is compiled by SWIG) is under your
control or if you trust your users, it might not be too dangerous.

Dave


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list