Py_ParseTuple Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • youngdubliner@hotmail.com

    Py_ParseTuple Problem

    Hi All ,

    Bit of a problem with Py_Parse or Py_ParseTuple ?

    I have a python script that reads a sector of flash in an embedded device.
    it returns the value of each byte within the sector as a tuple.

    i.e. [255,255,255,255 ,255,255,255, .......etc etc for the whole sector !

    We are using this script to read whats currently in any sector.
    No problems there.

    Now I need to call our script from C.

    I want to copy each of the bytes within the sector into an C unsigned char *array

    Is there any way to do this using Py_ParseTuple or Py_Parse ???

    something like this for instance ??? ......

    Py_ParseTuple[pres,"??",&my_c _array]

    so now my_c_array[0] = 255 ,my_c_array[1] = 255 ,my_c_array[2] = 255 , etc etc


    Thanks for the help !
  • Rick L. Ratzel

    #2
    Re: Py_ParseTuple Problem

    I'm assuming you mean PyArg_Parse and PyArg_ParseTupl e? I couldn't find
    any docs on Py_Parse or Py_ParseTuple.. .

    Anyway, maybe something like this might work for you (portions taken
    from example in http://elmer.sourceforge.net/PyCon04...pycon04.html):

    ....
    PyObject* evalModule;
    PyObject* evalDict;
    PyObject* evalVal;
    PyObject* tupleItem;
    unsigned char* my_c_array;
    int i;
    int tupleSize;

    PyRun_SimpleStr ing( "result = pyFuncWhichRead sDevice()" )

    evalModule = PyImport_AddMod ule( (char*)"__main_ _" );
    evalDict = PyModule_GetDic t( evalModule );
    evalVal = PyDict_GetItemS tring( evalDict, "result" );

    if( evalVal == NULL ) {
    PyErr_Print();
    exit( 1 );

    } else {
    if( !PyTuple_Check( evalVal ) ) {
    printf( "Error: pyFuncWhichRead sDevice() did not return a tuple" );
    exit( 1 );
    }

    my_c_array = (unsigned char*) malloc( sizeof( unsigned char ) *
    PyTuple_Size( evalVal ) );

    tupleSize = PyTuple_Size( evalVal );

    for( i=0; i < tupleSize; i++ ) {
    tupleItem = PyTuple_GetItem ( evalVal, i );

    if( !PyInt_Check( tupleItem ) ) {
    printf( "Error: pyFuncWhichRead sDevice() returned tuple with
    non-int value" );
    exit( 1 );
    }
    my_c_array[i] = (unsigned char) PyInt_AsLong( tupleItem );
    }
    }
    ....

    I have no idea if this will work for you since I haven't even tried
    to compile it...consider it pseudo-code.

    -Rick.

    youngdubliner@h otmail.com wrote:[color=blue]
    > Hi All ,
    >
    > Bit of a problem with Py_Parse or Py_ParseTuple ?
    >
    > I have a python script that reads a sector of flash in an embedded device.
    > it returns the value of each byte within the sector as a tuple.
    >
    > i.e. [255,255,255,255 ,255,255,255, .......etc etc for the whole sector !
    >
    > We are using this script to read whats currently in any sector.
    > No problems there.
    >
    > Now I need to call our script from C.
    >
    > I want to copy each of the bytes within the sector into an C unsigned char *array
    >
    > Is there any way to do this using Py_ParseTuple or Py_Parse ???
    >
    > something like this for instance ??? ......
    >
    > Py_ParseTuple[pres,"??",&my_c _array]
    >
    > so now my_c_array[0] = 255 ,my_c_array[1] = 255 ,my_c_array[2] = 255 , etc etc
    >
    >
    > Thanks for the help ![/color]

    Comment

    • Terry Reedy

      #3
      Re: Py_ParseTuple Problem


      <youngdubliner@ hotmail.com> wrote in message
      news:4039221c.0 404202256.3f21d 817@posting.goo gle.com...[color=blue]
      > Hi All ,
      >
      > Bit of a problem with Py_Parse or Py_ParseTuple ?
      >
      > I have a python script that reads a sector of flash in an embedded[/color]
      device.[color=blue]
      > it returns the value of each byte within the sector as a tuple.
      >
      > i.e. [255,255,255,255 ,255,255,255, .......etc etc for the whole sector ![/color]

      As written, above is a list instead of a tuple.
      [color=blue]
      > We are using this script to read whats currently in any sector.
      > No problems there.
      >
      > Now I need to call our script from C.
      >
      > I want to copy each of the bytes within the sector into an C unsigned[/color]
      char *array

      Without knowing the C API, I would try to generate byte string from within
      Python. Three possibilities:

      1. turn list into string - secstring = ''.join(map(chr , seclist)). Then
      use C API to get the cstring from string object.

      2. turn list/tuple into array (of unsigned chars) or built that directly
      instead of list or tuple. See array module. I presume you can get at the
      buffer itself from C API.

      3. look into ctypes module.
      [color=blue]
      > Is there any way to do this using Py_ParseTuple or Py_Parse ???[/color]

      If you mean PyArg.... stuff, I believe these are meant for parsing
      argu;ments passed as heterogeneous Python tuple, which is quite different
      from what you want to do.

      Terry J. Reedy




      Comment

      • youngdubliner@hotmail.com

        #4
        Re: Py_ParseTuple Problem

        Thanks Rick ,

        Thats excellent I tried your code , changed it a little and it works
        really well.

        I didn't even know that PyTuple_Size() existed , handy little function that.

        Thanks again !

        Keith.

        Comment

        Working...