Swig/Python: data type problem

Carole Valentin carole at autosim.no
Wed Apr 3 08:10:26 EST 2002


"Lyle Johnson" <ljohnson at resgen.com> wrote in message news:<uajfhvcudhk5cb at corp.supernews.com>...
> > But in C++, we expect a data type:
> >
> > void MyObject::read(String* namefile)
> >
> > I tryed with 3 data types: char / string / string*
> > Each time, I have the same error:
> >
> > File "c:\python21\Module1.py", line 172, in read
> >     val = apply(Interfc.RoadNetwork_lecture,args)
> > TypeError: Type error. Expected _p_String
> >
> > Do you have any suggestion?
> 
> Well, the short answer is that you need to specify a typemap for String
> pointers in your SWIG interface file. Since I'm not familiar with your
> library's String class, let's just assume that the String class has a
> constructor that takes a const char * for the string's contents. In that
> case you could write an "in" typemap:
> 
> %typemap(in) String * {
>     $1 = new String(PyString_AsString($input));
> }
> 
> For more details, please read the SWIG user manual (especially the stuff
> about typemaps).
> 
> Hope this helps,
> 
> Lyle

Hello,

Thank you for your answer.
I have read the swig documentation, but I have still some more questions:

- where I have to put the code:
%typemap(in) String * {
    $1 = new String(PyString_AsString($input));
}
in the .i file?
- what is $1 and $input ? They are not recognized...
- is it the only piece of code I must write to solve my data type problem?
for instance, do I have to write something in the read function?

Thanks again,
Carole.



More information about the Python-list mailing list