Cpoying a PyList to a C string array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sheldon

    Cpoying a PyList to a C string array

    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 readPythonObjec t(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_AsStri ng(msgop);
    ppsop = PyList_GetItem( work.ppsobj, i);
    work.pps_scenes[i] = PyString_AsStri ng(ppsop);
    }
    for (i = 0; i < NumberOfTiles; i++) {
    tileop = PyList_GetItem( work.tileobj, i);
    work.tiles[i] = PyString_AsStri ng(tileop);
    sceneop = PyList_GetItem( work.nscenesobj , i);
    work.nscenes[i] = PyInt_AsLong(sc eneop);
    }
    return 1;
    } /*end readPythonObjec t*/

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

    /S

  • Klaas

    #2
    Re: Cpoying a PyList to a C string array

    Sheldon wrote:
    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?
    You have numerous problems in this code. The most important problem is
    that you are referring to global variables which appear to be c structs
    but you don't provide the definition (e.g., "work"). However, I can
    guess some of the issues:
    for (i = 0; i < work.sumscenes; i++) {
    msgop = PyList_GetItem( work.msgobj, i);
    work.msg_scenes[i] = PyString_AsStri ng(msgop);
    ppsop = PyList_GetItem( work.ppsobj, i);
    work.pps_scenes[i] = PyString_AsStri ng(ppsop);
    }
    PyString_AsStri ng returns a pointer to the internal buffer of the
    python string. If you want to be able to free() it (or indeed have it
    exist for beyond the lifetime of the associated python string), you
    need to malloc() memory and strcpy() the data. If the strings contain
    binary data, you should be using PyString_AsStri ngAndSize. see
    http://docs.python.org/api/stringObjects.html.

    I notice that you are doing no error checking or ref counting, but my
    (inexperienced python c programming) opinion is that it should work
    (neither api could potentially call python code, so I don't think
    threading is an issue).
    for (i = 0; i < NumberOfTiles; i++) {
    tileop = PyList_GetItem( work.tileobj, i);
    work.tiles[i] = PyString_AsStri ng(tileop);
    sceneop = PyList_GetItem( work.nscenesobj , i);
    work.nscenes[i] = PyInt_AsLong(sc eneop);
    }
    return 1;
    Similarly.

    -Mike

    Comment

    • Sheldon

      #3
      Re: Cpoying a PyList to a C string array


      Klaas skrev:
      Sheldon wrote:
      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?
      >
      You have numerous problems in this code. The most important problem is
      that you are referring to global variables which appear to be c structs
      but you don't provide the definition (e.g., "work"). However, I can
      guess some of the issues:
      >
      for (i = 0; i < work.sumscenes; i++) {
      msgop = PyList_GetItem( work.msgobj, i);
      work.msg_scenes[i] = PyString_AsStri ng(msgop);
      ppsop = PyList_GetItem( work.ppsobj, i);
      work.pps_scenes[i] = PyString_AsStri ng(ppsop);
      }
      >
      PyString_AsStri ng returns a pointer to the internal buffer of the
      python string. If you want to be able to free() it (or indeed have it
      exist for beyond the lifetime of the associated python string), you
      need to malloc() memory and strcpy() the data. If the strings contain
      binary data, you should be using PyString_AsStri ngAndSize. see
      http://docs.python.org/api/stringObjects.html.
      >
      I notice that you are doing no error checking or ref counting, but my
      (inexperienced python c programming) opinion is that it should work
      (neither api could potentially call python code, so I don't think
      threading is an issue).
      >
      for (i = 0; i < NumberOfTiles; i++) {
      tileop = PyList_GetItem( work.tileobj, i);
      work.tiles[i] = PyString_AsStri ng(tileop);
      sceneop = PyList_GetItem( work.nscenesobj , i);
      work.nscenes[i] = PyInt_AsLong(sc eneop);
      }
      return 1;
      >
      Similarly.
      >
      -Mike
      Thanks Mike,

      I am rewriting the code but I don't understand the part about the c
      struct variable called work. The function I posted is a part of a
      larger script and I just posted that part that was problamatic. I was
      under the impression that if I declared the structure as global with
      the variable in tow:

      struct my_struct {
      int var;
      } work;

      then this is visible everywhere in the function as long as everything
      is in one file. Did I miss something?

      /S

      Comment

      • Klaas

        #4
        Re: Cpoying a PyList to a C string array


        Sheldon wrote:
        Thanks Mike,
        >
        I am rewriting the code but I don't understand the part about the c
        struct variable called work. The function I posted is a part of a
        larger script and I just posted that part that was problamatic. I was
        under the impression that if I declared the structure as global with
        the variable in tow:
        >
        struct my_struct {
        int var;
        } work;
        >
        then this is visible everywhere in the function as long as everything
        is in one file. Did I miss something?
        It's not important how you declare the struct. It matters what is in
        the struct (in particular, the data types of the members, and what
        initialization you've done to them).

        The important part was the rest of my message.

        -Mike

        Comment

        Working...