By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,096 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,096 IT Pros & Developers. It's quick & easy.

help with Python C-API, tuple object

P: n/a
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

May 12 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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,

--
Carsten Haese
http://informixdb.sourceforge.net

May 12 '07 #2

P: n/a
On May 12, 2:49 pm, "Carsten Haese" <cars...@uniqsys.comwrote:
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 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,

--
Carsten Haesehttp://informixdb.sourceforge.net
Thanks billions!!! Works perfect, the world would be a lot worse
without Pro's like you guys helping newbies like me ;-)

May 12 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.