[PyKDE] Re: Basic SIP Question

Jim Bublitz jbublitz at nwinternet.com
Fri May 3 16:51:01 BST 2002


On 03-May-02 "Bjorn Pettersen" <BPettersen at narex.com> wrote:
> I'm trying to wrap a very basic class:
 
>   class Foo {
>     int var;
>   public:
>     void setVar(int v);
>     int getVar();
>   };
 
> with the following .sip file
 
>   class Foo {
>   %HeaderCode
>   #include "Test.h"
>   %End
>   public:
>     int getVar();
>     void setVar(int v);
>   };
> 
> and SIP is giving me a parse error on the void setVar() line. If
> I remove the "int v" argument SIP runs, but doesn't create any
> output files...

Dave Smith already pointed out that you need (at a minimum) the -c
switch with a path for output files in addition to your .sip input
file when running sip.

The other problem is that:

   void setVar(int v);

should be:

   void setVar(int);

To create a sip file:

1. Strip all variable names (as above)
2. Change class declarations as follows:

   class Foo : public Bar

to:

   class Foo : Bar

3. Remove any casts on default values, eg:

   void foo(long x = (long)0);

becomes:

   void foo(long = 0);

Function calls in default values should work.

4. Any methods involving templates or basic types (except char *)
with & or * or ** (eg int&, bool *, char **) require %MemberCode
(handwritten code). I believe anything with ** requires %MemberCode
(you'd probably want a Python list anyway).

5. Any virtual methods with %MemberCode require %VirtualCode

6. Remove any forward declarations, including class' namespace
declarations, for example:

class foo;                  // delete this
namespace NS {class bar;}   // delete this too

7. All namespaced objects need to be fully qualified EVERYWHERE:

namespace NS
{
enum someEnum { a, b, c };

class foo
{
public:
                foo ();
        NS:foo  someMethod (NS:someEnum);  // the 'NS' are required

};
}; // end of NS scope

A C++ namespace behaves like a Python class:

   from someModule import NS

   x = NS.foo ()
   y = NS.a

8. All private methods/variables (EXCEPT ctors and dtors) are
deleted.

9. protected static methods used to be not allowed - I'm not sure
if Phil changed this or not (I think not)

10. Duplicate typedefs within a module are not allowed.

11. Some types (eg unsigned short int) are not recognized - ushort
works in Qt related stuff, and unsigned short will probably work
too. At the moment (it's very early here) my brain isn't
registering what type names are legal standard C++ alternatives and
which require typedefs.

sip builds a single symbol table for the entire module before any
code is generated (unlike C/C++ which allows you to build one object
file at a time) and sip does not read .h files so everything needs
to be defined in a .sip file accessible from the module (you can
%Import sip files too - works like #include) but usually defined
once and only once. sip will not generate ANY code if it finds an
error ANYWHERE in the module.

There are probably other considerations I've left out, but that
should cover 98% of cases.

When you compile/link, you need to link to libsip as well as your
own C++ lib.


Jim




More information about the PyQt mailing list