Returning a byte buffer from C extension

  • Thread starter forwardshortleg
  • Start date
F

forwardshortleg

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.
 
F

forwardshortleg

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top