ctypes & strings

Aaron Brady castironpi at gmail.com
Sat Dec 27 19:06:23 EST 2008


On Dec 27, 2:03 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Sat, 27 Dec 2008 10:54:32 -0200, Hendrik van Rooyen  
> <m... at microcorp.co.za> escribió:
>
> > The c routine will actually break Python's normal string
> > immmutability and give you back a changed ins.
>
> ...so don't do that!
> If you require a mutable string to pass to C functions, use  
> ctypes.create_string_buffer()
>
> > It is in general not a good idea to change the passed string
> > like I am doing - but you wanted to know how
> > to pass a python string, and the outs example should get
> > you going - a string is an array of characters in c...
>
> Hmmm, I don't think posting a potentially harmful example is actually a  
> good idea...
>
> --
> Gabriel Genellina

One alternative would be to return a pair of Python strings.  You
might want to wrap the C function you have already in another one,
which allocates buffers and creates Python objects.

Hendrik is right, though.  The ctypes module allows us to break string
immutability within Python code.



More information about the Python-list mailing list