pythoncswigargvargc

swig - calling char const * [] from python


I have C code with a standard main of the form

int mymain(int argc, const char *argv[]);

I managed to get through the swig 'pythonizing' process to be able to call this from python. When I import my module and try to call mymain I get

>>>mymodule.mymain(1,[])
TypeError:in method 'mymain', argument 2 of type 'char const *[]'
>>>mymodule.mymain(1,["test"])
TypeError:in method 'mymain', argument 2 of type 'char const *[]'
>>>mymodule.mymain(1,"test")
TypeError:in method 'mymain', argument 2 of type 'char const *[]'
>>>mymodule.mymain(1,)
TypeError:mymain() missing 1 required position argument: 'argv'

and similarly changing argc to 0 or anything else didn't help. Since the C is autogenerated I'd rather not modify that - can someone fill me in on how to pass a char *[] argument? I don't actually care about the values being passed as main ignores them completely.


Solution

  • It looks like the swig 'typemap' allows one to take care of it: in the .i file you can add

    %inline %{
    int main_wrap()
        {
            const char* v[]={"foo"};
            mymain(1,v);
        }
    %}
    

    Reswigging this and recompiling, I can now call main_wrap() from python.