[PyKDE] Using SIP to call Python from C++
Jim Bublitz
jbublitz at nwinternet.com
Thu May 19 18:45:46 BST 2005
On Thursday 19 May 2005 07:26, Niac Neb wrote:
> James,
>
> I'm assuming you don't have an example of calling
> Python from C++. Your previous post indicated you'd
> done this numerous times. The code snippet pretty
> much left me hanging.
>
> The folks at Riverbank indicated that SIP didn't even
> facilitate this capability.
>
> Perhaps more tutorials/WIKI will evolve as SIP becomes
> mature. I'll look forward to it. For now, the
> Boost.Python community seems more responsive to posts.
> I guess that's due to a larger user base.
The fundamental difference is that PyQt is primarily aimed at enabling you to
write Qt based applications completely in Python, while Boost::Python is
intended to allow you to embed Python in C++ applications. It would be
slightly more difficult to use Boost::Python to write purely Python apps.
That said, embedding PyQt or other sip-based code into a C/C++ application is
exactly no different than embedding any other Python code into C/C++. You
need to load the interpreter, tell it to load some modules, tell it to
execute some code (with argument passing), and retrieve the results. That's
all covered in the Python embedding/extending docs and there are some good
examples in the O'Reilly Python book, among other places.
The one thing sip does provide (from the sip Python module) is the
wrapinstance/unwrapinstance functions, which allow you to access either the
Python-space or C-space objects sip creates (for example, create an object in
Python and return a pointer to the underlying C++ object to a C++ program).
There is no reason to access sip from the C++ side of a C++-Python
mixed-language application (at least in cases I'm familiar with).
There is a lib (libpythonize) that was part of PyKDE for a while that handles
embedding with either a C++ class or C functions - it's fairly up-to-date but
not released anywhere. I can send you a copy, but quite honestly it's pretty
trivial. I have examples of usage, but they're all KDE/PyKDE oriented (plugin
stuff - panel applets and extensions, control center modules, etc), other
than a small amount of test code.
Jim
More information about the PyQt
mailing list