AW: SIP translate Union

Phil Thompson phil at riverbankcomputing.com
Fri Nov 27 10:52:23 GMT 2020


On 26/11/2020 08:08, Marian Thomsen wrote:
> Thank you for the advice!
> 
> Unfortunately I cant get a simple example to work.
> I have a simple union in the header test_union.h
> 
> 
> union {
>     int test1;
>     int test2;
> } u;
> 
> And this is the best I got so far in the .sip file:
> 
> 
> struct union{
> 
> %TypeHeaderCode
> #include <test_union.h>
> %End
> 
>     int *test1 {
>     %GetCode
>         sipPy = PyLong_FromLongLong(sipCpp->test1)
>         if(sipPy == NULL)
>             sipPy = NULL;
>     %End
>     %SetCode
>         if (PyLong_Check(sipPy))
>             sipCpp->test1;
>         else
>             sipErr = 1;
>     %End
>     };
>     int *test2 {
>     %GetCode
>         sipPy = PyLong_FromLongLong(sipCpp->test2);
>         if(sipPy == NULL)
>             sipPy = NULL;
>     %End
>     %SetCode
>         if (PyLong_Check(sipPy))
>             sipCpp->test2;
>         else
>             sipErr = 1;
>     %End
>     };
> }u;
> 
> I get a syntax error in the last line and have tried to write it so
> that sip would understand it, but also with something like:
> 
> 
> typedef struct union union;
> union u{ ... };
> 
> I got no luck so far. Am I completly wrong?
> 
> Some advice would be appreciated

First of all, the contents of your test_union.h file is the definition 
of a variable, not a type. It should be...

union u {
     int test1;
     int test2;
};

At the moment you can't wrap a union directly. You have to wrap it in a 
struct and pretend to Python that the struct is the union. Also there is 
a bug in the C code generator which generates invalid code (but the 
equivalent C++ code is Ok). So try something like the following...

struct u_wrapper /PyName=u/
{
%TypeHeaderCode
#include<test_union.h>

struct u_wrapper
{
     union u wrapped;
};
%End

     int test1 {
     %GetCode
         sipPy = PyLong_FromLongLong(sipCpp->wrapped.test1);
     %End
     %SetCode
         if (PyLong_Check(sipPy))
             sipCpp->wrapped.test1;
         else
             sipErr = 1;
     %End
     };
};

Phil


More information about the PyQt mailing list