Access c++ objects

Alex Martelli aleaxit at yahoo.com
Fri Feb 9 03:14:45 EST 2001


<dustin at dunsinane.net> wrote in message
news:mailman.981681613.8981.python-list at python.org...
> I am embedding Python within a large c++ project. I am looking for
> documentation or examples on accessing c++ objects and variables from
> Python scripts. Thank you.

One option is Boost Python, http://www.boost.org/libs/python/doc/index.html.

I personally like it a lot, but then I'm quite enamoured of the whole
www.boost.org concept -- powerful, free, open-source, peer-reviewed
libraries based on the concepts of the Standard C++ Library and working
very well with it.  Boost Python has not been around all that long; I
think it's pretty well-documented, decently rich, and working neatly,
for its age, but of course it can (and likely will) improve on each
of these aspects.  BPL relies crucially on the metaclass concept for
smooth C++/Python interoperation, but you don't have to grasp that
concept to make effective use of it -- you just smoothly and almost
seamlessly inherit/use/override between C++ and Python, either way...
not QUITE as perfect as Java/Jython integration, but moving towards
that direction!

A perhaps more mature option is http://cxx.sourceforge.net/.  _Its_
use of metaclass concepts, the "CXX_Extensions facility", is said to
be "still in a quite experimental state", but it's not as central to
CXX's workings as the BPL equivalent is to Boost Python's.  CXX's base
approach doesn't do quite as much 'wrapping' as BPL's -- it helps you
a lot compared with the raw Python C API's, but less than BPL.

Depending on one's viewpoint, one may transliterate "helps you a lot"
to "does mysterious black magick behind your back", though -- i.e.,
both approaches have merit!  In CXX, the functions you write to be
called from Python have signatures that look just like the classic C
API ones -- "static PyObject* mymodule_addvalue (PyObject* self,
PyObject* pargs)", etc, etc -- and the helpers come inside the
function body, with CXX objects such as Tuple, Dict, Int, to wrap
the corresponding Python ones, and a C++ exception based approach
to detecting and letting Python known about errors.  In BPL, you
expose 'directly' to Python some functions (or methods of your C++
classes) with 'normal' C++ signatures -- BPL templates take care
of unpacking the Python arguments and 'dispatching' to the right
exposed C++ function.

If you're a modern C++-er, feeling right at home with advanced uses
of templates such as those which are so pervasive in the Standard C++
Library (and other similar frameworks designed to cooperate with it,
such as Microsoft's ATL and WTL, other Boost libraries, etc), then
BPL is probably right for you; or, at the other extreme, BPL may be
best if what you want to wrap and expose to Python is very simple and
seamlessly handled by BPL -- e.g., if you have C++ methods that take
and/or return std::string objects (from the Standard C++ Library),
Boost Python will translate those to/from Python string objects
without any real effort on your part.  If, on the other hand, you
dread complicated templates, and swear each and every time you get
a mysterious compiler error connected to some slight imperfection in
template expansions, then CXX may be more to your liking -- e.g.,
Python strings are mapped to a special-purpose 'String' class, and
you explicitly use that, and its methods, rather than std::string.

(I have never benchmarked the overhead for CXX vs BPL -- I wonder
if BPL's template approach lets it gain the same performance boost
that many template libraries, such as the Standard C++ one, can get
wrt more-classic C++ approaches, or if the amount of behind the scenes
work is too high in this case; but I don't wonder hard enough to get
down to serious benchmarking:-).


There are other approaches, such as SIP (that comes with PyQT and
was actually used to construct the latter) and Gordon McMillan's
simple, lightweight SCXX 'C++/Python API' (the latter is at
http://www.mcmillan-inc.com/scxx.html) -- SCXX is probably your
only option if your chosen C++ compiler is not MSVC++6, gcc, or
else highly ISO-compliant, since CXX and Boost (dunno 'bout SIP,
sorry) do rely on good C++ standard compliance (and only VC++6
and gcc are widespread enough to warrant special tweaks & hacks
to workaround their standard-compliance issues...), while SCXX is
deliberately VERY minimalist in the set of C++ standard features
it supports.


Alex






More information about the Python-list mailing list