SWIG, Python and C structures

Discussion in 'Python' started by RocketIII, Jul 28, 2006.

  1. RocketIII

    RocketIII Guest

    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
    RocketIII, Jul 28, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,755
    Eric Sosman
    Jun 11, 2004
  2. mwallace1

    swig+structures -> python

    mwallace1, Jun 25, 2003, in forum: Python
    Replies:
    1
    Views:
    894
    vincent wehren
    Jun 25, 2003
  3. Jeff
    Replies:
    0
    Views:
    712
  4. Alfonso Morra
    Replies:
    11
    Views:
    696
    Emmanuel Delahaye
    Sep 24, 2005
  5. Keith Sabine
    Replies:
    0
    Views:
    572
    Keith Sabine
    Jun 6, 2008
Loading...

Share This Page