C API and memory allocation

Aaron Brady castironpi at gmail.com
Thu Dec 18 22:40:35 CET 2008


On Dec 18, 7:54 am, Stefan Behnel <stefan... at behnel.de> 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 pointer to the string object's
contents (a char*) is useless after control leaves the caller's scope.

> >  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, since they carry
their lengths.  Therefore strlen will always succeed, but isn't always
right.

>>> len( "abc\x00def" )
7

'strlen' says '3'.

So, with 's', you are limited to the operations preceding null bytes
in the current scope (with the GIL held).

I hold this is strong enough to put the burden of proof on the
defenders of having 's'.  What is its use case?



More information about the Python-list mailing list