Py_ParseTuple Problem

Discussion in 'Python' started by youngdubliner@hotmail.com, Apr 21, 2004.

  1. Guest

    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 !
     
    , Apr 21, 2004
    #1
    1. Advertising

  2. I'm assuming you mean PyArg_Parse and PyArg_ParseTuple? 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/elmer_pycon04.html):

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

    PyRun_SimpleString( "result = pyFuncWhichReadsDevice()" )

    evalModule = PyImport_AddModule( (char*)"__main__" );
    evalDict = PyModule_GetDict( evalModule );
    evalVal = PyDict_GetItemString( evalDict, "result" );

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

    } else {
    if( !PyTuple_Check( evalVal ) ) {
    printf( "Error: pyFuncWhichReadsDevice() 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: pyFuncWhichReadsDevice() returned tuple with
    non-int value" );
    exit( 1 );
    }
    my_c_array = (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.

    wrote:
    > 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, Apr 21, 2004
    #2
    1. Advertising

  3. Terry Reedy Guest

    <> wrote in message
    news:...
    > 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 !


    As written, above is a list instead of a tuple.

    > 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

    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.

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


    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
     
    Terry Reedy, Apr 21, 2004
    #3
  4. Guest

    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.
     
    , Apr 22, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    4,000
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,179
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,980
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    404
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    558
    Bullschmidt
    May 11, 2004
Loading...

Share This Page