Py_BuildValue, PyArg_ParseTuple

Randy Heiland heiland at ncsa.uiuc.edu
Wed Jul 7 09:57:08 EDT 1999


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]);
}






More information about the Python-list mailing list