C API and memory allocation

Aaron Brady castironpi at gmail.com
Wed Dec 17 19:51:03 EST 2008


On Dec 17, 6:42 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Wed, 17 Dec 2008 21:35:04 -0200, Floris Bruynooghe  
> <floris.bruynoo... at gmail.com> escribió:
> Yes; but you don't have to dig into the implementation; from  http://docs.python.org/c-api/arg.html:
>
> s (string or Unicode object) [const char *]
> Convert a Python string or Unicode object to a C pointer to a character  
> string. You must not provide storage for the string itself; a pointer to  
> an existing string is stored into the character pointer variable whose  
> address you pass.
>
> > But how can python now know how long to keep that buffer object in
> > memory for?
>
> It doesn't - *you* have to ensure that the original string object isn't  
> destroyed (by example, incrementing its reference count as long as you  
> keep the pointer), or copy the string contents into your own buffer.

I missed something.  How did you get a reference to the original
string object, with which to increment its reference count?  How do
you know its length to copy it into your own buffer?




More information about the Python-list mailing list