C API and memory allocation

Aaron Brady castironpi at gmail.com
Thu Dec 18 06:52:52 EST 2008


On Dec 18, 5:09 am, Ivan Illarionov <ivan.illario... at gmail.com> wrote:
> On 18 ÄÅË, 03:51, Aaron Brady <castiro... at gmail.com> 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) ...
edit: &PyString_Type
>
> Then you can use PyString_AS_STRING explicitly, and control ref.
> counts yourself.
>
> > How do you know its length to copy it into your own buffer?
>
> Use the "s#" format, as Gabriel has said.
>
> Ivan

I see.  Do I read correctly that 's' is only useful when the
argument's position is known?  Otherwise you can't know its length or
change its reference count.



More information about the Python-list mailing list