[PyKDE] Using SIP to call Python from C++

Jim Bublitz jbublitz at nwinternet.com
Thu May 19 21:52:22 BST 2005


On Thursday 19 May 2005 12:49, Niac Neb wrote:
> Jim,
>
> Thanks for the detailed response!

> I believe I do need to call SIP (maybe just Python)
> from C++.  I have an existing C++ application that I'm
> trying to add Python plotting capabilities to.  I need
> to spawn these plot utilities ... telling them where
> to get the plot data ... then closing them (with
> resource cleanup) when done.  Make sense?

To spawn the plot utilities, you simply need to run a Python function, which 
the Python C API will let you do (after initializing the interpreter and 
loading the necessary modules). 

Passing the plot data would depend on how much data there is - if it's a 
really large amount, passing it via a file or pipe would seem to make sense. 
Otherwise, it can be loaded into a Python structure (list, dict) using the 
Python C API and passed as an arg to the function call.

Alternatively, you might find some way to pass the data via a C++ object with 
a sip binding (create your own C++ object that holds the data, write a Python 
binding in sip; in your app, create the object in C++, pass the instance 
pointer to Python and convert the pointer to a Python object using 
'wrapinstance' from the sip Python module - see docs).

Clean up just gets done in the Python and C++ spaces.

I don't see any advantage in calling into libsip, and in fact libsip isn't 
really designed to be linked to by C++ programs - it's a Python module.

> The Python/C API seems very tedious to code to.

It is, but if the actions are repetitive, it can be wrapped with a C++ class 
to allow you to code at a higher level of abstraction. While tedious, it 
isn't very complicated once you have the basic operations down. The hardest 
part for me is managing the reference count on PyObjects.

> Perhaps the book you recommended will be a great
> start.  I have done it.  But, getting the Python
> function, setting the arguments, and freeing the
> allocated resources was very, very difficult (IMHO).

There isn't anything in sip that I can think of that would make writing the 
C++ side of the code easier. In fact, the way I've done it is to do as little 
as possible in C++ and try to dump most of the work into Python. If there are 
speed problems, you can write the critical code in C++, write bindings for 
it, and when called from Python, it'll still be the C++ code doing the work.

> Examples ... do you recommend the WIKI as a place to
> start learning?  As you might know, it's a little
> overwhelming.  I particularly appreciated the
> Boost.Python tutorial.  Does SIP have one of this
> detail?

I haven't visited the wiki lately, so I'm not sure what's there. I'm not aware 
of any tutorial, but once again, it isn't any different than embedding Python 
in other applications, and there's some tutorials and docs available on that.

Otherwise, start from a simple application - something like invoking a Python 
program from C++ that prints "Hello, World", then modify to print an 
arbitrary string passed from C++ or adds two numbers and returns the result 
to be printed from C++, etc.  You'll encounter most of the issues involved 
pretty quickly. 

That's all pretty well covered in the Python embedding and extending docs. The 
rest is in the Python/C API docs.

Jim




More information about the PyQt mailing list