PyArg_ParseTuple for structs or binary data

Gabriel Genellina gagsl-py2 at
Thu Apr 10 06:58:22 CEST 2008

En Wed, 09 Apr 2008 05:05:45 -0300, Alvin Delagon <adelagon at>  

> I'm currently writing a simple python SCTP module in C. So far it works
> sending and receiving strings from it. The C sctp function sctp_sendmsg()
> has been wrapped and my function looks like this:
> sendMessage(PyObject *self, PyObject *args)
> {
>   const char *msg = "";
>   if (!PyArg_ParseTuple(args, "s", &msg))
>     return NULL;
>   snprintf(buffer, 1025, msg);
>   ret = sctp_sendmsg(connSock, (void *)buffer, (size_t)strlen(buffer),  
> 0, 0,
> 0x03000000, 0, 0, 0, 0);
>   return Py_BuildValue("b", "");
> }
> I'm going to construct an SS7 packet in python using struct.pack().  
> Here's
> the question, how am I going to pass the packet I wrote in python to my
> module? Thanks in advance! :)

Same as you do now; struct.pack returns a string object. The "s#" format  
is safer, in case embedded NUL characters are allowed. Also, there is no  
need to copy the string into another buffer (assuming the sctp_sendmsg  
doesn't alter it).
This Py_BuildValue looks suspicious - what's the intended return value? In  
case you want to return "ret" (an integer, I guess), use  
Py_BuildValue("i", ret) or just PyInt_FromLong(ret)

Gabriel Genellina

More information about the Python-list mailing list