Py_BuildValue, PyArg_ParseTuple

Michael P. Reilly arcege at shore.net
Wed Jul 7 11:57:12 EDT 1999


Randy Heiland <heiland at ncsa.uiuc.edu> wrote:
: Here's a very simple program where I want to get back out
: (PyArg_ParseTuple) what I put in (Py_BuildValue) - however, I can't seem
: to do that.  Can someone PLEASE enlighten me?

: thanks,
: --Randy

: /* Py_BuildValue, PyArg_ParseTuple  usage */

: #include "Python.h"

: main(argc, argv)
:     int argc;
:     char **argv;
: {
:   unsigned char ba[3] = {2,4,8};  /* byte array */
:   PyObject *pobj;
:   char *cstr;
:   int len;

:   Py_Initialize();

:   printf("Py_BuildValue of %d %d %d\n",ba[0],ba[1],ba[2]);
:   pobj = Py_BuildValue("s#", ba,3);
:   if (pobj == (PyObject *)NULL)
:      printf("pobj is NULL\n");

:   printf("\nPyArg_ParseTuple(pobj,...)\n" );
:   PyArg_ParseTuple(pobj,"s#", &cstr,&len);
:   printf("len=%d\n",len);
:   printf("cstr=%s\n",cstr);

:   printf("cstr[0]=%c\n",cstr[0]);
:   printf("cstr[1]=%c\n",cstr[1]);
:   printf("cstr[2]=%c\n",cstr[2]);

:   printf("(int)cstr[0]=%d\n",(int)cstr[0]);
:   printf("(int)cstr[1]=%d\n",(int)cstr[1]);
:   printf("(int)cstr[2]=%d\n",(int)cstr[2]);
: }


PyArg_ParseTuple expects to be given a Python tuple object, not a
string.  The compliment to Py_BuildValue is PyArg_Parse.  Use that
instead of PyArg_ParseTuple and it should work. :)

  -Arcege





More information about the Python-list mailing list