Replacing call to PyObject_CallObject with PyEval_CallFunction

G

grbgooglefan

Hello Python Experts
May you please help me on this change?
I have following code:
//==================================================
typedef struct IOParams {
char *ioString;
long lionum;
double dionum;
float fionum;
int nionum;
} *pIOParams;
// iterate thru list of variables, check if all req data is set
int nCtr, ndtyp, ret = 0;
IOParams inputVar;
PyObject *pTuple = 0;
pTuple = PyTuple_New(numofVars);
for(nCtr = 0; nCtr < numofVars; nCtr++){
ndtyp = pEvalFunc->pExprVarsArray[nCtr].nDataType;
switch(ndtyp){
case(INT_T):
printf("%s=%d ",pEvalFunc-
pExprVarsArray[nCtr].szVarName,inputVar.nionum);

PyTuple_SetItem(pTuple,nCtr,PyInt_FromLong(inputVar.nionum));
break;
case(LONG_T):
printf("%s=%ld ",pEvalFunc-
pExprVarsArray[nCtr].szVarName,inputVar.lionum);

PyTuple_SetItem(pTuple,nCtr,PyLong_FromLong(inputVar.lionum));
break;
case(FLOAT_T):
printf("%s=%f ",pEvalFunc-
pExprVarsArray[nCtr].szVarName,inputVar.fionum);

PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(inputVar.fionum));
break;
case(DOUBLE_T):
printf("%s=%f ",pEvalFunc-
pExprVarsArray[nCtr].szVarName,inputVar.dionum);

PyTuple_SetItem(pTuple,nCtr,PyFloat_FromDouble(inputVar.dionum));
break;
case(STRING_T):
printf("%s=%s ",pEvalFunc-
pExprVarsArray[nCtr].szVarName,inputVar.ioString);

PyTuple_SetItem(pTuple,nCtr,PyString_FromString(inputVar.ioString));
break;
default:
printf("\nUnknown data type [%d] for %s
\n",ndtyp,pEvalFunc->pExprVarsArray[nCtr].szVarName);
bUnknownDataType = true;
break;
}
if(bUnknownDataType){ // got an unknown data type for a variable
ret = -1;
break;
}
}
// all variables are set, call Python function
if(ret == 0){
printf("\n");
PyObject *pResult = PyObject_CallObject(pEvalFunc-
pPyEvalFunction,pTuple);
//==================================================
In this, I am using IOParams struct to set values for the pTuple. The
number & type of values to be set in pTuple change at runtime and
therefore I had to put this for loop to set the value in this pTuple.

Then I came to know about another function call
PyEval_CallFunction(PyObject *obj, char *format, ...);
This function call accepts variable number of arguments, like:
PyEval_CallFunction(obj, "iii", a, b, c);

Limitation with this is that "a", "b" and "c" are individual variables
& are all known at compilation time.
But, in may case, the variables with the values are in a vector of
IOParams structs.

How do I pass the elements populated in struct variables of this
vector dynamically to PyEval_CallFunction, in the fashion somewhat
like below?
PyEval_CallFunction(obj, "iii", vector[0].ioparam-
nionum,vector[1].ioparam->nionum,vector[2].ioparam->nionum);
PyEval_CallFunction(obj, "di", vector[0].ioparam-
fionum,vector[1].ioparam->nionum);
PyEval_CallFunction(obj, "diiisis", vector[0].ioparam-
fionum,vector[1].ioparam->nionum,vector[2].ioparam-
nionum,vector[3].ioparam->nionum,vector[4].ioparam-
ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);
and so on.....

These function calls are just few samples & not all of them.
Any suggestions on this.
Thanks & Regards.
 
G

Gabriel Genellina

How do I pass the elements populated in struct variables of this
vector dynamically to PyEval_CallFunction, in the fashion somewhat
like below?
PyEval_CallFunction(obj, "iii", vector[0].ioparam->nionum,vector[1].ioparam->nionum,vector[2].ioparam->nionum);

PyEval_CallFunction(obj, "di", vector[0].ioparam->fionum,vector[1].ioparam->nionum);

PyEval_CallFunction(obj, "diiisis", vector[0].ioparam->fionum,vector[1].ioparam->nionum,vector[2].ioparam-
nionum,vector[3].ioparam->nionum,vector[4].ioparam-
ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);

I didn't know of PyEval_CallFunction until now, but aparently the
lines above are OK. Can't you use it that way? What's your problem?
 
G

grbgooglefan

How do I pass the elements populated in struct variables of this
vector dynamically to PyEval_CallFunction, in the fashion somewhat
like below?
PyEval_CallFunction(obj, "iii", vector[0].ioparam->nionum,vector[1].ioparam->nionum,vector[2].ioparam->nion­um);
PyEval_CallFunction(obj, "di", vector[0].ioparam->fionum,vector[1].ioparam->nionum);
PyEval_CallFunction(obj, "diiisis", vector[0].ioparam->fionum,vector[1].ioparam->nionum,vector[2].ioparam-
nionum,vector[3].ioparam->nionum,vector[4].ioparam-
ioString,vector[5].ioparam->nionum,vector[6].ioparam->ioString);

I didn't know of PyEval_CallFunction until now, but aparently the
lines above are OK. Can't you use it that way? What's your problem?

slowness of current code - vector is to parsed one by one & switch
adds another time for checks.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top