help with Python C-API, tuple object

F

fabian.conrad

Hi,
sorry for the rather basic question but I've searched everywhere and
don't find an answer.
I want to call PyObject_CallObject from the Python C-API and pass a
tuple I've created from a C-array

How can I pass the tuple as an object rather then having to declare
the python function with the number of arguments equal to the no of
elements in the tuple?

Example:
C-Code fragment:
PyObject *pArgs = PyTuple_New(3);
//module is imported and function object is build and checked
for (i=0; i<3; i++){
pInt = PyInt_FromLong(i);
error = PyTuple_SetItem(pArgs, i, pInt);
}
pValue=PyObject_CallObject(pFunc, pArgs);//returns NULL!!!

Python Script:
def length(a):
length = len(a)
return length
 
C

Carsten Haese

On 11 May 2007 21:11:06 -0700, fabian.conrad wrote
Hi,
sorry for the rather basic question but I've searched everywhere and
don't find an answer.
I want to call PyObject_CallObject from the Python C-API and pass a
tuple I've created from a C-array

How can I pass the tuple as an object rather then having to declare
the python function with the number of arguments equal to the no of
elements in the tuple?

The documentation at http://docs.python.org/api/object.html lists various Call
flavors. The one you chose will unpack the tuple as if you called
pFunc(*pArgs), which results in the undesired behavior of pFunc receiving
three arguments. You want it to receive one argument that is the tuple pArgs.

One (bad) way of achieving this would be to build another tuple of size one,
stick pArgs into it, and pass that size-one tuple to PyObject_CallObject. A
better way is to use PyObject_CallFunctionObjArgs. If I'm not mistaken, you'll
want something like

pValue = PyObject_CallFunctionObjArgs(pFunc, pArgs, NULL);

Hope this helps,
 
F

fabian.conrad

On 11 May 2007 21:11:06 -0700, fabian.conrad wrote



The documentation athttp://docs.python.org/api/object.htmllists various Call
flavors. The one you chose will unpack the tuple as if you called
pFunc(*pArgs), which results in the undesired behavior of pFunc receiving
three arguments. You want it to receive one argument that is the tuple pArgs.

One (bad) way of achieving this would be to build another tuple of size one,
stick pArgs into it, and pass that size-one tuple to PyObject_CallObject. A
better way is to use PyObject_CallFunctionObjArgs. If I'm not mistaken, you'll
want something like

pValue = PyObject_CallFunctionObjArgs(pFunc, pArgs, NULL);

Hope this helps,

Thanks billions!!! Works perfect, the world would be a lot worse
without Pro's like you guys helping newbies like me ;-)
 

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,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top