[PyKDE] [RFC] [PATCH] Adding support for templated container types to sip

Bryan O'Sullivan bos at serpentine.com
Wed Mar 2 19:39:22 GMT 2005

Hi -

I've been working on a patch to sip 4.2 that adds support for templated
container types.

This lets sip automatically generate code to handle mapping between a
templated C++ container type and a Python collection of the appropriate

Some background: right now, if you want to map between, for example
std::vector<double> and a Python list, you have to explicitly write the
conversion code between the two.  Worse, you cannot reuse this code to
convert between a std::list<double> and a Python list.  So every kind of
templated container you use requires a pile of hand-written mapping
code, coupling drudgery with plenty of potential for error.

I've added to sip the ability to automatically map between C++
containers and Python collections.  You must still declare the C++
containers you want mapped, but now it's simply one line of code for
each, instead of a potentially large amount of source.

I've modified the grammar accepted by sip as follows.  In addition to
the current form of %MappedType ...

        %MappedType std::vector<blah> {
          // %TypeHeaderCode, %ConvertFromTypeCode and %ConvertToTypeCode

sip now also accepts this:

        %MappedType std::vector<blah>;

It treats the lack of a body here as a cue to autogenerate the code that
will convert between a std::vector of blah and a Python collection.

If you want explicit control over the mapping code, you can still
specify a body, and sip will behave as it currently does.

For non-simple value types (the things inside the containers), you must
tell sip how to handle the value type, or it will produce an error
message.  No surprise there.

If you try to get sip to autogenerate code for an unsupported container
type (e.g. a std::pair), it will produce an error message.

I've attached a prerelease of the patch against sip 4.2, to get
feedback.  It does not affect any of the existing functionality of sip,
but simply adds the autogeneration feature.  I've attempted to follow
sip's coding style, so the patch will integrate cleanly.

The patch has the following caveats:

      * No support yet for Qt containers such as QValueList<>, which I
        don't use.  I don't even know if KDE has its own containers, so
        needless to say, there's no support for them, if they exist.
      * No support yet for std::map or related types.
      * I suspect that the error checking in some of the generated code
        is wrong in cases of memory allocation failure, but that should
        be easy to fix.
      * I haven't attached a diff against sipgen/parser.c, since the
        diff is huge.  You must use bison to regenerate parser.c from
        parser.y, because sip's build process doesn't create an explicit
        Makefile rule telling make to do this (so make will either do
        nothing or use yacc, both of which are wrong).

Here's a diffstat summary:

 gencode.c |  392 +++++++++++++++++++++++++++++++++++++++++++++-----------------
 parser.y  |   14 ++
 sip.h     |    7 +
 3 files changed, 310 insertions(+), 103 deletions(-)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sip-autogen-1.patch
Type: text/x-patch
Size: 11841 bytes
Desc: not available
Url : http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20050302/d21a0f18/sip-autogen-1.bin

More information about the PyQt mailing list