[C++-SIG] Beginer questions

Ian Searle ians at amc.com
Sun Nov 14 23:45:05 CET 1999


I just spent the last few days developing a C++ extension to Python.
While I am by no means an expert, I do have some relevant information
floating about in my short term memory.  The extension we developed was
much like what you describe.  A rather complex C++ class (set of
classes) is already in the form of a library, and we want to be able to
instantiate these object(s) from Python, call their methods, and get at
their data, etc...

One of the programmers in my group first attempted this using the LLNL
CXX package to develop the extension.  This resulted in lots of trouble
for us, which has yet to be resolved.  Not because the CXX package isn't
good.  The problem may be that it is too good, or more precisely, uses
C++ constructs that are too much for g++ (GCC-2.95.2).  In this
particular instance, g++ (Solaris) has a nasty bug that does not allow
code built with it to catch exceptions.  Instead, the exceptions get
thrown all the way to the default exception handler which causes a core
dump.  We have absolutely no problems when we build the extension on
Windows with VC++.  Solaris, with g++ is another story altogether
though.

So, as a means to explore alternatives, I wrote the C++ extension by
hand.  It turned out to fairly simple, and efficient (at least so far, I
am only part way through my unit tests).  To do this, I just developed a
type extension for Python, almost exactly as described in the book:
"Internet Programming with Python" (see the bstreammodule example).
Adding a new type to Python gives you the opportunity to stash a pointer
to your C++ object in the object structure. Important things to note:

1) the init<module> function must be declared extern "C" so that it can
be runtime linked with Python.  The rest of the functions don't matter,
as they are declared static, and they show up in the method tables
anyways.

2) No global objects as we are linking against a C compiled Python.

The C++ library the we wrote an extension for makes _heavy_ use of C++
STL containers.  Getting select data out of the containers and into
Python tuples, lists, and dictionaries is fairly simple.  The hard part
(assuming you are familiar with your own library) is learning the
expectations of Python extensions.  I found the book, and the standard
Python documentation to be just fine.

When we used the LLNL CXX extension, our contribution was about 500
lines of code.  The "plain" extension is also about 500 lines of code.
The main difference is in writing the plain extension I avoided use of
C++ exceptions, and other C++ features that I know are not very portable
(like new iostreams, but don't get me started...)

I hope this helps.
Cheers,
-Ian Searle

Ephi Dror wrote:

> Hi All,  I am a beginner in Python and kind of confused on the subject
> of combining C++ and Python. I understand that there are 2 ways of
> interaction. We in our project are only interesting in the case that
> you call C++ classes and functions from Python, We would like to use
> C++ classes that are already written in C++ from Python script. We
> successfully built a shared library with C api, import it and call the
> function from Python, but when it comes to calling C++, we are
> completely in the dark and would appreciate any help in that area.
> Once we understand the subject we would be glad to help other to come
> up to speed as well. Here is our questions:1. At this point we are
> trying to wrap the C++ class as C, but our problem is, how do we pass
> a pointer from Python to C++ so we can keep the pointer to the
> class.2. Is there any easer way to call the functions of C++ classes
> from python instead of wrapping them with C functions.3. We recently
> came  across CObject Python object. is it the solution to pass pointer
> to C function from Python. Is there any example available on this?4.
> About Swig. Is swig the best solution for calling C++ from
> Python. Thanks a lot  in advanced for any information which can shade
> some light on this subject. Ephi.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/19991114/e526561c/attachment.htm>


More information about the Cplusplus-sig mailing list