Returning a byte buffer from C extension

forwardshortleg at gmail.com forwardshortleg at gmail.com
Wed Mar 5 20:08:38 EST 2008


I just realized that I could do this as follows:

static PyObject* GetByteBuffer(PyObject* self, PyObject* args)
{
    char byteBuffer[100];

    // do something to fill byteBuffer with values.

   return Py_BuildValue("s#", byteBuffer, numberOfBytesToReturn);

}

Sorry for the unnecessary distraction.

Eknath


On Mar 5, 4:42 pm, forwardshort... at gmail.com wrote:
> Hello,
>
> I am new to Python programming. So, kindly excuse me if I don't use
> correct terminology  here below.
>
> I am trying to write an extension function that returns an array of
> bytes as shown in the example below:
>
> static PyObject* GetByteBuffer(PyObject* self, PyObject* args)
> {
>     char byteBuffer[100];
>
>     // do something to fill byteBuffer with values.
>
>    return Py_BuildValue("y", byteBuffer);
>
> }
>
> Is this valid? I get a run time error in Python 2.5 (actually 2.6) and
> Python 3.0 returns null terminated byte string.
> My byte buffer may contain one or more zeros and I want is the entire
> buffer regardless of its contents. How do I do it?
>
> Thanks,
>
> Eknath
>
> P.S: I know that 2.6 & 3.0 are not meant for a newcomers like me.
> Unfortunately 2.5.2 and older for windows are built using MSCVC 6.0
> and they pose problems building extensions.




More information about the Python-list mailing list