[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Returning a byte buffer from C extension

forwardshortleg

3/6/2008 12:42:00 AM

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.
1 Answer

forwardshortleg

3/6/2008 1:09:00 AM

0

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