Appendix C. First Steps with Sip

Table of Contents
Introduction
How sip works
Creating .sip files
Things sip can't do automatically
Where to look to start writing your own wrappers/bindings
Sip usage and syntax
Directives
Accepted C++ / Qt constructs
SIPLIB Functions
Jim Bublitz

Jim Bublitz knows far more about sip, the tool used to wrap C++ libraries for Python, than I do. For instance, he's the author of the bindings to the KDE2 libraries. For these reasons, I asked him to write an appendix on using sip.

Introduction

Wrapping C++ libraries for use from Python is a profitable undertaking, given the wealth of functionality that becomes available after doing so. Python, in contrast with Java, has been designed from the outset to be easily extensible with ‘foreign' libraries written in C. C++, however, is a far more complex language than C, and requires careful attention to the creation, ownership and destruction of objects. Trolltech have made the work even more complicated with the invention of a special meta-object pre-compiler needed to get techniques like signals and slots working.

These days it is not usual to wrap C++ libraries by hand. If only because of the size of the Qt libraries, it probably isn't practical to write these bindings manually, so an automated tool, sip, was developed by Phil Thompson (the PyQt developer) to generate the necessary binding code.

As you know, PyQt is a set of Python bindings for the Qt libraries. That means that PyQt isn't a translation of Qt into Python—instead, the ‘bindings' let you access and use the C++ Qt libraries from the Python language via an intermediate wrapper library, which is also written (or rather, generated) in C++.

The sip program generates these C++ ‘wrappers'. Wrappers are chunks of C++ code that allow Python to pass data to and from other C++ code and to invoke C++ methods or functions. Sip gets its name (and some of it's architecture) from another wrapper generator named swig. In fact, sip started out as a small swig, although it has grown a bit since then. It is specifically designed to generate Python bindings for C++ libraries, while swig is more general purpose, and can wrap C libraries for a variety of scripting languages. Of course, sip also offers some additional functionality, like support for signals and slots, Python inheritance of C++ objects, and many other C++ language features: