SWIG, Python and C structures

RocketIII rocketiii.scientist at gmail.com
Fri Jul 28 09:32:02 EDT 2006


Hi,

I have structures like the following:

typedef struct _SX
{
  int   i;
  char  str[10];
} SX;

typedef struct _SXA
{
  int num_elements;
  SX  sa[10];
} SXA;

void myfunc1( SX *sx_p );
void myfunc2( SXA *sxa_p );

The swig interface file simply includes the .h-file with the '%include'
statement.

I'm having problems when sending elements of SX within the SXA as an
argument to myfunc2!

The Python codes is like following:
...
  sx1 = pymod.SX()
  sx1.i = 1
  sx1.str = "string1"
  sx2 = pymod.SX()
  sx2.i = 2
  sx2.str = "string2"

  sxl = []
  sxl.append( sx1 )
  sxl.append( sx2 )

  sxa = pymod.SXA()
  sxa.num_elements = len(sxl)
  sxa.sa = sxl                                  # <-triggers the
following error

Traceback (most recent call last):
  File "pymod_test.py", line 34, in ?
    test()
  File "pymod_test.py", line 31, in test
    test2()
  File "pymod_test.py", line 25, in test2
    sxa.sa = sxl
  File "/home/user/utv/misc/pymods/pymod.py", line 70, in <lambda>
    __setattr__ = lambda self, name, value: _swig_setattr(self, SXA,
name, value)
  File "/home/user/utv/misc/pymods/pymod.py", line 22, in _swig_setattr
    return _swig_setattr_nondynamic(self,class_type,name,value,0)
  File "/home/user/utv/misc/pymods/pymod.py", line 15, in
_swig_setattr_nondynamic
    if method: return method(self,value)
TypeError: argument number 2: a 'SX *' is expected, 'list([<pymod.SX;
proxy of C SX instance at _a0211708_p__SX>, <pymod.SX; proxy of C SX
instance at _70b21408_p__SX>])' is received

I've tried to add a %typemap (in) SX { ... } without any success.

The following assignment with call to myfunc2 works though.
sxa.sa = sx1 

Can anyone please advise

Regards
- Ingi




More information about the Python-list mailing list