logo       

Re: naive question about passing in a NULL via Python: msg#00187

programming.swig

Subject: Re: naive question about passing in a NULL via Python

Scott Koranda wrote:
> Suppose I have this simple C function declaration:
>
> void myfunc(char *mystring);
...
> What is the most "elegant", "easy" and "proper SWIG" way to have this
> function wrapped so that in Python I can simply do
>
> import mymodule
> mymodule.myfunc(<something>)
>
> and have the C code called with mystring == NULL?
>
> I think it would be "natural" to call in Python
>
> mymodule.myfunc(None)

The default string typemap isn't doing everything that
you want. Try the following in your .i file:

%module module

%typemap(in) char *
{
if ($input == Py_None)
{
$1 = NULL;
}
else if (PyString_Check($input))
{
$1 = PyString_AsString($input);
}
else
{
SWIG_fail;
}
}

void myfunc(char *mystring);

Then module.myfunc(None) and module.myfunc("string") will
act as you expect.

-Jerry

_______________________________________________
Swig maillist - Swig@xxxxxxxxxxxxxxx
http://mailman.cs.uchicago.edu/mailman/listinfo/swig



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise