Embedding Python in C

Y

youngdubliner

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 !
 
R

Rick L. Ratzel

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top