My first Python extension

Fredrik Lundh effbot at telia.com
Fri Mar 17 10:36:31 EST 2000


Kragen Sitaker <kragen at dnaco.net> wrote:
>         /* I can't find the answers to these questions in the docs in
>          * 'ext' or 'api' from Python 1.5.2:
>          * - does Py_BuildValue copy outstring, or does it create a string
>          *   object containing a reference to it?
>          * - if the latter, how should I allocate outstring?
>          * I'm assuming the answers are "it copies" and "it doesn't matter
as
>          * long as you free it."
>          */

in this case, it's more efficient to create the Python
string first, and then fill it with data:

...

    PyObject* str;
    char* outstring;

    str = PyString_FromStringAndSize(NULL, outlen);
    if (!str)
        return NULL;

    outstring = PyString_AS_STRING(str);

    /* outstring now points to the character buffer */

    ...

    return str;

...

</F>





More information about the Python-list mailing list