Re: C API and memory allocation

2008-12-21 Thread Hrvoje Niksic
Aaron Brady writes: > I hold this is strong enough to put the burden of proof on the > defenders of having 's'. What is its use case? Passing the string to a C API that can't handle (or don't care about) embedded null chars anyway. Filename API's are a typical example. -- http://mail.python.or

Re: C API and memory allocation

2008-12-19 Thread Stefan Behnel
Aaron Brady wrote: >>> Otherwise you can't know its length or change its reference count. >> The internal representation of Python byte strings is 0 terminated, so >> strlen() will work. > > As MRAB said, Python strings can contain null bytes, Sure, they can. Most byte strings I've seen didn't,

Re: C API and memory allocation

2008-12-18 Thread Aaron Brady
On Dec 18, 7:54 am, Stefan Behnel wrote: > Aaron Brady wrote: > > I see.  Do I read correctly that 's' is only useful when the > > argument's position is known? > > I assume you meant "length". No, position in the argument list. Otherwise you can't change its reference count; in which case, a po

Re: C API and memory allocation

2008-12-18 Thread Floris Bruynooghe
On Dec 18, 6:43 am, Stefan Behnel wrote: > Floris Bruynooghe wrote: > > I'm slightly confused about some memory allocations in the C API. > > If you want to reduce the number of things you have to get your head > around, learn Cython instead of the raw C-API. It's basically Python, does > all the

Re: C API and memory allocation

2008-12-18 Thread MRAB
Stefan Behnel wrote: Aaron Brady wrote: I see. Do I read correctly that 's' is only useful when the argument's position is known? I assume you meant "length". Otherwise you can't know its length or change its reference count. The internal representation of Python byte strings is 0 termi

Re: C API and memory allocation

2008-12-18 Thread Stefan Behnel
Aaron Brady wrote: > I see. Do I read correctly that 's' is only useful when the > argument's position is known? I assume you meant "length". > Otherwise you can't know its length or > change its reference count. The internal representation of Python byte strings is 0 terminated, so strlen()

Re: C API and memory allocation

2008-12-18 Thread Aaron Brady
On Dec 18, 5:09 am, Ivan Illarionov wrote: > On 18 ÄÅË, 03:51, Aaron Brady wrote: > (snip) > > > How did you get a reference to the original > > string object, with which to increment its reference count? > > Use the "O!" format  instead of "s": > PyObject *pystr; > ... PyArg_ParseTuple(args, "O!

Re: C API and memory allocation

2008-12-18 Thread Ivan Illarionov
On 18 дек, 14:09, Ivan Illarionov wrote: > ... PyArg_ParseTuple(args, "O!", &PyStringObject, &pystr) ... Sorry, I must have said &PyString_Type, not &PyStringObject -- http://mail.python.org/mailman/listinfo/python-list

Re: C API and memory allocation

2008-12-18 Thread Ivan Illarionov
On 18 дек, 03:51, Aaron Brady wrote: (snip) > How did you get a reference to the original > string object, with which to increment its reference count? Use the "O!" format instead of "s": PyObject *pystr; ... PyArg_ParseTuple(args, "O!", &PyStringObject, &pystr) ... Then you can use PyString_AS

Re: C API and memory allocation

2008-12-17 Thread Stefan Behnel
Floris Bruynooghe wrote: > I'm slightly confused about some memory allocations in the C API. If you want to reduce the number of things you have to get your head around, learn Cython instead of the raw C-API. It's basically Python, does all the reference counting for you and also reduces the amoun

Re: C API and memory allocation

2008-12-17 Thread Gabriel Genellina
En Wed, 17 Dec 2008 22:51:03 -0200, Aaron Brady escribió: On Dec 17, 6:42 pm, "Gabriel Genellina" wrote: En Wed, 17 Dec 2008 21:35:04 -0200, Floris Bruynooghe   escribió: > But how can python now know how long to keep that buffer object in > memory for? It doesn't - *you* have to ensure

Re: C API and memory allocation

2008-12-17 Thread Aaron Brady
On Dec 17, 6:42 pm, "Gabriel Genellina" wrote: > En Wed, 17 Dec 2008 21:35:04 -0200, Floris Bruynooghe   > 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

Re: C API and memory allocation

2008-12-17 Thread Gabriel Genellina
En Wed, 17 Dec 2008 21:35:04 -0200, Floris Bruynooghe escribió: On Dec 17, 11:06 pm, Floris Bruynooghe wrote: So I'm assuming PyArg_ParseTuple() must allocate new memory for the returned string.  However there is nothing in the API that provides for freeing that allocated memory again. I'

Re: C API and memory allocation

2008-12-17 Thread Floris Bruynooghe
Hello again On Dec 17, 11:06 pm, Floris Bruynooghe wrote: > So I'm assuming PyArg_ParseTuple() > must allocate new memory for the returned string.  However there is > nothing in the API that provides for freeing that allocated memory > again. I've dug a little deeper into this and found that PyA

C API and memory allocation

2008-12-17 Thread Floris Bruynooghe
Hi I'm slightly confused about some memory allocations in the C API. Take the first example in the documentation: static PyObject * spam_system(PyObject *self, PyObject *args) { const char *command; int sts; if (!PyArg_ParseTuple(args, "s", &command)) return NULL; sts = s