Skip to content Skip to sidebar Skip to footer

Swig Cannot Convert Typedef Type Correct

I'm using SWIT to convert a vc project to python. I found when a struct has a member which type is like 'typedef char TEXT[16]' cannot be converted correctly. for example: typedef

Solution 1:

Make sure your typedef statements are processed by SWIG. %header only adds code to the generated file, that data is not processed by SWIG. %inline both adds the code directly to the generated file and processes it with SWIG. Here's my .i file:

%module x

%inline %{
    typedefchar TEXT[16];
    typedefint NUMBER;
    namespace MyDataAPI
    {
        structMYSTRUCT
        {
            TEXT TradingDay;
        };
        structMYSTRUCT2
        {
            NUMBER Money;
        };
    }
%}

And use:

T:\>py
Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 292012, 10:57:17) [MSC v.160064 bit (AMD64)] on win32
Type"help", "copyright", "credits"or"license"for more information.
>>> import x
>>> a=x.MYSTRUCT()
>>> a.TradingDay
''>>> a.TradingDay='ABCDEFGHIJKLMNOPQ'# Note this is too long, 17 chars...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'MYSTRUCT_TradingDay_set', argument 2 of type'char [16]'>>> a.TradingDay='ABCDEFGHIJKLMNOP'>>> a.TradingDay
'ABCDEFGHIJKLMNOP'>>> b=x.MYSTRUCT2()
>>> b.Money
0>>> b.Money=100>>> b.Money
100

Post a Comment for "Swig Cannot Convert Typedef Type Correct"