Cpoying a PyList to a C string array

Sheldon shejo284 at gmail.com
Tue Dec 19 10:56:31 EST 2006


Hi,

The code below is a rookie attempt to copy a python list of strings to
a string array in C. It works to some extent but results in memory
problems when trying to free the C string array. Does anyone know how
to do this properly?

***********************************************************
/*Read an list of strings from a python object*/
static int readPythonObject(void) {

  int i;
  PyObject *msgop;
  PyObject *ppsop;
  PyObject *tileop;
  PyObject *sceneop;

  for (i = 0; i < work.sumscenes; i++) {
    msgop = PyList_GetItem(work.msgobj, i);
    work.msg_scenes[i] = PyString_AsString(msgop);
    ppsop = PyList_GetItem(work.ppsobj, i);
    work.pps_scenes[i] = PyString_AsString(ppsop);
  }
  for (i = 0; i < NumberOfTiles; i++) {
    tileop  = PyList_GetItem(work.tileobj, i);
    work.tiles[i] = PyString_AsString(tileop);
    sceneop = PyList_GetItem(work.nscenesobj, i);
    work.nscenes[i] = PyInt_AsLong(sceneop);
  }
  return 1;
} /*end readPythonObject*/

*******************************************

/S




More information about the Python-list mailing list