C API and memory allocation

Ivan Illarionov ivan.illarionov at gmail.com
Thu Dec 18 12:09:26 CET 2008

On 18 дек, 03:51, Aaron Brady <castiro... at gmail.com> wrote:
> 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_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.


More information about the Python-list mailing list